Completed
Branch master (32eaad)
by Andy
02:52 queued 39s
created

SocialiteWasCalled::classExtends()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 2
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;
8
9
class SocialiteWasCalled
10
{
11
    /**
12
     * @var LaravelApp
13
     */
14
    protected $app;
15
16
    /**
17
     * @param LaravelApp $app
18
     */
19 10
    public function __construct(LaravelApp $app)
20
    {
21 10
        $this->app = $app;
22 10
    }
23
24
    /**
25
     * @param string $providerName 'meetup'
26
     * @param string $providerClass 'Your\Name\Space\ClassNameProvider' must extend
27
     *                              either Laravel\Socialite\Two\AbstractProvider or
28
     *                              Laravel\Socialite\One\AbstractProvider
29
     * @param string $oauth1Server 'Your\Name\Space\ClassNameServer' must extend League\OAuth1\Client\Server\Server
30
     *
31
     * @throws InvalidArgumentException
32
     */
33 10
    public function extendSocialite($providerName, $providerClass, $oauth1Server = null)
34
    {
35
        /** @var SocialiteManager $socialite */
36 10
        $socialite = $this->app->make(\Laravel\Socialite\Contracts\Factory::class);
37 10
        $provider = $this->buildProvider($socialite, $providerName, $providerClass, $oauth1Server);
38 4
        $socialite->extend(
39 4
            $providerName,
40 4
            function () use ($provider) {
41 4
                return $provider;
42
            }
43 4
        );
44 4
    }
45
46
    /**
47
     * @param SocialiteManager $socialite
48
     * @param                  $providerName
49
     * @param string $providerClass
50
     * @param null|string $oauth1Server
51
     *
52
     * @return \Laravel\Socialite\One\AbstractProvider|\Laravel\Socialite\Two\AbstractProvider
53
     */
54 10
    protected function buildProvider(SocialiteManager $socialite, $providerName, $providerClass, $oauth1Server)
55
    {
56 10
        $config = $this->getConfig($providerName);
57 5
        if ($this->isOAuth1($oauth1Server)) {
58 1
            return $this->buildOAuth1Provider($providerClass, $oauth1Server, $socialite->formatConfig($config));
59
        }
60
61 4
        return $this->buildOAuth2Provider($socialite, $providerClass, $config);
62
    }
63
64
    /**
65
     * Build an OAuth 1 provider instance.
66
     *
67
     * @param string $providerClass must extend Laravel\Socialite\One\AbstractProvider
68
     * @param string $oauth1Server must extend League\OAuth1\Client\Server\Server
69
     * @param array $config
70
     *
71
     * @return \Laravel\Socialite\One\AbstractProvider
72
     */
73 1
    protected function buildOAuth1Provider($providerClass, $oauth1Server, array $config)
74
    {
75 1
        $this->classExtends($providerClass, \Laravel\Socialite\One\AbstractProvider::class);
76 1
        $this->classExtends($oauth1Server, \League\OAuth1\Client\Server\Server::class);
77
78 1
        return new $providerClass(
79 1
            $this->app->offsetGet('request'), new $oauth1Server($config)
80 1
        );
81
    }
82
83
    /**
84
     * Build an OAuth 2 provider instance.
85
     *
86
     * @param SocialiteManager $socialite
87
     * @param string $providerClass must extend Laravel\Socialite\Two\AbstractProvider
88
     * @param array $config
89
     *
90
     * @return \Laravel\Socialite\Two\AbstractProvider
91
     */
92 4
    protected function buildOAuth2Provider(SocialiteManager $socialite, $providerClass, array $config)
93
    {
94 4
        $this->classExtends($providerClass, \Laravel\Socialite\Two\AbstractProvider::class);
95
96 3
        return $socialite->buildProvider($providerClass, $config);
97
    }
98
99
    /**
100
     * @param string $providerName
101
     *
102
     * @return array
103
     */
104 10
    protected function getConfig($providerName)
105
    {
106
        try {
107
            /** @var Contracts\ConfigInterface $config */
108 10
            $config = $this->app->make('SocialiteProviders.config.' . $providerName);
109
110 8
            if (!($config instanceof Contracts\ConfigInterface)) {
111 5
                throw new InvalidArgumentException('Config class does not implement config contract');
112
            }
113
114 3
            return $config->get();
115 7
        } catch (\ReflectionException $e) {
116 2
            return $this->app->offsetGet('config')['services.' . $providerName] ?: (new Config('foobar', 'foobar', 'foobar'))->get();
117
        }
118
    }
119
120
    /**
121
     * Check if a server is given, which indicates that OAuth1 is used.
122
     *
123
     * @param string $oauth1Server
124
     *
125
     * @return bool
126
     */
127 5
    private function isOAuth1($oauth1Server)
128
    {
129 5
        return (!empty($oauth1Server));
130
    }
131
132
    /**
133
     * @param string $class
134
     * @param string $baseClass
135
     *
136
     * @throws InvalidArgumentException
137
     */
138 5
    private function classExtends($class, $baseClass)
139
    {
140 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...
141 1
            $message = $class . ' does not extend ' . $baseClass;
142 1
            throw new InvalidArgumentException($message);
143
        }
144 4
    }
145
}
146