CollectionFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 68%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 111
ccs 17
cts 25
cp 0.68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A has() 0 4 1
A factory() 0 31 3
A register() 0 4 1
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
use SocialConnect\OpenIDConnect;
15
16
/**
17
 * Class Factory
18
 * @package SocialConnect\Auth\Provider
19
 */
20
class CollectionFactory implements FactoryInterface
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $providers = [
26
        // OAuth1
27
        OAuth1\Provider\Twitter::NAME       => OAuth1\Provider\Twitter::class,
28
        OAuth1\Provider\Px500::NAME         => OAuth1\Provider\Px500::class,
29
        OAuth1\Provider\Tumblr::NAME        => OAuth1\Provider\Tumblr::class,
30
        OAuth1\Provider\Atlassian::NAME     => OAuth1\Provider\Atlassian::class,
31
        // OAuth2
32
        OAuth2\Provider\Facebook::NAME      => OAuth2\Provider\Facebook::class,
33
        OAuth2\Provider\Google::NAME        => OAuth2\Provider\Google::class,
34
        OAuth2\Provider\GitHub::NAME        => OAuth2\Provider\GitHub::class,
35
        OAuth2\Provider\Instagram::NAME     => OAuth2\Provider\Instagram::class,
36
        OAuth2\Provider\Vk::NAME            => OAuth2\Provider\Vk::class,
37
        OAuth2\Provider\SmashCast::NAME     => OAuth2\Provider\SmashCast::class,
38
        OAuth2\Provider\Slack::NAME         => OAuth2\Provider\Slack::class,
39
        OAuth2\Provider\Steein::NAME        => OAuth2\Provider\Steein::class,
40
        OAuth2\Provider\Twitch::NAME        => OAuth2\Provider\Twitch::class,
41
        OAuth2\Provider\Bitbucket::NAME     => OAuth2\Provider\Bitbucket::class,
42
        OAuth2\Provider\Amazon::NAME        => OAuth2\Provider\Amazon::class,
43
        OAuth2\Provider\GitLab::NAME        => OAuth2\Provider\GitLab::class,
44
        OAuth2\Provider\Vimeo::NAME         => OAuth2\Provider\Vimeo::class,
45
        OAuth2\Provider\DigitalOcean::NAME  => OAuth2\Provider\DigitalOcean::class,
46
        OAuth2\Provider\Yandex::NAME        => OAuth2\Provider\Yandex::class,
47
        OAuth2\Provider\MailRu::NAME        => OAuth2\Provider\MailRu::class,
48
        OAuth2\Provider\Microsoft::NAME     => OAuth2\Provider\Microsoft::class,
49
        OAuth2\Provider\Odnoklassniki::NAME => OAuth2\Provider\Odnoklassniki::class,
50
        OAuth2\Provider\Discord::NAME       => OAuth2\Provider\Discord::class,
51
        OAuth2\Provider\Reddit::NAME        => OAuth2\Provider\Reddit::class,
52
        OAuth2\Provider\LinkedIn::NAME      => OAuth2\Provider\LinkedIn::class,
53
        OAuth2\Provider\Yahoo::NAME         => OAuth2\Provider\Yahoo::class,
54
        OAuth2\Provider\WordPress::NAME     => OAuth2\Provider\WordPress::class,
55
        OAuth2\Provider\Meetup::NAME        => OAuth2\Provider\Meetup::class,
56
        // OpenID
57
        OpenID\Provider\Steam::NAME         => OpenID\Provider\Steam::class,
58
        // OpenIDConnect
59
        //OpenIDConnect\Provider\Google::NAME        => OpenIDConnect\Provider\Google::class,
60
        OpenIDConnect\Provider\PixelPin::NAME      => OpenIDConnect\Provider\PixelPin::class,
61
    ];
62
63
    /**
64
     * @param array $providers
65
     */
66 3
    public function __construct(array $providers = null)
67
    {
68 3
        if ($providers) {
69
            $this->providers = $providers;
70
        }
71 3
    }
72
73
    /**
74
     * @param $id
75
     * @return bool
76
     */
77
    public function has($id)
78
    {
79
        return isset($this->providers[$id]);
80
    }
81
82
    /**
83
     * @param string $id
84
     * @param array $parameters
85
     * @param Service $service
86
     * @return \SocialConnect\Provider\AbstractBaseProvider
87
     */
88 2
    public function factory($id, array $parameters, Service $service)
89
    {
90 2
        $consumer = new Consumer($parameters['applicationId'], $parameters['applicationSecret']);
91
92 2
        if (isset($parameters['applicationPublic'])) {
93
            $consumer->setPublic($parameters['applicationPublic']);
94
        }
95
96 2
        $id = strtolower($id);
97
98 2
        if (!isset($this->providers[$id])) {
99
            throw new LogicException('Provider with $id = ' . $id . ' doest not exist');
100
        }
101
102 2
        $providerClassName = $this->providers[$id];
103
104
        /**
105
         * @var $provider \SocialConnect\Provider\AbstractBaseProvider
106
         */
107 2
        $provider = new $providerClassName(
108 2
            $service->getHttpClient(),
109 2
            $service->getSession(),
110 2
            $consumer,
111 2
            array_merge(
112 2
                $parameters,
113 2
                $service->getConfig()
114
            )
115
        );
116
117 2
        return $provider;
118
    }
119
120
    /**
121
     * Register new provider to Provider's collection
122
     *
123
     * @param $providerName
124
     * @param $providerClass
125
     */
126
    public function register($providerName, $providerClass)
127
    {
128
        $this->providers[$providerName] = $providerClass;
129
    }
130
}
131