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 ( 31963b...74f330 )
by Alexander
02:26
created

ServiceCredentialsFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 99
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setCredentialsPath() 0 5 1
A loadServiceCredentialsConfig() 0 23 3
A getInitializedServices() 0 4 1
A getCredentialsByService() 0 21 3
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 string
21
     */
22
    protected $credentialsPath;
23
24
    /**
25
     * @var array
26
     */
27
    protected $config;
28
29
    /**
30
     * @param CredentialsMapper $credentialsMapper
31
     */
32
    public function __construct(CredentialsMapper $credentialsMapper)
33
    {
34
        $this->credentialsMapper = $credentialsMapper;
0 ignored issues
show
Bug introduced by
The property credentialsMapper does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
    }
36
37
    /**
38
     * @param string $credentialsPath
39
     * @return $this
40
     */
41
    public function setCredentialsPath($credentialsPath)
42
    {
43
        $this->credentialsPath = $credentialsPath;
44
        return $this;
45
    }
46
47
    /**
48
     * Load sender`s notification services credentials
49
     *
50
     * @return $this
51
     * @throws DomainException
52
     */
53
    public function loadServiceCredentialsConfig()
54
    {
55
        $configPath = $this->credentialsPath;
56
57
        if (!file_exists($configPath)) {
58
            throw new InvalidArgumentException(
59
                sprintf(
60
                    "Credentials file '%s' doesn`t exists",
61
                    $configPath
62
                )
63
            );
64
        }
65
66
        $config = json_decode(file_get_contents($configPath), true);
67
68
        if (empty($config)) {
69
            throw new RuntimeException('Empty credentials config');
70
        }
71
72
        $this->config = $config;
0 ignored issues
show
Documentation Bug introduced by
It seems like $config of type * is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
74
        return $this;
75
    }
76
77
    /**
78
     * Returns the list of names of notification services
79
     *
80
     * @return array
81
     */
82
    public function getInitializedServices()
83
    {
84
        return array_keys($this->config);
85
    }
86
87
    /**
88
     * Returns credentials for notification service
89
     *
90
     * @param string $serviceName
91
     * @throws DomainException
92
     * @return array
93
     */
94
    public function getCredentialsByService($serviceName)
95
    {
96
        if (empty($this->config)) {
97
            $this->config = $this->loadServiceCredentialsConfig();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->loadServiceCredentialsConfig() of type this<Zbox\UnifiedPush\No...viceCredentialsFactory> is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
98
        }
99
100
        if (!in_array($serviceName, $this->getInitializedServices())) {
101
            throw new DomainException(
102
                sprintf("Credentials for service '%s' was not initialized", $serviceName)
103
            );
104
        }
105
106
        return
107
            $this
108
                ->credentialsMapper
109
                ->mapCredentials(
110
                    NotificationServices::getCredentialsTypeByService($serviceName),
111
                    $this->config[$serviceName]
112
                )
113
            ;
114
    }
115
}
116