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 ( 281989...6cfff6 )
by Alexander
02:37
created

ServiceCredentialsFactory::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
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
17
class ServiceCredentialsFactory
18
{
19
    /**
20
     * @var CredentialsMapper
21
     */
22
    protected $credentialsMapper;
23
24
    /**
25
     * @var string
26
     */
27
    protected $credentialsPath;
28
29
    /**
30
     * @var array
31
     */
32
    protected $config;
33
34
    /**
35
     * @param CredentialsMapper $credentialsMapper
36
     */
37
    public function __construct(CredentialsMapper $credentialsMapper)
38
    {
39
        $this->credentialsMapper = $credentialsMapper;
40
    }
41
42
    /**
43
     * @param string $credentialsPath
44
     * @return $this
45
     */
46
    public function setCredentialsPath($credentialsPath)
47
    {
48
        $this->credentialsPath = $credentialsPath;
49
        return $this;
50
    }
51
52
    /**
53
     * @param array $config
54
     * @return $this
55
     */
56
    public function setConfig(array $config)
57
    {
58
        $this->config = $config;
59
        return $this;
60
    }
61
62
    /**
63
     * Load sender`s notification services credentials
64
     *
65
     * @return $this
66
     * @throws DomainException
67
     */
68
    public function loadServiceCredentialsConfig()
69
    {
70
        $configPath = $this->credentialsPath;
71
72
        if (!file_exists($configPath)) {
73
            throw new InvalidArgumentException(
74
                sprintf(
75
                    "Credentials file '%s' doesn`t exists",
76
                    $configPath
77
                )
78
            );
79
        }
80
81
        $config = json_decode(file_get_contents($configPath), true);
82
83
        if (!is_array($config)) {
84
            throw new RuntimeException('Empty credentials config');
85
        }
86
87
        $this->config = $config;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Returns the list of names of notification services
94
     *
95
     * @return array
96
     */
97
    public function getInitializedServices()
98
    {
99
        return array_keys($this->config);
100
    }
101
102
    /**
103
     * Returns credentials for notification service
104
     *
105
     * @param string $serviceName
106
     * @throws DomainException
107
     * @return array
108
     */
109
    public function getCredentialsByService($serviceName)
110
    {
111
        if (empty($this->config)) {
112
            $this->loadServiceCredentialsConfig();
113
        }
114
115
        if (!in_array($serviceName, $this->getInitializedServices())) {
116
            throw new DomainException(
117
                sprintf("Credentials for service '%s' was not initialized", $serviceName)
118
            );
119
        }
120
121
        return
122
            $this
123
                ->credentialsMapper
124
                ->mapCredentials(
125
                    NotificationServices::getCredentialsTypeByService($serviceName),
126
                    $this->config[$serviceName]
127
                )
128
            ;
129
    }
130
}
131