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 ( 15375f...2893b6 )
by Alexander
02:22
created

CredentialsMapper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 61
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mapCredentials() 0 17 3
A getCredentialAttributes() 0 13 2
A validateAttributes() 0 13 2
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\Utils\ClientCredentials;
11
12
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\NullCredentials;
13
use Zbox\UnifiedPush\Exception\InvalidArgumentException;
14
15
/**
16
 * Class CredentialsMapper
17
 * @package Zbox\UnifiedPush\Utils\ClientCredentials
18
 */
19
class CredentialsMapper
20
{
21
    /**
22
     * @param CredentialsInterface $credentials
23
     * @param array $params
24
     * @return CredentialsInterface
25
     */
26
    public function mapCredentials(CredentialsInterface $credentials, array $params)
27
    {
28
        if (empty($params)) {
29
            return new NullCredentials();
30
        }
31
32
        $attributes = $this->getCredentialAttributes($credentials);
33
34
        $this->validateAttributes($attributes, array_keys($params));
35
36
        foreach ($attributes as $attribute) {
37
            $setterName = sprintf('set%s', ucfirst($attribute));
38
            $credentials->$setterName($params[$attribute]);
39
        }
40
41
        return $credentials;
42
    }
43
44
    /**
45
     * @param CredentialsInterface $credentials
46
     * @return array
47
     */
48
    protected function getCredentialAttributes(CredentialsInterface $credentials)
49
    {
50
        $reflection = new \ReflectionObject($credentials);
51
        $properties = $reflection->getProperties(\ReflectionProperty::IS_PRIVATE);
52
53
        $attributesList = array();
54
55
        foreach ($properties as $property) {
56
            $attributesList[] = $property->getName();
57
        }
58
59
        return $attributesList;
60
    }
61
62
    /**
63
     * @param array $required
64
     * @param array $acquired
65
     */
66
    protected function validateAttributes($required, $acquired)
67
    {
68
        $missingAttributes = array_diff($required, $acquired);
69
70
        if (!empty($missingAttributes)) {
71
            throw new InvalidArgumentException(
72
                sprintf(
73
                    'Mandatory attribute missing: %s',
74
                    implode(', ', $missingAttributes)
75
                )
76
            );
77
        }
78
    }
79
}
80