Completed
Push — master ( 482363...f2fb73 )
by Pierre
03:07
created

Accounts::getAccountsFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
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
     * init
78
     *
79
     * @param string $assetsPath
80
     * @return AbstractSearch
81
     */
82 1
    protected function init(): AbstractSearch
83
    {
84 1
        $this->setFilename($this->getAccountsFilename());
85 1
        $this->createFile($this->filename);
86 1
        $this->setFilter(self::FILTER_ALL);
87 1
        $this->setSeparator(self::FIELD_SEPARATOR);
88 1
        return $this;
89
    }
90
91
    /**
92
     * return csv file accounts filename
93
     *
94
     * @return string
95
     */
96 1
    protected function getAccountsFilename(): string
97
    {
98 1
        return realpath(
99 1
            $this->config->getPath() . self::PATH_ASSETS_MODEL
100 1
        ) . self::ACCOUNTS_FILENAME;
101
    }
102
103
    /**
104
     * add account item to stack
105
     *
106
     * @param array $data
107
     * @return AbstractSearch
108
     */
109 1
    protected function setItem(array $data): AbstractSearch
110
    {
111 1
        $this->datas[] = [
112 1
            self::_ID => $data[0],
113 1
            self::_NAME => $data[1],
114 1
            self::_EMAIL => $data[2],
115 1
            self::_PASSWORD => $data[3],
116 1
            self::_STATUS => $data[4],
117 1
            self::_ROLE => $data[5]
118
        ];
119 1
        return $this;
120
    }
121
122
    /**
123
     * create csv file account from config accounts setting
124
     *
125
     * @param string $filename
126
     * @return AbstractSearch
127
     */
128 1
    protected function createFile(string $filename): AbstractSearch
129
    {
130 1
        if (!file_exists($filename)) {
131 1
            $crypt = new Crypt($this->config);
132 1
            $accounts = $this->config->getSettings(Config::_ACCOUNTS);
133 1
            $accounts = array_map(function ($acc) use ($crypt) {
134 1
                $acc[self::_PASSWORD] = $crypt->encrypt(
135 1
                    $acc[self::_PASSWORD],
136 1
                    true
137
                );
138 1
                return $acc;
139 1
            }, $accounts);
140 1
            $handler = fopen($filename, 'w');
141 1
            $error = (false === $handler);
142 1
            if ($error === false) {
143 1
                foreach ($accounts as $record) {
144 1
                    fputcsv($handler, array_values($record));
145
                }
146 1
                fclose($handler);
147
            }
148 1
            unset($handler, $accounts, $crypt);
149
        }
150 1
        return $this;
151
    }
152
}
153