Completed
Push — master ( c889ea...14eda7 )
by Pierre
03:04
created

Accounts::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
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 9
    public function __construct(Container $container)
41
    {
42 9
        parent::__construct($container);
43 9
        $this->config = $this->getService(Config::class);
44 9
        $this->init();
45 9
        return $this;
46
    }
47
48
    /**
49
     * auth
50
     *
51
     * @return array
52
     */
53 3
    public function auth(string $login, string $password): array
54
    {
55 3
        $crypt = new Crypt($this->config);
56
        $filter =
57
            '/^(.*),'
58
            . '(.*),'
59 3
            . '(' . $login . '),'
60 3
            . '(.*),'
61 3
            . '(.*),'
62 3
            . '(.*)/';
63 3
        $this->setFilter($filter)->readFromStream();
64 3
        $result = $this->get();
65 3
        if (empty($result)) {
66 1
            return [];
67
        }
68 2
        $user = $result[0];
69 2
        if ($password == $crypt->decrypt($user[self::_PASSWORD])) {
70 1
            return $user;
71
        }
72 1
        unset($crypt);
73 1
        return [];
74
    }
75
76
    /**
77
     * return csv content as array
78
     *
79
     * @return array
80
     */
81
    public function toArray(): array
82
    {
83
        return array_map(
84
            'str_getcsv',
85
            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

85
            /** @scrutinizer ignore-type */ file($this->getAccountsFilename())
Loading history...
86
        );
87
    }
88
89
    /**
90
     * init
91
     *
92
     * @param string $assetsPath
93
     * @return AbstractSearch
94
     */
95 1
    protected function init(): AbstractSearch
96
    {
97 1
        $this->setFilename($this->getAccountsFilename());
98 1
        $this->createFile($this->filename);
99 1
        $this->setFilter(self::FILTER_ALL);
100 1
        $this->setSeparator(self::FIELD_SEPARATOR);
101 1
        return $this;
102
    }
103
104
    /**
105
     * return csv file accounts filename
106
     *
107
     * @return string
108
     */
109 1
    protected function getAccountsFilename(): string
110
    {
111 1
        return realpath(
112 1
            $this->config->getPath() . self::PATH_ASSETS_MODEL
113 1
        ) . self::ACCOUNTS_FILENAME;
114
    }
115
116
    /**
117
     * add account item to stack
118
     *
119
     * @param array $data
120
     * @return AbstractSearch
121
     */
122 1
    protected function setItem(array $data): AbstractSearch
123
    {
124 1
        $this->datas[] = [
125 1
            self::_ID => $data[0],
126 1
            self::_NAME => $data[1],
127 1
            self::_EMAIL => $data[2],
128 1
            self::_PASSWORD => $data[3],
129 1
            self::_STATUS => $data[4],
130 1
            self::_ROLE => $data[5]
131
        ];
132 1
        return $this;
133
    }
134
135
    /**
136
     * create csv file account from config accounts setting
137
     *
138
     * @param string $filename
139
     * @return AbstractSearch
140
     */
141 1
    protected function createFile(string $filename): AbstractSearch
142
    {
143 1
        if (!file_exists($filename)) {
144 1
            $crypt = new Crypt($this->config);
145 1
            $accounts = $this->config->getSettings(Config::_ACCOUNTS);
146 1
            $accounts = array_map(function ($acc) use ($crypt) {
147 1
                $acc[self::_PASSWORD] = $crypt->encrypt(
148 1
                    $acc[self::_PASSWORD],
149 1
                    true
150
                );
151 1
                return $acc;
152 1
            }, $accounts);
153 1
            $handler = fopen($filename, 'w');
154 1
            $error = (false === $handler);
155 1
            if ($error === false) {
156 1
                foreach ($accounts as $record) {
157 1
                    fputcsv($handler, array_values($record));
158
                }
159 1
                fclose($handler);
160
            }
161 1
            unset($handler, $accounts, $crypt);
162
        }
163 1
        return $this;
164
    }
165
}
166