Completed
Push — master ( 17ee4a...c5eac5 )
by Atymic
10:15
created

ConfigRetriever   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
dl 0
loc 134
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2
ccs 40
cts 42
cp 0.9524
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fromServices() 0 16 1
A getConfigItems() 0 4 1
A retrieveItemsFromConfig() 0 10 2
A getFromServices() 0 16 5
A getConfigFromServicesArray() 0 19 3
A isAdditionalConfig() 0 4 1
1
<?php
2
3
namespace SocialiteProviders\Manager\Helpers;
4
5
use Closure;
6
use SocialiteProviders\Manager\Config;
7
use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface;
8
use SocialiteProviders\Manager\Exception\MissingConfigException;
9
10
class ConfigRetriever implements ConfigRetrieverInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $providerName;
16
17
    /**
18
     * @var string
19
     */
20
    protected $providerIdentifier;
21
22
    /**
23
     * @var array
24
     */
25
    protected $servicesArray;
26
27
    /**
28
     * @var array
29
     */
30
    protected $additionalConfigKeys;
31
32
    /**
33 2
     * @param string $providerName
34
     * @param array  $additionalConfigKeys
35 2
     *
36
     * @return \SocialiteProviders\Manager\Contracts\ConfigInterface
37 2
     */
38 2
    public function fromServices($providerName, array $additionalConfigKeys = [])
39 1
    {
40 1
        $this->providerName = $providerName;
41
        $this->getConfigFromServicesArray($providerName);
42 1
43 1
        $this->additionalConfigKeys = $additionalConfigKeys = array_unique($additionalConfigKeys + ['guzzle']);
44
45
        return new Config(
46
            $this->getFromServices('client_id'),
47
            $this->getFromServices('client_secret'),
48
            $this->getFromServices('redirect'),
0 ignored issues
show
Bug introduced by
It seems like $this->getFromServices('redirect') targeting SocialiteProviders\Manag...ever::getFromServices() can also be of type null; however, SocialiteProviders\Manager\Config::__construct() does only seem to accept callable, 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...
49
            $this->getConfigItems($additionalConfigKeys, function ($key) {
50
                return $this->getFromServices(strtolower($key));
51
            })
52
        );
53 2
    }
54
55 2
    /**
56 2
     * @param array    $configKeys
57
     * @param \Closure $keyRetrievalClosure
58 1
     *
59 1
     * @return array
60 1
     */
61 1
    protected function getConfigItems(array $configKeys, Closure $keyRetrievalClosure)
62 1
    {
63 1
        return $this->retrieveItemsFromConfig($configKeys, $keyRetrievalClosure);
64 1
    }
65
66
    /**
67
     * @param array    $keys
68
     * @param \Closure $keyRetrievalClosure
69
     *
70
     * @return array
71
     */
72
    protected function retrieveItemsFromConfig(array $keys, Closure $keyRetrievalClosure)
73 2
    {
74
        $out = [];
75 2
76
        foreach ($keys as $key) {
77
            $out[$key] = $keyRetrievalClosure($key);
78
        }
79 2
80
        return $out;
81
    }
82
83
    /**
84
     * @param string $key
85
     *
86
     * @return string|null
87
     *
88 2
     * @throws \SocialiteProviders\Manager\Exception\MissingConfigException
89
     */
90 2
    protected function getFromServices($key)
91
    {
92 2
        $keyExists = array_key_exists($key, $this->servicesArray);
93 2
94 2
        // ADDITIONAL value is empty
95
        if (! $keyExists && $this->isAdditionalConfig($key)) {
96 2
            return $key == 'guzzle' ? [] : null;
0 ignored issues
show
Bug Compatibility introduced by
The expression $key == 'guzzle' ? array() : null; of type array|null adds the type array to the return on line 96 which is incompatible with the return type documented by SocialiteProviders\Manag...riever::getFromServices of type string|null.
Loading history...
97
        }
98
99
        // REQUIRED value is empty
100
        if (! $keyExists) {
101
            throw new MissingConfigException("Missing services entry for {$this->providerName}.$key");
102
        }
103
104
        return $this->servicesArray[$key];
105 1
    }
106
107 1
    /**
108
     * @param string $providerName
109
     *
110
     * @return array
111 1
     *
112
     * @throws \SocialiteProviders\Manager\Exception\MissingConfigException
113
     */
114
    protected function getConfigFromServicesArray($providerName)
115
    {
116
        $configArray = config("services.{$providerName}");
117
118
        if (empty($configArray)) {
119
            // If we are running in console we should spoof values to make Socialite happy...
120 2
            if (app()->runningInConsole()) {
121
                $configArray = [
122 2
                    'client_id' => "{$this->providerIdentifier}_KEY",
123 2
                    'client_secret' => "{$this->providerIdentifier}_SECRET",
124
                    'redirect' => "{$this->providerIdentifier}_REDIRECT_URI",
125 2
                ];
126 1
            } else {
127
                throw new MissingConfigException("There is no services entry for $providerName");
128
            }
129 1
        }
130
131
        return $this->servicesArray = $configArray;
132
    }
133
134
    /**
135
     * @param string $key
136
     *
137
     * @return bool
138 2
     */
139
    protected function isAdditionalConfig($key)
140 2
    {
141 2
        return in_array(strtolower($key), $this->additionalConfigKeys, true);
142
    }
143
}
144