Completed
Pull Request — master (#38)
by Brian
25:16 queued 15:22
created

SocialiteWasCalled::classExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace SocialiteProviders\Manager;
4
5
use Illuminate\Contracts\Foundation\Application as LaravelApp;
6
use Laravel\Socialite\SocialiteManager;
7
use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface;
8
use SocialiteProviders\Manager\Exception\InvalidArgumentException;
9
use SocialiteProviders\Manager\Exception\MissingConfigException;
10
11
class SocialiteWasCalled
12
{
13
    const SERVICE_CONTAINER_PREFIX = 'SocialiteProviders.config.';
14
15
    /**
16
     * @var LaravelApp
17
     */
18
    protected $app;
19
20
    /**
21
     * @var ConfigRetrieverInterface
22
     */
23
    private $configRetriever;
24
25
    /**
26
     * @param LaravelApp               $app
27
     * @param ConfigRetrieverInterface $configRetriever
28
     */
29 10
    public function __construct(LaravelApp $app, ConfigRetrieverInterface $configRetriever)
30
    {
31 10
        $this->app = $app;
32 10
        $this->configRetriever = $configRetriever;
33 10
    }
34
35
    /**
36
     * @param string $providerName  'meetup'
37
     * @param string $providerClass 'Your\Name\Space\ClassNameProvider' must extend
38
     *                              either Laravel\Socialite\Two\AbstractProvider or
39
     *                              Laravel\Socialite\One\AbstractProvider
40
     * @param string $oauth1Server  'Your\Name\Space\ClassNameServer' must extend League\OAuth1\Client\Server\Server
41
     *
42
     * @throws InvalidArgumentException
43
     */
44 10
    public function extendSocialite($providerName, $providerClass, $oauth1Server = null)
45
    {
46
        /** @var SocialiteManager $socialite */
47 10
        $socialite = $this->app->make(\Laravel\Socialite\Contracts\Factory::class);
48 10
        $provider = $this->buildProvider($socialite, $providerName, $providerClass, $oauth1Server);
49 4
        $socialite->extend(
50 4
            $providerName,
51 4
            function () use ($provider) {
52 4
                return $provider;
53
            }
54 4
        );
55 4
    }
56
57
    /**
58
     * @param SocialiteManager $socialite
59
     * @param                  $providerName
60
     * @param string           $providerClass
61
     * @param null|string      $oauth1Server
62
     *
63
     * @return \Laravel\Socialite\One\AbstractProvider|\Laravel\Socialite\Two\AbstractProvider
64
     */
65 10
    protected function buildProvider(SocialiteManager $socialite, $providerName, $providerClass, $oauth1Server)
66
    {
67 10
        $this->classExists($providerClass);
68
69 7
        if ($this->isOAuth1($oauth1Server)) {
70 3
            $this->classExists($oauth1Server);
71 2
            $config = $this->getConfig($providerClass, $providerName);
72 2
73
            return $this->buildOAuth1Provider($providerClass, $oauth1Server, $socialite->formatConfig($config));
74
        }
75 4
76 3
        $config = $this->getConfig($providerClass, $providerName);
77
78
        return $this->buildOAuth2Provider($socialite, $providerClass, $config);
79
    }
80
81
    /**
82
     * Build an OAuth 1 provider instance.
83
     *
84
     * @param string $providerClass must extend Laravel\Socialite\One\AbstractProvider
85
     * @param string $oauth1Server  must extend League\OAuth1\Client\Server\Server
86
     * @param array  $config
87
     *
88 2
     * @return \Laravel\Socialite\One\AbstractProvider
89
     */
90 2
    protected function buildOAuth1Provider($providerClass, $oauth1Server, array $config)
91 1
    {
92
        $this->classExtends($providerClass, \Laravel\Socialite\One\AbstractProvider::class);
93 1
        $this->classExtends($oauth1Server, \League\OAuth1\Client\Server\Server::class);
94 1
95 1
        return new $providerClass(
96
            $this->app->offsetGet('request'), new $oauth1Server($config)
0 ignored issues
show
Bug introduced by
The method offsetGet() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
        );
98
    }
99
100
    /**
101
     * Build an OAuth 2 provider instance.
102
     *
103
     * @param SocialiteManager $socialite
104
     * @param string           $providerClass must extend Laravel\Socialite\Two\AbstractProvider
105
     * @param array            $config
106
     *
107 3
     * @return \Laravel\Socialite\Two\AbstractProvider
108
     */
109 3
    protected function buildOAuth2Provider(SocialiteManager $socialite, $providerClass, array $config)
110
    {
111 3
        $this->classExtends($providerClass, \Laravel\Socialite\Two\AbstractProvider::class);
112
113
        return $socialite->buildProvider($providerClass, $config);
114
    }
115
116
    /**
117
     * @param string $providerClass
118
     * @param string $providerName
119
     *
120
     * @return array
121 6
     *
122
     * @throws MissingConfigException
123 6
     */
124 6
    protected function getConfig($providerClass, $providerName)
125 6
    {
126
        $config = null;
127 6
        $additionalConfigKeys = $providerClass::additionalConfigKeys();
128 4
        $exceptionMessages = [];
129 2
        try {
130 2
            $config = $this->configRetriever->fromEnv($providerClass::IDENTIFIER, $additionalConfigKeys);
131
132
            return $config->get();
133 2
        } catch (MissingConfigException $e) {
134
            $exceptionMessages[] = $e->getMessage();
135 2
        }
136 1
137 1
        $config = null;
138 1
        try {
139
            $config = $this->configRetriever->fromServices($providerName, $additionalConfigKeys);
140
141 1
            return $config->get();
142
        } catch (MissingConfigException $e) {
143
            $exceptionMessages[] = $e->getMessage();
144
        }
145
146
        throw new MissingConfigException(implode(PHP_EOL, $exceptionMessages));
147
    }
148
149
    /**
150
     * Check if a server is given, which indicates that OAuth1 is used.
151 7
     *
152
     * @param string $oauth1Server
153 7
     *
154
     * @return bool
155
     */
156
    private function isOAuth1($oauth1Server)
157
    {
158
        return !empty($oauth1Server);
159
    }
160
161
    /**
162 5
     * @param string $class
163
     * @param string $baseClass
164 5
     *
165 1
     * @throws InvalidArgumentException
166 1
     */
167
    private function classExtends($class, $baseClass)
168 4
    {
169
        if (false === is_subclass_of($class, $baseClass)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $baseClass can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
170 10
            $message = $class.' does not extend '.$baseClass;
171
            throw new InvalidArgumentException($message);
172 10
        }
173 4
    }
174
175 7
    private function classExists($providerClass)
176
    {
177
        if (!class_exists($providerClass)) {
178
            throw new InvalidArgumentException("$providerClass doesn't exist");
179
        }
180
    }
181
}
182