Completed
Branch master (b52e58)
by Pierre
03:02 queued 37s
created

Accounts::getAccountsFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Model;
4
5
use App\Container;
6
use App\Config;
7
use App\Model\AbstractSearch;
8
use App\Tools\Crypt;
9
10
/**
11
 * Class App\Model\Accounts
12
 *
13
 * Provides account list.
14
 */
15
class Accounts extends AbstractSearch
16
{
17
    const _ID = 'id';
18
    const _NAME = 'name';
19
    const _EMAIL = 'email';
20
    const _PASSWORD = 'password';
21
    const _STATUS = 'status';
22
    const _ROLE = 'role';
23
    const PATH_ASSETS_MODEL = '/../assets/model/';
24
    const ACCOUNTS_FILENAME = '/accounts.csv';
25
    const FIELD_SEPARATOR = ',';
26
    const FILTER_ALL = '/^(.*),(.*),(.*),(.*),(.*)/';
27
28
    /**
29
     * config
30
     *
31
     * @var Config
32
     */
33
    protected $config;
34
35
    /**
36
     * instanciate
37
     *
38
     */
39
    public function __construct(Container $container)
40
    {
41
        parent::__construct($container);
42
        $this->config = $this->getService(\App\Config::class);
43
        $this->init();
44
        return $this;
45
    }
46
47
    /**
48
     * init
49
     *
50
     * @param string $assetsPath
51
     * @return AbstractSearch
52
     */
53
    protected function init(): AbstractSearch
54
    {
55
        $this->setFilename($this->getAccountsFilename());
56
        $this->createFile($this->filename);
57
        $this->setFilter(self::FILTER_ALL);
58
        $this->setSeparator(self::FIELD_SEPARATOR);
59
        return $this;
60
    }
61
62
    /**
63
     * return csv file accounts filename
64
     *
65
     * @return string
66
     */
67
    protected function getAccountsFilename(): string
68
    {
69
        return realpath(
70
            $this->config->getPath() . self::PATH_ASSETS_MODEL
71
        )  . self::ACCOUNTS_FILENAME;
72
    }
73
74
    /**
75
     * add account item to stack
76
     *
77
     * @param array $data
78
     * @return AbstractSearch
79
     */
80
    protected function setItem(array $data): AbstractSearch
81
    {
82
        $this->datas[] = [
83
            self::_ID => $data[0],
84
            self::_NAME => $data[1],
85
            self::_EMAIL => $data[2],
86
            self::_PASSWORD => $data[3],
87
            self::_STATUS => $data[4],
88
            self::_ROLE => $data[5]
89
        ];
90
        return $this;
91
    }
92
93
    /**
94
     * create csv file account from config accounts setting
95
     *
96
     * @param string $filename
97
     * @return AbstractSearch
98
     */
99
    protected function createFile(string $filename): AbstractSearch
100
    {
101
        if (!file_exists($filename)) {
102
            $crypt = new Crypt($this->config);
103
            $accounts = $this->config->getSettings(Config::_ACCOUNTS);
104
            $accounts = array_map(function ($ac) use ($crypt) {
105
                $ac[self::_PASSWORD] = $crypt->encrypt(
106
                    $ac[self::_PASSWORD],
107
                    true
108
                );
109
                return $ac;
110
            }, $accounts);
111
            $fp = fopen($filename, 'w');
112
            foreach ($accounts as $record) {
113
                fputcsv($fp, array_values($record));
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, 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

113
                fputcsv(/** @scrutinizer ignore-type */ $fp, array_values($record));
Loading history...
114
            }
115
            fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

115
            fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
116
            unset($fp, $accounts, $crypt);
117
        }
118
        return $this;
119
    }
120
}
121