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

Messenger::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
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
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 Overtrue\EasySms\Contracts\MessageInterface;
15
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
16
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
17
use Overtrue\EasySms\Support\Config;
18
19
/**
20
 * Class Messenger.
21
 */
22
class Messenger
23
{
24
    const STATUS_SUCCESS = 'success';
25
26
    const STATUS_FAILURE = 'failure';
27
28
    /**
29
     * @var \Overtrue\EasySms\EasySms
30
     */
31
    protected $easySms;
32
33
    /**
34
     * Messenger constructor.
35
     *
36
     * @param \Overtrue\EasySms\EasySms $easySms
37
     */
38 1
    public function __construct(EasySms $easySms)
39
    {
40 1
        $this->easySms = $easySms;
41 1
    }
42
43
    /**
44
     * Send a message.
45
     *
46
     * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
47
     * @param \Overtrue\EasySms\Contracts\MessageInterface     $message
48
     * @param array                                            $gateways
49
     *
50
     * @return array
51
     *
52
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
53
     * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
54
     */
55
    public function send(PhoneNumberInterface $to, MessageInterface $message, array $gateways = [])
56
    {
57
        if (empty($gateways)) {
58
            $gateways = $message->getGateways();
59
        }
60
61
        if (empty($gateways)) {
62
            $gateways = $this->easySms->getConfig()->get('default.gateways', []);
63
        }
64
65
        $gateways = $this->formatGateways($gateways);
66
        $strategyAppliedGateways = $this->easySms->strategy()->apply($gateways);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $strategyAppliedGateways exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
67
68
        $results = [];
69
        $isSuccessful = false;
70
        foreach ($strategyAppliedGateways as $gateway) {
71
            try {
72
                $results[$gateway] = [
73
                    'status' => self::STATUS_SUCCESS,
74
                    'result' => $this->easySms->gateway($gateway)->send($to, $message, new Config($gateways[$gateway])),
75
                ];
76
                $isSuccessful = true;
77
78
                break;
79
            } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
80
                $results[$gateway] = [
81
                    'status' => self::STATUS_FAILURE,
82
                    'exception' => $e,
83
                ];
84
            } catch (\Exception $e) {
85
                $results[$gateway] = [
86
                    'status' => self::STATUS_FAILURE,
87
                    'exception' => $e,
88
                ];
89
            }
90
        }
91
92
        if (!$isSuccessful) {
93
            throw new NoGatewayAvailableException($results);
94
        }
95
96
        return $results;
97
    }
98
99
    /**
100
     * @param array $gateways
101
     *
102
     * @return array
103
     */
104
    protected function formatGateways(array $gateways)
105
    {
106
        $formatted = [];
107
        $config = $this->easySms->getConfig();
108
109
        foreach ($gateways as $gateway => $setting) {
110
            if (is_int($gateway) && is_string($setting)) {
111
                $gateway = $setting;
112
                $setting = [];
113
            }
114
115
            $formatted[$gateway] = $setting;
116
            $globalSetting = $config->get("gateways.{$gateway}", []);
117
118
            if (is_string($gateway) && !empty($globalSetting) && is_array($setting)) {
119
                $formatted[$gateway] = array_merge($globalSetting, $setting);
120
            }
121
        }
122
123
        return $formatted;
124
    }
125
}
126