Factory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 97
ccs 22
cts 24
cp 0.9167
rs 10
c 1
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A auth() 0 3 1
A getAdapter() 0 3 1
A allowedAdapters() 0 5 1
A __construct() 0 3 1
A setAdapter() 0 20 5
A getById() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Component\Auth;
6
7
use Nymfonya\Component\Config;
8
use Nymfonya\Component\Container;
9
use App\Component\Auth\AdapterInterface;
10
use App\Component\Auth\Adapters\File as FileAdapter;
11
use App\Component\Auth\Adapters\Config as ConfigAdapter;
12
use App\Component\Auth\Adapters\Repository as RepositoryAdapter;
13
14
class Factory implements AdapterInterface
15
{
16
17
    const CONFIG_AUTH_ADAPTER = 'auth';
18
19
    /**
20
     * container
21
     *
22
     * @var Container
23
     */
24
    protected $container;
25
26
    /**
27
     * adapter
28
     *
29
     * @var AdapterInterface
30
     */
31
    protected $adapter;
32
33
    /**
34
     * instanciate
35
     *
36
     * @param Container $container
37
     */
38 9
    public function __construct(Container $container)
39
    {
40 9
        $this->container = $container;
41
    }
42
43
    /**
44
     * set auth adapter
45
     *
46
     * @param string $classname
47
     * @return Factory
48
     */
49 7
    public function setAdapter(string $classname = ''): Factory
50
    {
51 7
        if (empty($classname)) {
52 3
            $config = $this->container->getService(Config::class);
53 3
            if (false === $config->hasEntry(self::CONFIG_AUTH_ADAPTER)) {
54 1
                throw new \Exception('Missing auth config entry');
55
            }
56 2
            $adapterConfig = $config->getSettings(self::CONFIG_AUTH_ADAPTER);
57 2
            if (false === isset($adapterConfig['adapter'])) {
58 1
                throw new \Exception('Missing auth config adapter');
59
            }
60 1
            $adapterClassname = $adapterConfig['adapter'];
61
        } else {
62 4
            $adapterClassname = $classname;
63
        }
64 5
        if (false === in_array($adapterClassname, $this->allowedAdapters())) {
65 1
            throw new \Exception('Bad auth adapter classname');
66
        }
67 4
        $this->adapter = new $adapterClassname($this->container);
68 4
        return $this;
69
    }
70
71
    /**
72
     * return adapter instance
73
     *
74
     * @return AdapterInterface
75
     */
76 4
    public function getAdapter(): AdapterInterface
77
    {
78 4
        return $this->adapter;
79
    }
80
81
    /**
82
     * return auth user
83
     *
84
     * @return array
85
     */
86 4
    public function auth(string $login, string $password): array
87
    {
88 4
        return $this->getAdapter()->auth($login, $password);
89
    }
90
91
    /**
92
     * return user by id
93
     *
94
     * @return array
95
     */
96
    public function getById(int $id): array
97
    {
98
        return $this->getAdapter()->getById($id);
99
    }
100
101
    /**
102
     * returns alloed adapter list
103
     *
104
     * @return array
105
     */
106 1
    protected function allowedAdapters(): array
107
    {
108
        return [
109 1
            FileAdapter::class, ConfigAdapter::class,
110
            RepositoryAdapter::class
111
        ];
112
    }
113
}
114