Lines::runInsert()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 21
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 30
ccs 19
cts 19
cp 1
crap 5
rs 9.2728
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Component\Migration\Metro;
6
7
use App\Component\Model\Orm\Orm;
8
use App\Component\Db\Migration;
9
use App\Model\Repository\Metro\Lines as LinesRepository;
10
use Nymfonya\Component\Container;
11
use SplFileObject;
12
13
class Lines extends Migration
14
{
15
    const MIG_FIELDS = [
16
        'id', 'ligne', 'src', 'hsrc', 'dst', 'hdst', 'dist'
17
    ];
18
19
    const CSV_FIXTURE = '/../../../../../assets/model/metro/lines.csv';
20
21
    /**
22
     * repository
23
     *
24
     * @var Orm
25
     */
26
    protected $repository;
27
28
    /**
29
     * instanciate
30
     *
31
     * @param Container $container
32
     */
33 8
    public function __construct(Container $container)
34
    {
35 8
        parent::__construct($container);
36 8
        $this->repository = new LinesRepository($container);
37 8
        $this->fromOrm($this->repository);
38
    }
39
40
    /**
41
     * check if migration can run
42
     *
43
     * @return boolean
44
     */
45 1
    protected function canMigrate(): bool
46
    {
47 1
        return (!$this->tableExists());
48
    }
49
50
    /**
51
     * create table
52
     *
53
     * @return Migration
54
     */
55 1
    protected function runCreate(): Migration
56
    {
57 1
        if (!$this->tableExists()) {
58 1
            $sql = sprintf(
59 1
                'CREATE TABLE `%s` (
60
                    `id` bigint(20) NOT NULL ,
61
                    `ligne` varchar(4) NOT NULL,
62
                    `src` varchar(150) NOT NULL,
63
                    `hsrc` varchar(16) NOT NULL,
64
                    `dst` varchar(150) NOT NULL,
65
                    `hdst` varchar(16) NOT NULL,
66
                    `dist` double NOT NULL
67
                  ) ENGINE=InnoDB DEFAULT CHARSET=utf8',
68 1
                $this->repository->getTable()
69
            );
70 1
            $this->run($sql);
71
        }
72 1
        return $this;
73
    }
74
75
    /**
76
     * insert into table
77
     *
78
     * @return Migration
79
     */
80 1
    protected function runInsert(): Migration
81
    {
82 1
        if ($this->tableExists()) {
83 1
            $stream = new SplFileObject(
84 1
                __DIR__ . self::CSV_FIXTURE
85
            );
86 1
            $stream->setFlags(
87
                SplFileObject::READ_CSV |
88
                    SplFileObject::SKIP_EMPTY |
89
                    SplFileObject::READ_AHEAD |
90 1
                    SplFileObject::DROP_NEW_LINE
91
            );
92 1
            while (false === $stream->eof()) {
93 1
                $csvData = $stream->fgetcsv();
94 1
                if (!empty($csvData)) {
95 1
                    $data = array_combine(self::MIG_FIELDS, $csvData);
96 1
                    if (false !== $data) {
97 1
                        $this->repository->resetBuilder()->insert($data);
98 1
                        $this->run(
99 1
                            $this->repository->getSql(),
100 1
                            $this->repository->getBuilderValues()
101
                        );
102
                    }
103
                }
104 1
                unset($csvData);
105
            }
106 1
            $stream = null;
107 1
            unset($stream);
108
        }
109 1
        return $this;
110
    }
111
112
    /**
113
     * index table
114
     *
115
     * @return Migration
116
     */
117 1
    protected function runIndex(): Migration
118
    {
119 1
        $pkey = $this->repository->getPrimary();
120 1
        $sqlIndex = sprintf(
121 1
            'ALTER TABLE `%s`'
122
                . 'ADD PRIMARY KEY (`%s`),'
123
                . 'ADD KEY `%s` (`%s`),'
124
                . 'ADD KEY `ligne` (`ligne`),'
125
                . 'ADD KEY `hsrc` (`hsrc`),'
126 1
                . 'ADD KEY `hdst` (`hdst`)',
127 1
            $this->repository->getTable(),
128
            $pkey,
129
            $pkey,
130
            $pkey
131
        );
132 1
        $this->run($sqlIndex);
133 1
        $sqlAutoinc = sprintf('ALTER TABLE `%s`'
134
            . 'MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, '
135 1
            . 'AUTO_INCREMENT=1', $this->repository->getTable());
136 1
        $this->run($sqlAutoinc);
137 1
        return $this;
138
    }
139
140
    /**
141
     * return true if table exists
142
     *
143
     * @return boolean
144
     */
145 1
    protected function tableExists(): bool
146
    {
147 1
        $sqlWhere = ' WHERE table_schema = \'%s\' AND table_name = \'%s\';';
148 1
        $sql = sprintf(
149 1
            'SELECT count(*) as counter FROM %s ' . $sqlWhere,
150 1
            'information_schema.tables',
151 1
            $this->repository->getDatabase(),
152 1
            $this->repository->getTable()
153
        );
154 1
        $this->run($sql)->hydrate();
155 1
        $result = $this->getRowset()[0];
156 1
        $counter = (int) $result['counter'];
157 1
        return ($counter > 0);
158
    }
159
160
    /**
161
     * drop table if exists
162
     *
163
     * @return boolean
164
     */
165 2
    protected function dropTable(): bool
166
    {
167 2
        if (!$this->tableExists()) {
168 1
            return false;
169
        }
170 1
        $sql = 'DROP TABLE ' . $this->repository->getTable() . ';';
171 1
        $this->run($sql);
172 1
        return true;
173
    }
174
}
175