GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — master ( 9d889d...27a869 )
by Carlos
02:09
created

EasySms::formatMessage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 0
cts 10
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
3
/*
4
 * This file is part of the overtrue/easy-sms.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\EasySms;
13
14
use Closure;
15
use Overtrue\EasySms\Contracts\GatewayInterface;
16
use Overtrue\EasySms\Contracts\MessageInterface;
17
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
18
use Overtrue\EasySms\Contracts\StrategyInterface;
19
use Overtrue\EasySms\Exceptions\InvalidArgumentException;
20
use Overtrue\EasySms\Strategies\OrderStrategy;
21
use Overtrue\EasySms\Support\Config;
22
use RuntimeException;
23
24
/**
25
 * Class EasySms.
26
 */
27
class EasySms
28
{
29
    /**
30
     * @var \Overtrue\EasySms\Support\Config
31
     */
32
    protected $config;
33
34
    /**
35
     * @var string
36
     */
37
    protected $defaultGateway;
38
39
    /**
40
     * @var array
41
     */
42
    protected $customCreators = [];
43
44
    /**
45
     * @var array
46
     */
47
    protected $gateways = [];
48
49
    /**
50
     * @var \Overtrue\EasySms\Messenger
51
     */
52
    protected $messenger;
53
54
    /**
55
     * @var array
56
     */
57
    protected $strategies = [];
58
59
    /**
60
     * Constructor.
61
     *
62 6
     * @param array $config
63
     */
64 6
    public function __construct(array $config)
65
    {
66 6
        $this->config = new Config($config);
67 2
68 2
        if (!empty($config['default'])) {
69 6
            $this->setDefaultGateway($config['default']);
70
        }
71
    }
72
73
    /**
74
     * Send a message.
75
     *
76
     * @param string|array                                       $to
77
     * @param \Overtrue\EasySms\Contracts\MessageInterface|array $message
78
     * @param array                                              $gateways
79
     *
80 1
     * @return array
81
     *
82 1
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
83
     * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
84
     */
85
    public function send($to, $message, array $gateways = [])
86
    {
87
        $to = $this->formatPhoneNumber($to);
0 ignored issues
show
Bug introduced by
It seems like $to can also be of type array; however, Overtrue\EasySms\EasySms::formatPhoneNumber() does only seem to accept string|object<Overtrue\E...s\PhoneNumberInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
88
        $message = $this->formatMessage($message);
89
90
        return $this->getMessenger()->send($to, $message, $gateways);
91
    }
92 4
93
    /**
94 4
     * @param string|\Overtrue\EasySms\Contracts\PhoneNumberInterface $number
95
     *
96 3
     * @return \Overtrue\EasySms\PhoneNumber
97 3
     */
98 3
    protected function formatPhoneNumber($number)
99
    {
100 3
        if ($number instanceof PhoneNumberInterface) {
101
            return $number;
102
        }
103
104
        return new PhoneNumber(intval($number));
105
    }
106
107
    /**
108
     * @param array|string|\Overtrue\EasySms\Contracts\MessageInterface $message
109
     *
110
     * @return \Overtrue\EasySms\Contracts\MessageInterface
111
     */
112
    protected function formatMessage($message)
113
    {
114
        if (!($message instanceof MessageInterface)) {
115
            if (!is_array($message)) {
116
                $message = [
117
                    'content' => strval($message),
118
                    'template' => strval($message),
119
                ];
120
            }
121
122
            $message = new Message($message);
123
        }
124
125
        return $message;
126
    }
127
128
    /**
129
     * Create a gateway.
130
     *
131
     * @param string|null $name
132
     *
133
     * @return \Overtrue\EasySms\Contracts\GatewayInterface
134
     *
135
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
136
     */
137
    public function gateway($name = null)
138
    {
139
        $name = $name ?: $this->getDefaultGateway();
140
141 1
        if (!isset($this->gateways[$name])) {
142
            $this->gateways[$name] = $this->createGateway($name);
143 1
        }
144
145 1
        return $this->gateways[$name];
146
    }
147
148
    /**
149
     * Get a strategy instance.
150
     *
151
     * @param string|null $strategy
152
     *
153
     * @return \Overtrue\EasySms\Contracts\StrategyInterface
154
     *
155
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
156
     */
157
    public function strategy($strategy = null)
158
    {
159
        if (is_null($strategy)) {
160
            $strategy = $this->config->get('default.strategy', OrderStrategy::class);
161
        }
162
163 2
        if (!class_exists($strategy)) {
164
            $strategy = __NAMESPACE__.'\Strategies\\'.ucfirst($strategy);
165 2
        }
166 1
167
        if (!class_exists($strategy)) {
168
            throw new InvalidArgumentException("Unsupported strategy \"{$strategy}\"");
169 1
        }
170
171
        if (empty($this->strategies[$strategy]) || !($this->strategies[$strategy] instanceof StrategyInterface)) {
172
            $this->strategies[$strategy] = new $strategy($this);
173
        }
174
175
        return $this->strategies[$strategy];
176
    }
177
178
    /**
179 2
     * Register a custom driver creator Closure.
180
     *
181 2
     * @param string   $name
182
     * @param \Closure $callback
183 2
     *
184
     * @return $this
185
     */
186
    public function extend($name, Closure $callback)
187
    {
188
        $this->customCreators[$name] = $callback;
189 1
190
        return $this;
191 1
    }
192
193
    /**
194
     * @return \Overtrue\EasySms\Support\Config
195
     */
196
    public function getConfig()
197
    {
198
        return $this->config;
199
    }
200
201
    /**
202
     * Get default gateway name.
203 3
     *
204
     * @return string
205 3
     *
206 1
     * @throws \RuntimeException if no default gateway configured
207 1
     */
208 2
    public function getDefaultGateway()
209 2
    {
210
        if (empty($this->defaultGateway)) {
211
            throw new RuntimeException('No default gateway configured.');
212 3
        }
213 1
214
        return $this->defaultGateway;
215
    }
216 3
217
    /**
218
     * Set default gateway name.
219
     *
220
     * @param string $name
221
     *
222
     * @return $this
223
     */
224
    public function setDefaultGateway($name)
225
    {
226
        $this->defaultGateway = $name;
227 2
228
        return $this;
229 2
    }
230 1
231
    /**
232
     * @return \Overtrue\EasySms\Messenger
233 2
     */
234
    public function getMessenger()
235
    {
236
        return $this->messenger ?: $this->messenger = new Messenger($this);
237
    }
238
239
    /**
240
     * Create a new driver instance.
241
     *
242
     * @param string $name
243 2
     *
244
     * @throws \InvalidArgumentException
245 2
     *
246 1
     * @return GatewayInterface
247
     *
248
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
249 1
     */
250
    protected function createGateway($name)
251 1
    {
252
        if (isset($this->customCreators[$name])) {
253
            $gateway = $this->callCustomCreator($name);
254
        } else {
255
            $className = $this->formatGatewayClassName($name);
256
            $gateway = $this->makeGateway($className, $this->config->get("gateways.{$name}", []));
257
        }
258
259
        if (!($gateway instanceof GatewayInterface)) {
260
            throw new InvalidArgumentException(sprintf('Gateway "%s" not inherited from %s.', $name, GatewayInterface::class));
261 1
        }
262
263 1
        return $gateway;
264
    }
265
266
    /**
267
     * Make gateway instance.
268
     *
269
     * @param string $gateway
270
     * @param array  $config
271
     *
272
     * @return \Overtrue\EasySms\Contracts\GatewayInterface
273
     *
274
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
275
     */
276
    protected function makeGateway($gateway, $config)
277
    {
278
        if (!class_exists($gateway)) {
279
            throw new InvalidArgumentException(sprintf('Gateway "%s" not exists.', $gateway));
280
        }
281
282
        return new $gateway($config);
283
    }
284
285
    /**
286
     * Format gateway name.
287
     *
288
     * @param string $name
289
     *
290
     * @return string
291
     */
292
    protected function formatGatewayClassName($name)
293
    {
294
        if (class_exists($name)) {
295
            return $name;
296
        }
297
298
        $name = ucfirst(str_replace(['-', '_', ''], '', $name));
299
300
        return __NAMESPACE__."\\Gateways\\{$name}Gateway";
301
    }
302
303
    /**
304
     * Call a custom gateway creator.
305
     *
306
     * @param string $gateway
307
     *
308
     * @return mixed
309
     */
310
    protected function callCustomCreator($gateway)
311
    {
312
        return call_user_func($this->customCreators[$gateway], $this->config->get("gateways.{$gateway}", []));
313
    }
314
}
315