Repository   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
c 2
b 0
f 0
dl 0
loc 101
ccs 34
cts 34
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A decrypt() 0 3 1
A auth() 0 16 3
A __construct() 0 7 1
A getById() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Component\Auth\Adapters;
6
7
use Nymfonya\Component\Config as AppConfig;
8
use Nymfonya\Component\Container;
9
use App\Component\Auth\AdapterInterface;
10
use App\Component\Crypt;
11
use App\Model\Repository\Users;
12
use App\Component\Db\Core;
13
14
/**
15
 * Adapter Repository let auth from config accounts entries
16
 * Decryption required on password.
17
 */
18
class Repository implements AdapterInterface
19
{
20
21
    /**
22
     * container
23
     *
24
     * @var Container
25
     */
26
    protected $container;
27
28
    /**
29
     * app config
30
     *
31
     * @var AppConfig
32
     */
33
    protected $config;
34
35
    /**
36
     * user repository
37
     *
38
     * @var Users
39
     */
40
    protected $userRepo;
41
42
    /**
43
     * db core instance
44
     *
45
     * @var Core
46
     */
47
    protected $dbCore;
48
49
    /**
50
     * instanciate
51
     *
52
     * @param Container $container
53
     */
54 5
    public function __construct(Container $container)
55
    {
56 5
        $this->container = $container;
57 5
        $this->userRepo = new Users($this->container);
58 5
        $this->dbCore = new Core($this->container);
59 5
        $this->dbCore->fromOrm($this->userRepo);
60 5
        $this->config = $this->container->getService(AppConfig::class);
61
    }
62
63
    /**
64
     * auth process
65
     *
66
     * @return array
67
     */
68 2
    public function auth(string $login, string $password): array
69
    {
70 2
        $this->userRepo->getByEmail($login);
71 2
        $this->dbCore
72 2
            ->run(
73 2
                $this->userRepo->getSql(),
74 2
                $this->userRepo->getBuilderValues()
75
            )
76 2
            ->hydrate();
77 2
        $result = $this->dbCore->getRowset();
78 2
        if (empty($result)) {
79 1
            return [];
80
        }
81 1
        $user = $result[0];
82 1
        $clearPassword = $this->decrypt($user[self::_PASSWORD]);
83 1
        return ($password === $clearPassword) ? $user : [];
84
    }
85
86
    /**
87
     * return user by id
88
     *
89
     * @param integer $id
90
     * @return array
91
     */
92 1
    public function getById(int $id): array
93
    {
94 1
        $this->userRepo->getById($id);
95 1
        $this->dbCore
96 1
            ->run(
97 1
                $this->userRepo->getSql(),
98 1
                $this->userRepo->getBuilderValues()
99
            )
100 1
            ->hydrate();
101 1
        $result = $this->dbCore->getRowset();
102 1
        if (empty($result)) {
103 1
            return [];
104
        }
105 1
        $user = $result[0];
106 1
        $user[self::_PASSWORD] = $this->decrypt($user[self::_PASSWORD]);
107 1
        return $user;
108
    }
109
110
    /**
111
     * decrypt content
112
     *
113
     * @param string $content
114
     * @return string
115
     */
116 1
    protected function decrypt(string $content): string
117
    {
118 1
        return (new Crypt($this->config))->decrypt($content, true);
119
    }
120
}
121