Completed
Push — master ( 046422...8eb4ac )
by ARCANEDEV
08:13 queued 14s
created

SocialiteManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 166
ccs 0
cts 75
cp 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A with() 0 4 1
A getDefaultDriver() 0 6 1
A createTwitterDriver() 0 9 1
A createBitbucketDriver() 0 9 1
A createGithubDriver() 0 7 1
A createFacebookDriver() 0 7 1
A createGoogleDriver() 0 7 1
A createLinkedinDriver() 0 7 1
A buildProvider() 0 9 1
A config() 0 4 1
A formatConfig() 0 8 1
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\Base\OAuthOneProvider
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\Base\OAuthOneProvider
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
     |  OAuth V2 Drivers
81
     | ------------------------------------------------------------------------------------------------
82
     */
83
    /**
84
     * Create an instance of the specified driver.
85
     *
86
     * @return \Arcanedev\Socialite\Base\OAuthTwoProvider
87
     */
88
    protected function createGithubDriver()
89
    {
90
        return $this->buildProvider(
91
            OAuthTwo\GithubProvider::class,
92
            $this->config()->get('services.github')
93
        );
94
    }
95
96
    /**
97
     * Create an instance of the specified driver.
98
     *
99
     * @return \Arcanedev\Socialite\Base\OAuthTwoProvider
100
     */
101
    protected function createFacebookDriver()
102
    {
103
        return $this->buildProvider(
104
            OAuthTwo\FacebookProvider::class,
105
            $this->config()->get('services.facebook')
106
        );
107
    }
108
    /**
109
     * Create an instance of the specified driver.
110
     *
111
     * @return \Arcanedev\Socialite\Base\OAuthTwoProvider
112
     */
113
    protected function createGoogleDriver()
114
    {
115
        return $this->buildProvider(
116
            OAuthTwo\GoogleProvider::class,
117
            $this->config()->get('services.google')
118
        );
119
    }
120
    /**
121
     * Create an instance of the specified driver.
122
     *
123
     * @return \Arcanedev\Socialite\Base\OAuthTwoProvider
124
     */
125
    protected function createLinkedinDriver()
126
    {
127
        return $this->buildProvider(
128
            OAuthTwo\LinkedInProvider::class,
129
            $this->config()->get('services.linkedin')
130
        );
131
    }
132
133
    /**
134
     * Build an OAuth 2 provider instance.
135
     *
136
     * @param  string  $provider
137
     * @param  array   $config
138
     *
139
     * @return \Arcanedev\Socialite\Base\OAuthTwoProvider
140
     */
141
    public function buildProvider($provider, $config)
142
    {
143
        return new $provider(
144
            $this->app['request'],
145
            $config['client_id'],
146
            $config['client_secret'],
147
            $config['redirect']
148
        );
149
    }
150
151
    /* ------------------------------------------------------------------------------------------------
152
     |  Other Functions
153
     | ------------------------------------------------------------------------------------------------
154
     */
155
    /**
156
     * Get the config repository.
157
     *
158
     * @return \Illuminate\Contracts\Config\Repository
159
     */
160
    protected function config()
161
    {
162
        return $this->app['config'];
163
    }
164
165
    /**
166
     * Format the server configuration.
167
     *
168
     * @param  array  $config
169
     *
170
     * @return array
171
     */
172
    protected function formatConfig(array $config)
173
    {
174
        return array_merge([
175
            'identifier'   => $config['client_id'],
176
            'secret'       => $config['client_secret'],
177
            'callback_uri' => $config['redirect'],
178
        ], $config);
179
    }
180
}
181