Completed
Push — master ( 2824b7...5b296f )
by Andy
03:36
created

SocialiteWasCalled::classExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
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
            return $this->buildOAuth1Provider($providerClass, $oauth1Server, $socialite->formatConfig($config));
73
        }
74
75 4
        $config = $this->getConfig($providerClass, $providerName);
76 3
        return $this->buildOAuth2Provider($socialite, $providerClass, $config);
77
    }
78
79
    /**
80
     * Build an OAuth 1 provider instance.
81
     *
82
     * @param string $providerClass must extend Laravel\Socialite\One\AbstractProvider
83
     * @param string $oauth1Server  must extend League\OAuth1\Client\Server\Server
84
     * @param array  $config
85
     *
86
     * @return \Laravel\Socialite\One\AbstractProvider
87
     */
88 2
    protected function buildOAuth1Provider($providerClass, $oauth1Server, array $config)
89
    {
90 2
        $this->classExtends($providerClass, \Laravel\Socialite\One\AbstractProvider::class);
91 1
        $this->classExtends($oauth1Server, \League\OAuth1\Client\Server\Server::class);
92
93 1
        return new $providerClass(
94 1
            $this->app->offsetGet('request'), new $oauth1Server($config)
95 1
        );
96
    }
97
98
    /**
99
     * Build an OAuth 2 provider instance.
100
     *
101
     * @param SocialiteManager $socialite
102
     * @param string           $providerClass must extend Laravel\Socialite\Two\AbstractProvider
103
     * @param array            $config
104
     *
105
     * @return \Laravel\Socialite\Two\AbstractProvider
106
     */
107 3
    protected function buildOAuth2Provider(SocialiteManager $socialite, $providerClass, array $config)
108
    {
109 3
        $this->classExtends($providerClass, \Laravel\Socialite\Two\AbstractProvider::class);
110
111 3
        return $socialite->buildProvider($providerClass, $config);
112
    }
113
114
    /**
115
     * @param string $providerClass
116
     * @param string $providerName
117
     *
118
     * @return array
119
     * @throws MissingConfigException
120
     */
121 6
    protected function getConfig($providerClass, $providerName)
122
    {
123 6
        $config = null;
124 6
        $additionalConfigKeys = $providerClass::additionalConfigKeys();
125 6
        $exceptionMessages = [];
126
        try {
127 6
            $config = $this->configRetriever->fromEnv($providerClass::IDENTIFIER, $additionalConfigKeys);
128 4
            return $config->get();
129 2
        } catch (MissingConfigException $e) {
130 2
            $exceptionMessages[] = $e->getMessage();
131
        }
132
133 2
        $config = null;
134
        try {
135 2
            $config = $this->configRetriever->fromServices($providerName, $additionalConfigKeys);
136 1
            return $config->get();
137 1
        } catch (MissingConfigException $e) {
138 1
            $exceptionMessages[] = $e->getMessage();
139
        }
140
141 1
        throw new MissingConfigException(implode(PHP_EOL, $exceptionMessages));
142
    }
143
144
    /**
145
     * Check if a server is given, which indicates that OAuth1 is used.
146
     *
147
     * @param string $oauth1Server
148
     *
149
     * @return bool
150
     */
151 7
    private function isOAuth1($oauth1Server)
152
    {
153 7
        return !empty($oauth1Server);
154
    }
155
156
    /**
157
     * @param string $class
158
     * @param string $baseClass
159
     *
160
     * @throws InvalidArgumentException
161
     */
162 5
    private function classExtends($class, $baseClass)
163
    {
164 5
        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...
165 1
            $message = $class.' does not extend '.$baseClass;
166 1
            throw new InvalidArgumentException($message);
167
        }
168 4
    }
169
170 10
    private function classExists($providerClass)
171
    {
172 10
        if (!class_exists($providerClass)) {
173 4
            throw new InvalidArgumentException("$providerClass doesn't exist");
174
        }
175 7
    }
176
}
177