Completed
Push — master ( e79a1a...3e0fb7 )
by Miguel
08:55
created

src/Helpers/ConfigRetriever.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
     * @throws \SocialiteProviders\Manager\Exception\MissingConfigException
39 1
     */
40 1
    public function fromServices($providerName, array $additionalConfigKeys = [])
41
    {
42 1
        $this->providerName = $providerName;
43 1
        $this->getConfigFromServicesArray($providerName);
44
45
        $this->additionalConfigKeys = $additionalConfigKeys = array_unique($additionalConfigKeys + ['guzzle']);
46
47
        return new Config(
48
            $this->getFromServices('client_id'),
49
            $this->getFromServices('client_secret'),
50
            $this->getFromServices('redirect'),
51
            $this->getConfigItems($additionalConfigKeys, function ($key) {
52
                return $this->getFromServices(strtolower($key));
53 2
            })
54
        );
55 2
    }
56 2
57
    /**
58 1
     * @param array    $configKeys
59 1
     * @param \Closure $keyRetrievalClosure
60 1
     *
61 1
     * @return array
62 1
     */
63 1
    protected function getConfigItems(array $configKeys, Closure $keyRetrievalClosure)
64 1
    {
65
        return $this->retrieveItemsFromConfig($configKeys, $keyRetrievalClosure);
66
    }
67
68
    /**
69
     * @param array    $keys
70
     * @param \Closure $keyRetrievalClosure
71
     *
72
     * @return array
73 2
     */
74
    protected function retrieveItemsFromConfig(array $keys, Closure $keyRetrievalClosure)
75 2
    {
76
        $out = [];
77
78
        foreach ($keys as $key) {
79 2
            $out[$key] = $keyRetrievalClosure($key);
80
        }
81
82
        return $out;
83
    }
84
85
    /**
86
     * @param string $key
87
     *
88 2
     * @return string
89
     *
90 2
     * @throws \SocialiteProviders\Manager\Exception\MissingConfigException
91
     */
92 2
    protected function getFromServices($key)
93 2
    {
94 2
        $keyExists = array_key_exists($key, $this->servicesArray);
95
96 2
        // ADDITIONAL value is empty
97
        if (!$keyExists && $this->isAdditionalConfig($key)) {
98
            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 98 which is incompatible with the return type documented by SocialiteProviders\Manag...riever::getFromServices of type string.
Loading history...
99
        }
100
101
        // REQUIRED value is empty
102
        if (!$keyExists) {
103
            throw new MissingConfigException("Missing services entry for {$this->providerName}.$key");
104
        }
105 1
106
        return $this->servicesArray[$key];
107 1
    }
108
109
    /**
110
     * @param string $providerName
111 1
     *
112
     * @return array
113
     *
114
     * @throws \SocialiteProviders\Manager\Exception\MissingConfigException
115
     */
116
    protected function getConfigFromServicesArray($providerName)
117
    {
118
        $configArray = config("services.{$providerName}");
119
120 2
        if (empty($configArray)) {
121
            // If we are running in console we should spoof values to make Socialite happy...
122 2
            if (app()->runningInConsole()) {
123 2
                $configArray = [
124
                    'client_id' => "{$this->providerIdentifier}_KEY",
125 2
                    'client_secret' => "{$this->providerIdentifier}_SECRET",
126 1
                    'redirect' => "{$this->providerIdentifier}_REDIRECT_URI",
127
                ];
128
            } else {
129 1
                throw new MissingConfigException("There is no services entry for $providerName");
130
            }
131
        }
132
133
        return $this->servicesArray = $configArray;
134
    }
135
136
    /**
137
     * @param string $key
138 2
     *
139
     * @return bool
140 2
     */
141 2
    protected function isAdditionalConfig($key)
142
    {
143 2
        return in_array(strtolower($key), $this->additionalConfigKeys, true);
144 1
    }
145
}
146