SocialiteManager::getDefaultDriver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php namespace Arcanedev\Socialite;
2
3
use Arcanedev\Socialite\OAuth\One as OAuthOne;
4
use Arcanedev\Socialite\OAuth\Two as OAuthTwo;
5
use Illuminate\Support\Manager;
6
use League\OAuth1\Client\Server\Bitbucket as BitbucketServer;
7
use League\OAuth1\Client\Server\Twitter as TwitterServer;
8
9
/**
10
 * Class     SocialiteManager
11
 *
12
 * @package  Arcanedev\Socialite
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class SocialiteManager extends Manager implements Contracts\Factory
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Main Functions
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /**
22
     * Get a driver instance.
23
     *
24
     * @param  string  $driver
25
     *
26
     * @return mixed
27
     */
28
    public function with($driver)
29
    {
30
        return $this->driver($driver);
31
    }
32
33
    /**
34
     * Get the default driver name.
35
     *
36
     * @return string
37
     */
38
    public function getDefaultDriver()
39
    {
40
        throw new Exceptions\UnspecifiedDriverException(
41
            'No Socialite driver was specified.'
42
        );
43
    }
44
45
    /* ------------------------------------------------------------------------------------------------
46
     |  OAuth V1 Drivers
47
     | ------------------------------------------------------------------------------------------------
48
     */
49
    /**
50
     * Create an instance of the specified driver.
51
     *
52
     * @return \Arcanedev\Socialite\OAuth\One\AbstractProvider
53
     */
54
    protected function createTwitterDriver()
55
    {
56
        $config = $this->config()->get('services.twitter');
57
58
        return new OAuthOne\TwitterProvider(
59
            $this->app['request'],
60
            new TwitterServer($this->formatConfig($config))
61
        );
62
    }
63
64
    /**
65
     * Create an instance of the specified driver.
66
     *
67
     * @return \Arcanedev\Socialite\OAuth\One\AbstractProvider
68
     */
69
    protected function createBitbucketDriver()
70
    {
71
        $config = $this->config()->get('services.bitbucket');
72
73
        return new OAuthOne\BitbucketProvider(
74
            $this->app['request'],
75
            new BitbucketServer($this->formatConfig($config))
76
        );
77
    }
78
79
    /**
80
     * Format the server configuration.
81
     *
82
     * @param  array  $config
83
     *
84
     * @return array
85
     */
86
    protected function formatConfig(array $config)
87
    {
88
        return array_merge([
89
            'identifier'   => $config['client_id'],
90
            'secret'       => $config['client_secret'],
91
            'callback_uri' => $config['redirect'],
92
        ], $config);
93
    }
94
95
    /* ------------------------------------------------------------------------------------------------
96
     |  OAuth V2 Drivers
97
     | ------------------------------------------------------------------------------------------------
98
     */
99
    /**
100
     * Create an instance of the specified driver.
101
     *
102
     * @return \Arcanedev\Socialite\OAuth\Two\AbstractProvider
103
     */
104
    protected function createGithubDriver()
105
    {
106
        return $this->buildProvider(
107
            OAuthTwo\GithubProvider::class,
108
            $this->config()->get('services.github')
109
        );
110
    }
111
112
    /**
113
     * Create an instance of the specified driver.
114
     *
115
     * @return \Arcanedev\Socialite\OAuth\Two\AbstractProvider
116
     */
117
    protected function createFacebookDriver()
118
    {
119
        return $this->buildProvider(
120
            OAuthTwo\FacebookProvider::class,
121
            $this->config()->get('services.facebook')
122
        );
123
    }
124
    /**
125
     * Create an instance of the specified driver.
126
     *
127
     * @return \Arcanedev\Socialite\OAuth\Two\AbstractProvider
128
     */
129
    protected function createGoogleDriver()
130
    {
131
        return $this->buildProvider(
132
            OAuthTwo\GoogleProvider::class,
133
            $this->config()->get('services.google')
134
        );
135
    }
136
    /**
137
     * Create an instance of the specified driver.
138
     *
139
     * @return \Arcanedev\Socialite\OAuth\Two\AbstractProvider
140
     */
141
    protected function createLinkedinDriver()
142
    {
143
        return $this->buildProvider(
144
            OAuthTwo\LinkedInProvider::class,
145
            $this->config()->get('services.linkedin')
146
        );
147
    }
148
149
    /**
150
     * Build an OAuth 2 provider instance.
151
     *
152
     * @param  string  $provider
153
     * @param  array   $config
154
     *
155
     * @return \Arcanedev\Socialite\OAuth\Two\AbstractProvider
156
     */
157
    protected function buildProvider($provider, $config)
158
    {
159
        return new $provider(
160
            $this->app['request'],
161
            $config['client_id'],
162
            $config['client_secret'],
163
            $config['redirect']
164
        );
165
    }
166
167
    /* ------------------------------------------------------------------------------------------------
168
     |  Other Functions
169
     | ------------------------------------------------------------------------------------------------
170
     */
171
    /**
172
     * Get the config repository.
173
     *
174
     * @return \Illuminate\Contracts\Config\Repository
175
     */
176
    protected function config()
177
    {
178
        return $this->app['config'];
179
    }
180
}
181