Passed
Push — master ( a6d59a...971523 )
by butschster
16:28 queued 13s
created

TokenStorageProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 35
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 13 3
A getStorage() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Auth;
6
7
use Spiral\Auth\Config\AuthConfig;
8
use Spiral\Core\Container\Autowire;
9
use Spiral\Core\FactoryInterface;
10
11
class TokenStorageProvider implements TokenStorageProviderInterface
12
{
13
    /** @var TokenStorageInterface[] */
14
    private array $storages = [];
15
16 274
    public function __construct(
17
        private readonly AuthConfig $config,
18
        private readonly FactoryInterface $factory
19
    ) {
20
    }
21
22 274
    public function getStorage(?string $name = null): TokenStorageInterface
23
    {
24 274
        $name ??= $this->config->getDefaultStorage();
25
26 274
        if (isset($this->storages[$name])) {
27 1
            return $this->storages[$name];
28
        }
29
30 274
        return $this->storages[$name] = $this->resolve($name);
31
    }
32
33 274
    private function resolve(string $name): TokenStorageInterface
34
    {
35 274
        $storage = $this->config->getStorage($name);
36
37 272
        if ($storage instanceof TokenStorageInterface) {
38 2
            return $storage;
39
        }
40
41 270
        if ($storage instanceof Autowire) {
42 1
            return $storage->resolve($this->factory);
43
        }
44
45 269
        return $this->factory->make($storage);
46
    }
47
}
48