|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LBHurtado\Missive\Handlers; |
|
4
|
|
|
|
|
5
|
|
|
use LBHurtado\Missive\Facades\Missive; |
|
6
|
|
|
use LBHurtado\Missive\Repositories\SMSRepository; |
|
7
|
|
|
use LBHurtado\Tactician\Contracts\CommandInterface; |
|
8
|
|
|
use LBHurtado\Tactician\Contracts\HandlerInterface; |
|
9
|
|
|
|
|
10
|
|
|
class CreateSMSHandler implements HandlerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var SMSRepository */ |
|
13
|
|
|
protected $smss; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* CreateSMSHandler constructor. |
|
17
|
|
|
* @param SMSRepository $smss |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(SMSRepository $smss) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->smss = $smss; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Persist the missive in the database using |
|
26
|
|
|
* the repository create method and command attributes. |
|
27
|
|
|
* |
|
28
|
|
|
* @param CommandInterface $command |
|
29
|
|
|
*/ |
|
30
|
|
|
public function handle(CommandInterface $command) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->smss->create($this->getCommandAttributes($command)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Arrange the attributes for creating the model. |
|
37
|
|
|
* |
|
38
|
|
|
* If the relay provider is telerivet: |
|
39
|
|
|
* |
|
40
|
|
|
* from |
|
41
|
|
|
* |
|
42
|
|
|
* [ |
|
43
|
|
|
* 'secret' => 'ABC12344567890', //miscellaneous field |
|
44
|
|
|
* 'from_number' => '+639171234567', |
|
45
|
|
|
* 'to_number' => '+639187654321', |
|
46
|
|
|
* 'content' => 'The quick brown fox...', |
|
47
|
|
|
* ] |
|
48
|
|
|
* |
|
49
|
|
|
* to |
|
50
|
|
|
* |
|
51
|
|
|
* [ |
|
52
|
|
|
* 'from' => '+639171234567', |
|
53
|
|
|
* 'to' => '+639187654321', |
|
54
|
|
|
* 'message' => 'The quick brown fox...', |
|
55
|
|
|
* ] |
|
56
|
|
|
* |
|
57
|
|
|
* @param CommandInterface $command |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getCommandAttributes(CommandInterface $command): array |
|
61
|
|
|
{ |
|
62
|
|
|
return optional(Missive::getRelayProviderConfig(), function ($mapping) use ($command) { |
|
63
|
|
|
$attributes = []; |
|
64
|
|
|
$properties = $command->getProperties(); |
|
65
|
|
|
foreach ($this->getSMSFields() as $field) { |
|
66
|
|
|
$property = $mapping[$field]; |
|
67
|
|
|
$attributes[$field] = $properties[$property]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $attributes; |
|
71
|
|
|
}); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Extract the fields of the SMS model e.g. |
|
76
|
|
|
* |
|
77
|
|
|
* [ |
|
78
|
|
|
* 'from', |
|
79
|
|
|
* 'to', |
|
80
|
|
|
* 'message', |
|
81
|
|
|
* ] |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getSMSFields() |
|
86
|
|
|
{ |
|
87
|
|
|
return app($this->smss->model())->getFillable(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|