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 ( 58a2c7...b93be4 )
by Alexander
03:10
created

getCredentialsDTOByServiceName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 11
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),
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
146
            case NotificationServices::CREDENTIALS_AUTH_TOKEN:
147
                return new AuthToken();
148
149
            case NotificationServices::CREDENTIALS_NULL:
150
                return new NullCredentials();
151
152
            default:
153
                throw new DomainException(sprintf("Unsupported credentials type '%s'", $credentialsType));
154
        }
155
    }
156
}
157