CollectionFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 70.83%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 52
c 3
b 0
f 1
dl 0
loc 110
ccs 17
cts 24
cp 0.7083
rs 10
wmc 7

4 Methods

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