ServerRequestLazyChainProvider   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 83
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A hasToken() 0 4 1
B getPlainToken() 0 42 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\Wallit\Token\Provider;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
9
class ServerRequestLazyChainProvider implements ServerRequestProviderInterface
10
{
11
    /**
12
     * @var mixed[]
13
     */
14
    protected $providers = [];
15
16
    /**
17
     * @var ServerRequestInterface
18
     */
19
    protected $request;
20
21
    /**
22
     * ServerRequestLazyChainProvider constructor.
23
     *
24
     * @param ServerRequestInterface $request
25
     * @param mixed[]                $providers initial providers to lazy load
26
     */
27 14
    public function __construct(ServerRequestInterface $request, array $providers = [])
28
    {
29 14
        if (count($providers) === 0) {
30 1
            throw new \InvalidArgumentException('$providers argument is empty, at least one provider must be set');
31
        }
32 13
        $this->request = $request;
33 13
        $this->providers = $providers;
34 13
    }
35
36
    /**
37
     * @return bool
38
     */
39 3
    public function hasToken(): bool
40
    {
41 3
        return $this->getPlainToken() !== null;
42
    }
43
44
    /**
45
     * @throws \InvalidArgumentException
46
     *
47
     * @return null|string
48
     */
49 13
    public function getPlainToken(): ?string
50
    {
51 13
        foreach ($this->providers as $idx => $providerParam) {
52
            // Lazy load the class
53 13
            if (is_string($providerParam) || is_array($providerParam)) {
54 10
                if (is_string($providerParam)) {
55 1
                    $providerParam = [$providerParam => []];
56
                }
57 10
                $providerClass = key($providerParam);
58
59 10
                if (is_string($providerClass) && class_exists($providerClass)) {
60 9
                    $providerOptions = $providerParam[$providerClass];
61 9
                    $provider = new $providerClass($this->request, $providerOptions);
62
                } else {
63 1
                    throw new \InvalidArgumentException(
64 1
                        sprintf(
65 1
                            "Cannot instanciate provider '%s' class cannot be loaded.",
66 10
                            gettype($providerClass)
67
                        )
68
                    );
69
                }
70 5
            } elseif (!$providerParam instanceof ProviderInterface) {
71 2
                throw new \InvalidArgumentException(
72 2
                    sprintf(
73 2
                        "Type error token provider '%s' must implement '%s'.",
74 2
                        is_object($providerParam) ? get_class($providerParam) : gettype($providerParam),
75 2
                        ProviderInterface::class
76
                    )
77
                );
78
            } else {
79 3
                $provider = $providerParam;
80
            }
81
82
            // Set for later reuse
83 10
            $this->providers[$idx] = $provider;
84 10
            if (null !== ($plainToken = $provider->getPlainToken())) {
85 10
                return $plainToken;
86
            }
87
        }
88
89 1
        return null;
90
    }
91
}
92