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

Accounts::getById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 1
dl 0
loc 19
ccs 16
cts 16
cp 1
crap 2
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace App\Model;
4
5
use Nymfonya\Component\Container;
6
use Nymfonya\Component\Config;
7
use App\Component\Auth\AuthInterface;
8
use App\Model\AbstractSearch;
9
use App\Component\Crypt;
10
11
/**
12
 * Class App\Model\Accounts
13
 *
14
 * Provides account list from file accounts.csv.
15
 */
16
class Accounts extends AbstractSearch implements AuthInterface
17
{
18
    const _ID = 'id';
19
    const _NAME = 'name';
20
    const _EMAIL = 'email';
21
    const _PASSWORD = 'password';
22
    const _STATUS = 'status';
23
    const _ROLE = 'role';
24
    const PATH_ASSETS_MODEL = '/../assets/model/';
25
    const ACCOUNTS_FILENAME = '/accounts.csv';
26
    const FIELD_SEPARATOR = ',';
27
    const FILTER_ALL = '/^(.*),(.*),(.*),(.*),(.*),(.*)/';
28
29
    /**
30
     * config
31
     *
32
     * @var Config
33
     */
34
    protected $config;
35
36
    /**
37
     * instanciate
38
     *
39
     */
40 11
    public function __construct(Container $container)
41
    {
42 11
        parent::__construct($container);
43 11
        $this->config = $this->getService(Config::class);
44 11
        $this->init();
45 11
        return $this;
46
    }
47
48
    /**
49
     * auth
50
     *
51
     * @return array
52
     */
53 3
    public function auth(string $login, string $password): array
54
    {
55
        $filter =
56
            '/^(.*),'
57
            . '(.*),'
58 3
            . '(' . $login . '),'
59 3
            . '(.*),'
60 3
            . '(.*),'
61 3
            . '(.*)/';
62 3
        $this->setFilter($filter)->readFromStream();
63 3
        $result = $this->get();
64 3
        if (empty($result)) {
65 1
            return [];
66
        }
67 2
        $user = $result[0];
68 2
        $crypt = new Crypt($this->config);
69 2
        if ($password == $crypt->decrypt($user[self::_PASSWORD], true)) {
70 1
            return $user;
71
        }
72 1
        unset($crypt);
73 1
        return [];
74
    }
75
76
    /**
77
     * getById
78
     *
79
     * @return array
80
     */
81 1
    public function getById(int $id): array
82
    {
83
        $filter =
84 1
            '/^(' . $id . '),'
85 1
            . '(.*),'
86 1
            . '(.*),'
87 1
            . '(.*),'
88 1
            . '(.*),'
89 1
            . '(.*)/';
90 1
        $this->setFilter($filter)->readFromStream();
91 1
        $result = $this->get();
92 1
        if (empty($result)) {
93 1
            return [];
94
        }
95 1
        $user = $result[0];
96 1
        $crypt = new Crypt($this->config);
97 1
        $clearPassword = $crypt->decrypt($user[self::_PASSWORD], true);
98 1
        $user[self::_PASSWORD] = $clearPassword;
99 1
        return $user;
100
    }
101
102
    /**
103
     * return csv content as array
104
     *
105
     * @return array
106
     */
107 1
    public function toArray(): array
108
    {
109 1
        return array_map(
110 1
            'str_getcsv',
111 1
            file($this->getAccountsFilename())
0 ignored issues
show
Bug introduced by
It seems like file($this->getAccountsFilename()) can also be of type false; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
            /** @scrutinizer ignore-type */ file($this->getAccountsFilename())
Loading history...
112
        );
113
    }
114
115
    /**
116
     * init
117
     *
118
     * @param string $assetsPath
119
     * @return AbstractSearch
120
     */
121 1
    protected function init(): AbstractSearch
122
    {
123 1
        $this->setFilename($this->getAccountsFilename());
124 1
        $this->createFile($this->filename);
125 1
        $this->setFilter(self::FILTER_ALL);
126 1
        $this->setSeparator(self::FIELD_SEPARATOR);
127 1
        return $this;
128
    }
129
130
    /**
131
     * return csv file accounts filename
132
     *
133
     * @return string
134
     */
135 1
    protected function getAccountsFilename(): string
136
    {
137 1
        return realpath(
138 1
            $this->config->getPath() . self::PATH_ASSETS_MODEL
139 1
        ) . self::ACCOUNTS_FILENAME;
140
    }
141
142
    /**
143
     * add account item to stack
144
     *
145
     * @param array $data
146
     * @return AbstractSearch
147
     */
148 1
    protected function setItem(array $data): AbstractSearch
149
    {
150 1
        $this->datas[] = [
151 1
            self::_ID => $data[0],
152 1
            self::_NAME => $data[1],
153 1
            self::_EMAIL => $data[2],
154 1
            self::_PASSWORD => $data[3],
155 1
            self::_STATUS => $data[4],
156 1
            self::_ROLE => $data[5]
157
        ];
158 1
        return $this;
159
    }
160
161
    /**
162
     * create csv file account from config accounts setting
163
     *
164
     * @param string $filename
165
     * @return AbstractSearch
166
     */
167 1
    protected function createFile(string $filename): AbstractSearch
168
    {
169 1
        if (!file_exists($filename)) {
170 1
            $crypt = new Crypt($this->config);
171 1
            $accounts = $this->config->getSettings(Config::_ACCOUNTS);
172 1
            $accounts = array_map(function ($acc) use ($crypt) {
173 1
                $acc[self::_PASSWORD] = $crypt->encrypt(
174 1
                    $acc[self::_PASSWORD],
175 1
                    true
176
                );
177 1
                return $acc;
178 1
            }, $accounts);
179 1
            $handler = fopen($filename, 'w');
180 1
            $error = (false === $handler);
181 1
            if ($error === false) {
182 1
                foreach ($accounts as $record) {
183 1
                    fputcsv($handler, array_values($record));
184
                }
185 1
                fclose($handler);
186
            }
187 1
            unset($handler, $accounts, $crypt);
188
        }
189 1
        return $this;
190
    }
191
}
192