BaseClientFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 27
c 1
b 0
f 0
dl 0
loc 81
ccs 21
cts 23
cp 0.913
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 3
A storage() 0 3 1
A idTokenParser() 0 3 1
A create() 0 11 3
1
<?php
2
3
namespace Parroauth2\Client\Factory;
4
5
use Parroauth2\Client\Client;
6
use Parroauth2\Client\ClientConfig;
7
use Parroauth2\Client\ClientInterface;
8
use Parroauth2\Client\EndPoint\Authorization\AuthorizationEndPoint;
9
use Parroauth2\Client\EndPoint\Introspection\IntrospectionEndPoint;
10
use Parroauth2\Client\EndPoint\Token\RevocationEndPoint;
11
use Parroauth2\Client\EndPoint\Token\TokenEndPoint;
12
use Parroauth2\Client\OpenID\EndPoint\AuthorizationEndPoint as OpenIdAuthorizationEndPoint;
13
use Parroauth2\Client\OpenID\EndPoint\EndSessionEndPoint;
14
use Parroauth2\Client\OpenID\EndPoint\Token\TokenEndPoint as OpenIdTokenEndPoint;
15
use Parroauth2\Client\OpenID\EndPoint\Userinfo\UserinfoEndPoint;
16
use Parroauth2\Client\OpenID\IdToken\IdTokenParserInterface;
17
use Parroauth2\Client\OpenID\IdToken\JwsIdTokenParser;
18
use Parroauth2\Client\Provider\ProviderInterface;
19
use Parroauth2\Client\Storage\ArrayStorage;
20
use Parroauth2\Client\Storage\StorageInterface;
21
22
/**
23
 * Client factory which detect if openid is enabled, and register the corresponding endpoints
24
 */
25
final class BaseClientFactory implements ClientFactoryInterface
26
{
27
    /**
28
     * @var StorageInterface
29
     */
30
    private $storage;
31
32
    /**
33
     * @var EndPointConfigurator
34
     */
35
    private $oauthConfigurator;
36
37
    /**
38
     * @var EndPointConfigurator
39
     */
40
    private $openidConfigurator;
41
42
    /**
43
     * @var IdTokenParserInterface|null
44
     */
45
    private $idTokenParser;
46
47
48
    /**
49
     * BaseClientFactory constructor.
50
     *
51
     * @param StorageInterface|null $storage
52
     * @param IdTokenParserInterface|null $idTokenParser
53
     */
54 196
    public function __construct(StorageInterface $storage = null, IdTokenParserInterface $idTokenParser = null)
55
    {
56 196
        $this->storage = $storage ?: new ArrayStorage();
57 196
        $this->idTokenParser = $idTokenParser;
58
59 196
        $this->oauthConfigurator = new EndPointConfigurator($oauthEndpoints = [
60 196
            AuthorizationEndPoint::NAME => AuthorizationEndPoint::class,
61
            TokenEndPoint::NAME => TokenEndPoint::class,
62
            RevocationEndPoint::NAME => RevocationEndPoint::class,
63
            IntrospectionEndPoint::NAME => IntrospectionEndPoint::class,
64
        ]);
65
66 196
        $this->openidConfigurator = new EndPointConfigurator([
67 196
            OpenIdAuthorizationEndPoint::NAME => OpenIdAuthorizationEndPoint::class,
68
            OpenIdTokenEndPoint::NAME => function (ClientInterface $client) {
69 117
                return new OpenIdTokenEndPoint($client, $this->idTokenParser ?: new JwsIdTokenParser());
70 196
            },
71 196
            UserinfoEndPoint::NAME => UserinfoEndPoint::class,
72 196
            EndSessionEndPoint::NAME => EndSessionEndPoint::class,
73 196
        ] + $oauthEndpoints);
74 196
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 144
    public function create(ProviderInterface $provider, ClientConfig $config): ClientInterface
80
    {
81 144
        $client = new Client($provider, $config, $this->storage);
82
83 144
        if ($config->openid() && $provider->openid()) {
84 119
            $this->openidConfigurator->configure($client);
85
        } else {
86 25
            $this->oauthConfigurator->configure($client);
87
        }
88
89 144
        return $client;
90
    }
91
92
    /**
93
     * @return StorageInterface
94
     */
95 2
    public function storage(): StorageInterface
96
    {
97 2
        return $this->storage;
98
    }
99
100
    /**
101
     * @return IdTokenParserInterface|null
102
     */
103
    public function idTokenParser(): ?IdTokenParserInterface
104
    {
105
        return $this->idTokenParser;
106
    }
107
}
108