Service::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 2
nc 2
nop 4
crap 2
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
declare(strict_types=1);
7
8
namespace SocialConnect\Auth;
9
10
use SocialConnect\Provider\Exception\InvalidProviderConfiguration;
11
use SocialConnect\Common\HttpStack;
12
use SocialConnect\Provider\Session\SessionInterface;
13
14
class Service
15
{
16
    /**
17
     * @var HttpStack
18
     */
19
    protected $httpStack;
20
21
    /**
22
     * @var FactoryInterface
23
     */
24
    protected $factory;
25
26
    /**
27
     * @var array
28
     */
29
    protected $config;
30
31
    /**
32
     * @var SessionInterface
33
     */
34
    protected $session;
35
36
    /**
37
     * @param HttpStack $httpStack
38
     * @param SessionInterface $session
39
     * @param array $config
40
     * @param FactoryInterface|null $factory
41
     * @internal param $storage
42
     */
43 3
    public function __construct(HttpStack $httpStack, SessionInterface $session, array $config, FactoryInterface $factory = null)
44
    {
45 3
        $this->httpStack = $httpStack;
46 3
        $this->session = $session;
47 3
        $this->config = $config;
48
49 3
        $this->factory = is_null($factory) ? new CollectionFactory() : $factory;
50 3
    }
51
52
    /**
53
     * @param string $name
54
     * @return array
55
     * @throws InvalidProviderConfiguration
56
     */
57 1
    public function getProviderConfiguration(string $name)
58
    {
59 1
        if (isset($this->config['provider'][$name])) {
60 1
            return $this->config['provider'][$name];
61
        }
62
63
        throw new InvalidProviderConfiguration(
64
            'Please setup configuration for ' . ucfirst($name) . ' provider'
65
        );
66
    }
67
68
    /**
69
     * Check that provider exists by $name
70
     *
71
     * @param string $name
72
     * @return bool
73
     */
74
    public function has(string $name)
75
    {
76
        return $this->factory->has($name);
77
    }
78
79
    /**
80
     * Get provider class by $name
81
     *
82
     * @param string $name
83
     * @return \SocialConnect\Provider\AbstractBaseProvider
84
     * @throws InvalidProviderConfiguration
85
     */
86 1
    public function getProvider(string $name)
87
    {
88 1
        return $this->factory->factory($name, $this->getProviderConfiguration($name), $this);
89
    }
90
91
    /**
92
     * @return array
93
     */
94 2
    public function getConfig()
95
    {
96 2
        return $this->config;
97
    }
98
99
    /**
100
     * @return SessionInterface
101
     */
102 2
    public function getSession()
103
    {
104 2
        return $this->session;
105
    }
106
107
    /**
108
     * @return HttpStack
109
     */
110 2
    public function getHttpStack(): HttpStack
111
    {
112 2
        return $this->httpStack;
113
    }
114
}
115