Completed
Push — master ( 6ff50e...4cc05a )
by Brian
07:59
created

src/Helpers/ConfigRetriever.php (2 issues)

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 SocialiteProviders\Manager\Config;
6
use SocialiteProviders\Manager\SocialiteWasCalled;
7
use SocialiteProviders\Manager\Contracts\ConfigInterface;
8
use SocialiteProviders\Manager\Exception\MissingConfigException;
9
use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface;
10
11
class ConfigRetriever implements ConfigRetrieverInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $providerName;
17
18
    /**
19
     * @var string
20
     */
21
    protected $providerIdentifier;
22
23
    /**
24
     * @var array
25
     */
26
    protected $servicesArray;
27
28
    /**
29
     * @var array
30
     */
31
    protected $additionalConfigKeys;
32
33 2
    /**
34
     * @param string $providerIdentifier
35 2
     * @param array  $additionalConfigKeys
36
     *
37 2
     * @throws MissingConfigException
38 2
     *
39 1
     * @return ConfigInterface
40 1
     */
41
    public function fromEnv($providerIdentifier, array $additionalConfigKeys = [])
42 1
    {
43 1
        $this->providerIdentifier = $providerIdentifier;
44
        $this->additionalConfigKeys = $additionalConfigKeys;
45
46
        return new Config(
47
            $this->getFromEnv('KEY'),
48
            $this->getFromEnv('SECRET'),
49
            $this->getFromEnv('REDIRECT_URI'),
50
            $this->getConfigItems($additionalConfigKeys, function ($key) {
51
                return $this->getFromEnv(strtoupper($key));
52
            }));
53 2
    }
54
55 2
    /**
56 2
     * @param string $providerName
57
     * @param array  $additionalConfigKeys
58 1
     *
59 1
     * @throws MissingConfigException
60 1
     *
61 1
     * @return ConfigInterface
62 1
     */
63 1
    public function fromServices($providerName, array $additionalConfigKeys = [])
64 1
    {
65
        $this->providerName = $providerName;
66
        $this->getConfigFromServicesArray($providerName);
67
68
        $this->additionalConfigKeys = $additionalConfigKeys;
69
70
        return new Config(
71
            $this->getFromServices('client_id'),
72
            $this->getFromServices('client_secret'),
73 2
            $this->getFromServices('redirect'),
74
            $this->getConfigItems($additionalConfigKeys, function ($key) {
75 2
                return $this->getFromServices(strtolower($key));
76
            }));
77
    }
78
79 2
    /**
80
     * @param array    $configKeys
81
     * @param \Closure $keyRetrievalClosure
82
     *
83
     * @return array
84
     */
85
    protected function getConfigItems(array $configKeys, \Closure $keyRetrievalClosure)
86
    {
87
        if (count($configKeys) < 1) {
88 2
            return [];
89
        }
90 2
91
        return $this->retrieveItemsFromConfig($configKeys, $keyRetrievalClosure);
92 2
    }
93 2
94 2
    /**
95
     * @param array    $keys
96 2
     * @param \Closure $keyRetrievalClosure
97
     *
98
     * @return array
99
     */
100
    protected function retrieveItemsFromConfig(array $keys, \Closure $keyRetrievalClosure)
101
    {
102
        $out = [];
103
104
        foreach ($keys as $key) {
105 1
            $out[$key] = $keyRetrievalClosure($key);
106
        }
107 1
108
        return $out;
109
    }
110
111 1
    /**
112
     * @param string $key
113
     *
114
     * @throws MissingConfigException
115
     *
116
     * @return string
117
     */
118
    protected function getFromServices($key)
119
    {
120 2
        $keyExists = array_key_exists($key, $this->servicesArray);
121
122 2
        // ADDITIONAL value is empty
123 2
        if (! $keyExists && $this->isAdditionalConfig($key)) {
124
            return;
125 2
        }
126 1
127
        // REQUIRED value is empty
128
        if (! $keyExists) {
129 1
            throw new MissingConfigException("Missing services entry for {$this->providerName}.$key");
130
        }
131
132
        return $this->servicesArray[$key];
133
    }
134
135
    /**
136
     * @param string $key
137
     *
138 2
     * @throws MissingConfigException
139
     *
140 2
     * @return string
141 2
     */
142
    protected function getFromEnv($key)
143 2
    {
144 1
        $providerKey = "{$this->providerIdentifier}_{$key}";
145
        $item = env($providerKey);
146 1
147
        // ADDITIONAL value is empty
148 1
        if (empty($item) && $this->isAdditionalConfig($key)) {
149
            return;
150
        }
151
152
        // REQUIRED value is empty
153
        if (empty($item)) {
154
            // If we are running in console we should spoof values to make Socialite happy...
155
            if (! app()->runningInConsole()) {
156
                throw new MissingConfigException("Configuration for $providerKey is missing.");
157
            }
158
            $item = $providerKey;
159
160
            SocialiteWasCalled::$spoofedConfig = true;
0 ignored issues
show
The property spoofedConfig cannot be accessed from this context as it is declared private in class SocialiteProviders\Manager\SocialiteWasCalled.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
161
        }
162
163
        return $item;
164
    }
165
166
    /**
167
     * @param string $providerName
168
     *
169
     * @throws MissingConfigException
170
     *
171
     * @return array
172
     */
173
    protected function getConfigFromServicesArray($providerName)
174
    {
175
        /** @var array $configArray */
176
        $configArray = config("services.$providerName");
177
178
        if (empty($configArray)) {
179
            // If we are running in console we should spoof values to make Socialite happy...
180
            if (app()->runningInConsole()) {
181
                $configArray = [
182
                    'client_id' => "{$this->providerIdentifier}_KEY",
183
                    'client_secret' => "{$this->providerIdentifier}_SECRET",
184
                    'redirect' => "{$this->providerIdentifier}_REDIRECT_URI",
185
                ];
186
187
                SocialiteWasCalled::$spoofedConfig = true;
0 ignored issues
show
The property spoofedConfig cannot be accessed from this context as it is declared private in class SocialiteProviders\Manager\SocialiteWasCalled.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
188
            } else {
189
                throw new MissingConfigException("There is no services entry for $providerName");
190
            }
191
        }
192
193
        $this->servicesArray = $configArray;
194
195
        return $this->servicesArray;
196
    }
197
198
    /**
199
     * @param string $key
200
     *
201
     * @return bool
202
     */
203
    protected function isAdditionalConfig($key)
204
    {
205
        return in_array(strtolower($key), $this->additionalConfigKeys);
206
    }
207
}
208