Completed
Push — master ( 20a8a3...23113a )
by Дмитрий
03:35
created

CollectionFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 73.08%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 87
ccs 19
cts 26
cp 0.7308
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A has() 0 4 1
B factory() 0 31 3
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\Auth;
8
9
use LogicException;
10
use SocialConnect\Provider\Consumer;
11
use SocialConnect\OAuth1;
12
use SocialConnect\OAuth2;
13
use SocialConnect\OpenID;
14
15
/**
16
 * Class Factory
17
 * @package SocialConnect\Auth\Provider
18
 */
19
class CollectionFactory implements FactoryInterface
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $providers = [
25
        // OAuth1
26
        'twitter'       => OAuth1\Provider\Twitter::class,
27
        'px500'         => OAuth1\Provider\Px500::class,
28
        'tumblr'        => OAuth1\Provider\Tumblr::class,
29
        // OAuth2
30
        'facebook'      => OAuth2\Provider\Facebook::class,
31
        'github'        => OAuth2\Provider\GitHub::class,
32
        'instagram'     => OAuth2\Provider\Instagram::class,
33
        'google'        => OAuth2\Provider\Google::class,
34
        'vk'            => OAuth2\Provider\Vk::class,
35
        'slack'         => OAuth2\Provider\Slack::class,
36
        'twitch'        => OAuth2\Provider\Twitch::class,
37
        'bitbucket'     => OAuth2\Provider\Bitbucket::class,
38
        'amazon'        => OAuth2\Provider\Amazon::class,
39
        'gitlab'        => OAuth2\Provider\GitLab::class,
40
        'vimeo'         => OAuth2\Provider\Vimeo::class,
41
        'digital-ocean' => OAuth2\Provider\DigitalOcean::class,
42
        'yandex'        => OAuth2\Provider\Yandex::class,
43
        'mail-ru'       => OAuth2\Provider\MailRu::class,
44
        'odnoklassniki' => OAuth2\Provider\Odnoklassniki::class,
45
        // OpenID
46
        'steam'         => OpenID\Provider\Steam::class,
47
    ];
48
49
    /**
50
     * @param array $providers
51
     */
52 3
    public function __construct(array $providers = null)
53
    {
54 3
        if ($providers) {
55
            $this->providers = $providers;
56
        }
57 3
    }
58
59
    /**
60
     * @param $id
61
     * @return bool
62
     */
63
    public function has($id)
64
    {
65
        return isset($this->providers[$id]);
66
    }
67
68
    /**
69
     * @param string $id
70
     * @param array $parameters
71
     * @param Service $service
72
     * @return \SocialConnect\Provider\AbstractBaseProvider
73
     */
74 2
    public function factory($id, array $parameters, Service $service)
75
    {
76 2
        $consumer = new Consumer($parameters['applicationId'], $parameters['applicationSecret']);
77
78 2
        if (isset($parameters['applicationPublic'])) {
79
            $consumer->setPublic($parameters['applicationPublic']);
80
        }
81
82 2
        $id = strtolower($id);
83
84 2
        if (!isset($this->providers[$id])) {
85
            throw new LogicException('Provider with $id = ' . $id . ' doest not exist');
86
        }
87
88 2
        $providerClassName = $this->providers[$id];
89
90
        /**
91
         * @var $provider \SocialConnect\Provider\AbstractBaseProvider
92
         */
93 2
        $provider = new $providerClassName(
94 2
            $service->getHttpClient(),
95 2
            $service->getSession(),
96 2
            $consumer,
97 2
            array_merge(
98 2
                $parameters,
99 2
                $service->getConfig()
100 2
            )
101 2
        );
102
103 2
        return $provider;
104
    }
105
}
106