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.
Completed
Push — master ( b6f376...58a2c7 )
by Alexander
03:08
created

getCredentialsDTOByServiceName()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 15
nc 4
nop 1
1
<?php
2
3
/*
4
 * (c) Alexander Zhukov <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Zbox\UnifiedPush\NotificationService;
11
12
use Zbox\UnifiedPush\Utils\ClientCredentials\CredentialsMapper;
13
use Zbox\UnifiedPush\Exception\InvalidArgumentException;
14
use Zbox\UnifiedPush\Exception\RuntimeException;
15
use Zbox\UnifiedPush\Exception\DomainException;
16
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\AuthToken;
17
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\NullCredentials;
18
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\SSLCertificate;
19
20
class ServiceCredentialsFactory
21
{
22
    /**
23
     * @var CredentialsMapper
24
     */
25
    protected $credentialsMapper;
26
27
    /**
28
     * @var string
29
     */
30
    protected $credentialsPath;
31
32
    /**
33
     * @var array
34
     */
35
    protected $config;
36
37
    /**
38
     * @param CredentialsMapper $credentialsMapper
39
     */
40
    public function __construct(CredentialsMapper $credentialsMapper)
41
    {
42
        $this->credentialsMapper = $credentialsMapper;
43
    }
44
45
    /**
46
     * @param string $credentialsPath
47
     * @return $this
48
     */
49
    public function setCredentialsPath($credentialsPath)
50
    {
51
        $this->credentialsPath = $credentialsPath;
52
        return $this;
53
    }
54
55
    /**
56
     * @param array $config
57
     * @return $this
58
     */
59
    public function setConfig(array $config)
60
    {
61
        $this->config = $config;
62
        return $this;
63
    }
64
65
    /**
66
     * Load sender`s notification services credentials
67
     *
68
     * @return $this
69
     * @throws DomainException
70
     */
71
    public function loadServiceCredentialsConfig()
72
    {
73
        $configPath = $this->credentialsPath;
74
75
        if (!file_exists($configPath)) {
76
            throw new InvalidArgumentException(
77
                sprintf(
78
                    "Credentials file '%s' doesn`t exists",
79
                    $configPath
80
                )
81
            );
82
        }
83
84
        $config = json_decode(file_get_contents($configPath), true);
85
86
        if (!is_array($config)) {
87
            throw new RuntimeException('Empty credentials config');
88
        }
89
90
        $this->config = $config;
91
92
        return $this;
93
    }
94
95
    /**
96
     * Returns the list of names of notification services
97
     *
98
     * @return array
99
     */
100
    public function getInitializedServices()
101
    {
102
        return array_keys($this->config);
103
    }
104
105
    /**
106
     * Returns credentials for notification service
107
     *
108
     * @param string $serviceName
109
     * @throws DomainException
110
     * @return array
111
     */
112
    public function getCredentialsByService($serviceName)
113
    {
114
        if (empty($this->config)) {
115
            $this->loadServiceCredentialsConfig();
116
        }
117
118
        if (!in_array($serviceName, $this->getInitializedServices())) {
119
            throw new DomainException(
120
                sprintf("Credentials for service '%s' was not initialized", $serviceName)
121
            );
122
        }
123
124
        return
125
            $this
126
                ->credentialsMapper
127
                ->mapCredentials(
128
                    $this->getCredentialsDTOByServiceName($serviceName),
0 ignored issues
show
Bug introduced by
It seems like $this->getCredentialsDTO...rviceName($serviceName) targeting Zbox\UnifiedPush\Notific...tialsDTOByServiceName() can also be of type null; however, Zbox\UnifiedPush\Utils\C...apper::mapCredentials() does only seem to accept object<Zbox\UnifiedPush\...s\CredentialsInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
129
                    $this->config[$serviceName]
130
                )
131
            ;
132
    }
133
134
    /**
135
     * @param string $serviceName
136
     * @return \Zbox\UnifiedPush\Utils\ClientCredentials\CredentialsInterface
137
     */
138
    private function getCredentialsDTOByServiceName($serviceName)
139
    {
140
        $credentialsType = NotificationServices::getCredentialsTypeByService($serviceName);
141
142
        switch ($credentialsType) {
143
            case NotificationServices::CREDENTIALS_CERTIFICATE:
144
                return new SSLCertificate();
145
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
146
147
            case NotificationServices::CREDENTIALS_AUTH_TOKEN:
148
                return new AuthToken();
149
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
150
151
            case NotificationServices::CREDENTIALS_NULL:
152
                return new NullCredentials();
153
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
154
155
            default:
156
                throw new DomainException(sprintf("Unsupported credentials type '%s'", $credentialsType));
157
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
158
        }
159
    }
160
}
161