/** * bootstrap PhpSms */ protected function initPhpSms() { //export custom rule flag value define('CUSTOM_RULE', SmsManager::CUSTOM_RULE_FLAG); // define how to use queue $queueJob = config('laravel-sms.queueJob', 'App\\Jobs\\SendReminderSms'); Sms::queue(function ($sms, $data) use($queueJob) { if (!class_exists($queueJob)) { throw new LaravelSmsException("Class [{$queueJob}] does not exists."); } $this->dispatch(new $queueJob($sms)); return ['success' => true, 'after_push_to_queue' => true]; }); // before send hook // store sms data to database Sms::beforeSend(function ($task) { if (!config('laravel-sms.database_enable', false)) { return true; } $data = $task->data ?: []; $id = DB::table('laravel_sms')->insertGetId(['to' => $data['to'], 'temp_id' => json_encode($data['templates']), 'data' => json_encode($data['templateData']), 'content' => $data['content'], 'voice_code' => $data['voiceCode'], 'created_at' => date('Y-m-d H:i:s', time())]); $data['smsId'] = $id; $task->data($data); }); // after send hook // update sms data in database Sms::afterSend(function ($task, $result) { if (!config('laravel-sms.database_enable', false)) { return true; } // get time $microTime = $result['time']['finished_at']; $finishedAt = explode(' ', $microTime)[1]; // get sms id $data = $task->data; $smsId = isset($data['smsId']) ? $data['smsId'] : 0; // update database DB::beginTransaction(); $dbData = []; $dbData['updated_at'] = date('Y-m-d H:i:s', $finishedAt); $dbData['result_info'] = json_encode($result['logs']); if ($result['success']) { $dbData['sent_time'] = $finishedAt; } else { DB::table('laravel_sms')->where('id', $smsId)->increment('fail_times'); $dbData['last_fail_time'] = $finishedAt; } DB::table('laravel_sms')->where('id', $smsId)->update($dbData); DB::commit(); }); }
/** * bootstrap PhpSms */ protected function initPhpSms() { // define how to use queue Sms::queue(function ($sms, $data) { $this->dispatch(new SendReminderSms($sms)); return ['success' => true, 'after_push_to_queue' => true]; }); // before send hook // store sms data to database Sms::beforeSend(function ($task) { if (!config('laravel-sms.database_enable', false)) { return true; } $data = $task->data ?: []; $id = DB::table('sms')->insertGetId(['to' => $data['to'], 'temp_id' => json_encode($data['templates']), 'data' => $data['voiceCode'] ?: json_encode($data['templateData']), 'content' => $data['content'], 'created_at' => date('Y-m-d H:i:s', time())]); $data['smsId'] = $id; $task->data($data); }); // after send hook // update sms data in database Sms::afterSend(function ($task, $result) { if (!config('laravel-sms.database_enable', false)) { return true; } // get time $microTime = $result['time']['finished_at']; $finishedAt = explode(' ', $microTime)[1]; // get sms id $data = $task->data; $smsId = isset($data['smsId']) ? $data['smsId'] : 0; // update database DB::beginTransaction(); $dbData = []; $dbData['updated_at'] = date('Y-m-d H:i:s', $finishedAt); $dbData['result_info'] = json_encode($result['logs']); if ($result['success']) { $dbData['sent_time'] = $finishedAt; } else { DB::table('sms')->where('id', $smsId)->increment('fail_times'); $dbData['last_fail_time'] = $finishedAt; } DB::table('sms')->where('id', $smsId)->update($dbData); DB::commit(); }); }
public function testUseQueue() { $status = Sms::queue(); $this->assertFalse($status); //define how to use queue //way 1 Sms::queue(function ($sms, $data) { return 'in_queue_1'; }); $this->assertTrue(Sms::queue()); //define how to use queue //way 2 Sms::queue(false, function ($sms, $data) { return 'in_queue_2'; }); $this->assertFalse(Sms::queue()); //open queue Sms::queue(true); $this->assertTrue(Sms::queue()); //push sms to queue $result = self::$sms->send(); $this->assertEquals('in_queue_2', $result); //force send $result = self::$sms->send(true); $this->assertTrue($result['success']); $this->assertCount(1, $result['logs']); $this->assertEquals('Log', $result['logs'][0]['driver']); }