|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LBHurtado\Missive; |
|
4
|
|
|
|
|
5
|
|
|
use LBHurtado\Missive\Models\Topup; |
|
6
|
|
|
use LBHurtado\Missive\Models\Contact; |
|
7
|
|
|
use LBHurtado\Missive\Types\ChargeType; |
|
8
|
|
|
use LBHurtado\Missive\Classes\SMSAbstract; |
|
9
|
|
|
use LBHurtado\Missive\Pivots\AirtimeContact; |
|
10
|
|
|
use LBHurtado\Missive\Repositories\AirtimeRepository; |
|
11
|
|
|
|
|
12
|
|
|
class Missive |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var \LBHurtado\Missive\Classes\SMSAbstract */ |
|
15
|
|
|
protected $sms; |
|
16
|
|
|
|
|
17
|
|
|
/** @var \LBHurtado\Missive\Repositories\AirtimeRepository */ |
|
18
|
|
|
protected $airtimes; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(AirtimeRepository $airtimes) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->airtimes = $airtimes; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param SMSAbstract $sms |
|
27
|
|
|
* @return $this |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setSMS(SMSAbstract $sms) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->sms = $sms; |
|
32
|
|
|
|
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return SMSAbstract |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getSMS(): SMSAbstract |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->sms; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return Contact |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getContact(): Contact |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->getSMS()->origin; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param ChargeType $key |
|
54
|
|
|
* @param int $qty |
|
55
|
|
|
*/ |
|
56
|
|
|
public function chargeSMS(ChargeType $key, int $qty = 1) |
|
57
|
|
|
{ |
|
58
|
|
|
//TODO: return newly created pivot record |
|
59
|
|
|
tap($this->getContact(), function ($contact) use ($key, $qty) { |
|
60
|
|
|
optional($this->airtimes->findWhere(['key' => $key->value()])->first(), function ($airtime) use ($contact, $qty) { |
|
61
|
|
|
$contact->addAirtime($airtime, (new AirtimeContact)->setQty($qty)); |
|
62
|
|
|
}); |
|
63
|
|
|
}); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $otp |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
|
|
public function verifyContact(string $otp) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->getContact()->verify(trim($otp)); |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param array $attributes |
|
79
|
|
|
*/ |
|
80
|
|
|
public function topupMobile(array $attributes) |
|
81
|
|
|
{ |
|
82
|
|
|
$mobile = $attributes['mobile']; |
|
83
|
|
|
$amount = $attributes['amount']; |
|
84
|
|
|
|
|
85
|
|
|
//TODO: create a separate package for SMS Driver Manager |
|
86
|
|
|
tap(Contact::create(compact('mobile')), function ($contact) use ($amount) { |
|
87
|
|
|
Topup::make(compact( 'amount'))->contact()->associate($contact)->save(); |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Extract the associative array in config('missive.relay') |
|
93
|
|
|
* to be used for default relay configuration e.g. |
|
94
|
|
|
* |
|
95
|
|
|
* [ |
|
96
|
|
|
* 'from' => 'from_number', |
|
97
|
|
|
* 'to' => 'to_number', |
|
98
|
|
|
* 'message' => 'content', |
|
99
|
|
|
* ] |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getRelayProviderConfig(): array |
|
104
|
|
|
{ |
|
105
|
|
|
return config('missive.relay.providers')[config('missive.relay.default')]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Extract the associative array in config('tactician.fields') |
|
110
|
|
|
* and "merge: it with the relay provider config to be used for validation e.g. |
|
111
|
|
|
* |
|
112
|
|
|
* from |
|
113
|
|
|
* |
|
114
|
|
|
* [ |
|
115
|
|
|
* 'from' => 'required', |
|
116
|
|
|
* 'to' => 'required', |
|
117
|
|
|
* 'message' => 'string|max:800', |
|
118
|
|
|
* ] |
|
119
|
|
|
* |
|
120
|
|
|
* to |
|
121
|
|
|
* |
|
122
|
|
|
* [ |
|
123
|
|
|
* 'from_number' => 'required', |
|
124
|
|
|
* 'to_number' => 'required', |
|
125
|
|
|
* 'content' => 'string|max:800', |
|
126
|
|
|
* ] |
|
127
|
|
|
* |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getRelayRules(): array |
|
131
|
|
|
{ |
|
132
|
|
|
$relayRules = config('tactician.fields'); |
|
133
|
|
|
|
|
134
|
|
|
return optional(array_flip($this->getRelayProviderConfig()), function ($mapping) use ($relayRules) { |
|
135
|
|
|
$rules = []; |
|
136
|
|
|
foreach ($mapping as $field => $rule) { |
|
137
|
|
|
$rules[$field] = $relayRules[$rule]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $rules; |
|
141
|
|
|
}); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
//TODO: create an artisan command to challenge |
|
145
|
|
|
} |
|
146
|
|
|
|