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

TokenStorageProvider::getStorage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
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 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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