Completed
Push — master ( b2bf8f...b90fcd )
by Pierre
03:08
created

Factory   A

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