1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LBHurtado\Missive\Actions; |
4
|
|
|
|
5
|
|
|
use LBHurtado\Missive\Facades\Missive; |
6
|
|
|
use LBHurtado\Missive\Classes\SMSAbstract; |
7
|
|
|
use LBHurtado\Tactician\Classes\ActionAbstract; |
8
|
|
|
use LBHurtado\Tactician\Contracts\ActionInterface; |
9
|
|
|
use LBHurtado\Missive\Events\{SMSEvent, SMSEvents}; |
10
|
|
|
use LBHurtado\Missive\Jobs\{CreateContact, CreateRelay, ProcessSMS}; |
11
|
|
|
use LBHurtado\Missive\{Commands\CreateSMSCommand, Handlers\CreateSMSHandler, |
12
|
|
|
Responders\CreateSMSResponder, Validators\CreateSMSValidator}; |
13
|
|
|
|
14
|
|
|
class CreateSMSAction extends ActionAbstract implements ActionInterface |
15
|
|
|
{ |
16
|
|
|
protected $command = CreateSMSCommand::class; |
17
|
|
|
|
18
|
|
|
protected $handler = CreateSMSHandler::class; |
19
|
|
|
|
20
|
|
|
protected $middlewares = [ |
21
|
|
|
CreateSMSValidator::class, |
22
|
|
|
CreateSMSResponder::class, |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
public function setup() |
26
|
|
|
{ |
27
|
|
|
$this->getDispatcher()->handle(SMSEvents::CREATED, function (SMSEvent $event) { |
28
|
|
|
tap($event->getSMS(), function (SMSAbstract $sms) { |
29
|
|
|
$this->dispatch(new CreateContact($sms->from)); |
30
|
|
|
$this->dispatch(new CreateRelay($sms->to)); |
31
|
|
|
$this->dispatch(new ProcessSMS($sms)); |
32
|
|
|
}); |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getCommand(): string |
37
|
|
|
{ |
38
|
|
|
return config('missive.classes.commands.sms.create', $this->command); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getHandler(): string |
42
|
|
|
{ |
43
|
|
|
return config('missive.classes.handlers.sms.create', $this->handler); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getMiddlewares(): array |
47
|
|
|
{ |
48
|
|
|
return config('missive.classes.middlewares.sms.relay', $this->middlewares); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Extract the values in the associative array in config('missive.relay'). |
53
|
|
|
* |
54
|
|
|
* The following array array |
55
|
|
|
* |
56
|
|
|
* [ |
57
|
|
|
* 'from' => 'from_number', |
58
|
|
|
* 'to' => 'to_number', |
59
|
|
|
* 'message' => 'content', |
60
|
|
|
* ] |
61
|
|
|
* |
62
|
|
|
* becomes |
63
|
|
|
* |
64
|
|
|
* [ |
65
|
|
|
* 'from_number', |
66
|
|
|
* 'to_number', |
67
|
|
|
* 'content', |
68
|
|
|
* ] |
69
|
|
|
* |
70
|
|
|
* which is needed as properties of the command required by the tactician package |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function getFields(): array |
75
|
|
|
{ |
76
|
|
|
return optional(Missive::getRelayProviderConfig(), function ($mapping) { |
77
|
|
|
return array_keys(array_flip($mapping)); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|