Completed
Push — master ( 762452...1b306d )
by Дмитрий
04:05
created

CollectionFactory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 5
cp 0.6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2.2559
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
        // OAuth2
29
        'facebook'      => OAuth2\Provider\Facebook::class,
30
        'github'        => OAuth2\Provider\GitHub::class,
31
        'instagram'     => OAuth2\Provider\Instagram::class,
32
        'google'        => OAuth2\Provider\Google::class,
33
        'vk'            => OAuth2\Provider\Vk::class,
34
        'slack'         => OAuth2\Provider\Slack::class,
35
        'twitch'        => OAuth2\Provider\Twitch::class,
36
        'bitbucket'     => OAuth2\Provider\Bitbucket::class,
37
        'amazon'        => OAuth2\Provider\Amazon::class,
38
        'gitlab'        => OAuth2\Provider\GitLab::class,
39
        'vimeo'         => OAuth2\Provider\Vimeo::class,
40
        'digital-ocean' => OAuth2\Provider\DigitalOcean::class,
41
        'yandex'        => OAuth2\Provider\Yandex::class,
42
        'mail-ru'       => OAuth2\Provider\MailRu::class,
43
        'odnoklassniki' => OAuth2\Provider\Odnoklassniki::class,
44
        // OpenID
45
        'steam'         => OpenID\Provider\Steam::class,
46
    ];
47
48
    /**
49
     * @param array $providers
50
     */
51 3
    public function __construct(array $providers = null)
52
    {
53 3
        if ($providers) {
54
            $this->providers = $providers;
55
        }
56 3
    }
57
58
    /**
59
     * @param $id
60
     * @return bool
61
     */
62
    public function has($id)
63
    {
64
        return isset($this->providers[$id]);
65
    }
66
67
    /**
68
     * @param string $id
69
     * @param array $parameters
70
     * @param Service $service
71
     * @return \SocialConnect\Provider\AbstractBaseProvider
72
     */
73 2
    public function factory($id, array $parameters, Service $service)
74
    {
75 2
        $consumer = new Consumer($parameters['applicationId'], $parameters['applicationSecret']);
76
77 2
        if (isset($parameters['applicationPublic'])) {
78
            $consumer->setPublic($parameters['applicationPublic']);
79
        }
80
81 2
        $id = strtolower($id);
82
83 2
        if (!isset($this->providers[$id])) {
84
            throw new LogicException('Provider with $id = ' . $id . ' doest not exist');
85
        }
86
87 2
        $providerClassName = $this->providers[$id];
88
89
        /**
90
         * @var $provider \SocialConnect\Provider\AbstractBaseProvider
91
         */
92 2
        $provider = new $providerClassName(
93 2
            $service->getHttpClient(),
94 2
            $consumer,
95 2
            array_merge(
96 2
                $parameters,
97 2
                $service->getConfig()
98 2
            )
99 2
        );
100
101 2
        return $provider;
102
    }
103
}
104