1 | <?php |
||
2 | /* |
||
3 | * @copyright 2018 Mautic Contributors. All rights reserved |
||
4 | * @author Mautic |
||
5 | * |
||
6 | * @link http://mautic.org |
||
7 | * |
||
8 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
9 | */ |
||
10 | |||
11 | namespace Mautic\SmsBundle\Sms; |
||
12 | |||
13 | use Mautic\LeadBundle\Entity\Lead; |
||
14 | use Mautic\PluginBundle\Helper\IntegrationHelper; |
||
15 | use Mautic\SmsBundle\Entity\Stat; |
||
16 | use Mautic\SmsBundle\Exception\PrimaryTransportNotEnabledException; |
||
17 | |||
18 | class TransportChain |
||
19 | { |
||
20 | /** |
||
21 | * @var TransportInterface[] |
||
22 | */ |
||
23 | private $transports; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $primaryTransport; |
||
29 | |||
30 | /** |
||
31 | * @var IntegrationHelper |
||
32 | */ |
||
33 | private $integrationHelper; |
||
34 | |||
35 | /** |
||
36 | * @param string $primaryTransport |
||
37 | */ |
||
38 | public function __construct($primaryTransport, IntegrationHelper $integrationHelper) |
||
39 | { |
||
40 | $this->primaryTransport = $primaryTransport; |
||
41 | $this->transports = []; |
||
42 | $this->integrationHelper = $integrationHelper; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param string $alias |
||
47 | * @param string $translatableAlias |
||
48 | * @param string $integrationAlias |
||
49 | * |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function addTransport($alias, TransportInterface $transport, $translatableAlias, $integrationAlias) |
||
53 | { |
||
54 | $this->transports[$alias]['alias'] = $translatableAlias; |
||
55 | $this->transports[$alias]['integrationAlias'] = $integrationAlias; |
||
56 | $this->transports[$alias]['service'] = $transport; |
||
57 | |||
58 | return $this; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Return the transport defined in parameters. |
||
63 | * |
||
64 | * @return TransportInterface |
||
65 | * |
||
66 | * @throws PrimaryTransportNotEnabledException |
||
67 | */ |
||
68 | public function getPrimaryTransport() |
||
69 | { |
||
70 | $enabled = $this->getEnabledTransports(); |
||
71 | |||
72 | // If there no primary transport selected and there is just one available we will use it as primary |
||
73 | if (1 === count($enabled)) { |
||
74 | return array_shift($enabled); |
||
75 | } |
||
76 | |||
77 | if (0 === count($enabled)) { |
||
78 | throw new PrimaryTransportNotEnabledException('Primary SMS transport is not enabled'); |
||
79 | } |
||
80 | |||
81 | if (!array_key_exists($this->primaryTransport, $enabled)) { |
||
82 | throw new PrimaryTransportNotEnabledException('Primary SMS transport is not enabled. '.$this->primaryTransport); |
||
83 | } |
||
84 | |||
85 | return $enabled[$this->primaryTransport]; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param string $content |
||
90 | * |
||
91 | * @return mixed |
||
92 | * |
||
93 | * @throws \Exception |
||
94 | */ |
||
95 | public function sendSms(Lead $lead, $content, Stat $stat = null) |
||
96 | { |
||
97 | return $this->getPrimaryTransport()->sendSms($lead, $content, $stat); |
||
0 ignored issues
–
show
|
|||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Get all transports registered in service container. |
||
102 | * |
||
103 | * @return TransportInterface[] |
||
104 | */ |
||
105 | public function getTransports() |
||
106 | { |
||
107 | return $this->transports; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param string $transport |
||
112 | * |
||
113 | * @return TransportInterface |
||
114 | * |
||
115 | * @throws PrimaryTransportNotEnabledException |
||
116 | */ |
||
117 | public function getTransport($transport) |
||
118 | { |
||
119 | $enabled = $this->getEnabledTransports(); |
||
120 | |||
121 | if (!array_key_exists($transport, $enabled)) { |
||
122 | throw new PrimaryTransportNotEnabledException($transport.' SMS transport is not enabled or does not exist'); |
||
123 | } |
||
124 | |||
125 | return $enabled[$transport]; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Get published transports. |
||
130 | * |
||
131 | * @return TransportInterface[] |
||
132 | */ |
||
133 | public function getEnabledTransports() |
||
134 | { |
||
135 | $enabled = []; |
||
136 | foreach ($this->transports as $alias => $transport) { |
||
137 | if (!isset($transport['published'])) { |
||
138 | $integration = $this->integrationHelper->getIntegrationObject($transport['integrationAlias']); |
||
139 | if (!$integration) { |
||
140 | continue; |
||
141 | } |
||
142 | $transport['published'] = $integration->getIntegrationSettings()->getIsPublished(); |
||
143 | $this->transports[$alias] = $transport; |
||
144 | } |
||
145 | if ($transport['published']) { |
||
146 | $enabled[$alias] = $transport['service']; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | return $enabled; |
||
151 | } |
||
152 | } |
||
153 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.