MySqlDatabaseForm::submit()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 14
cp 0
rs 9.2568
c 0
b 0
f 0
cc 5
nc 12
nop 2
crap 30
1
<?php
2
3
namespace UniMan\Drivers\Mysql\Forms;
4
5
use Nette\Application\UI\Form;
6
use Nette\Utils\ArrayHash;
7
use PDO;
8
use UniMan\Core\Forms\DatabaseForm\DatabaseFormInterface;
9
10
class MySqlDatabaseForm implements DatabaseFormInterface
11
{
12
    private $pdo;
13
14
    private $database;
15
16
    public function __construct(PDO $pdo, $database)
17
    {
18
        $this->pdo = $pdo;
19
        $this->database = $database;
20
    }
21
22
    public function addFieldsToForm(Form $form)
23
    {
24
        $form->addText('name', 'Name')
25
            ->setRequired();
26
27
        $characterSets = $this->pdo->query('SELECT CHARACTER_SET_NAME, CONCAT(CHARACTER_SET_NAME, " (", DESCRIPTION, ")") FROM information_schema.CHARACTER_SETS ORDER BY CHARACTER_SET_NAME')->fetchAll(PDO::FETCH_KEY_PAIR);
28
        $form->addSelect('charset', 'Character set', $characterSets)
29
            ->setAttribute('class', 'js-select2')
0 ignored issues
show
Documentation introduced by
'js-select2' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
            ->setPrompt('Default character set');
31
32
        $collations = [];
33
        foreach ($this->pdo->query('SELECT CHARACTER_SET_NAME, COLLATION_NAME FROM information_schema.COLLATIONS ORDER BY COLLATION_NAME')->fetchAll(PDO::FETCH_ASSOC) as $collation) {
34
            $collations[$collation['CHARACTER_SET_NAME']][$collation['COLLATION_NAME']] = $collation['COLLATION_NAME'];
35
        }
36
        ksort($collations);
37
38
        $form->addSelect('collation', 'Collation', $collations)
39
            ->setAttribute('class', 'js-select2')
0 ignored issues
show
Documentation introduced by
'js-select2' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
            ->setPrompt('Default collation');
41
42
        if ($this->database) {
43
            $form['name']->setDisabled();
44
            $defaults = $this->pdo->query("SELECT `SCHEMA_NAME` AS `name`, `DEFAULT_CHARACTER_SET_NAME` AS `charset`, `DEFAULT_COLLATION_NAME` AS `collation` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = '{$this->database}'")->fetch(PDO::FETCH_ASSOC);
45
            $form->setDefaults($defaults);
46
        }
47
    }
48
49
    public function submit(Form $form, ArrayHash $values)
50
    {
51
        if ($this->database) {
52
            $query = 'ALTER DATABASE ' . $this->database;
53
        } else {
54
            $query = 'CREATE DATABASE ' . $values['name'];
55
        }
56
        if ($values['charset']) {
57
            $query .= ' CHARACTER SET ' . $values['charset'];
58
            if ($values['collation']) {
59
                $query .= ' COLLATE ' . $values['collation'];
60
            }
61
        }
62
63
        $statement = $this->pdo->prepare($query);
64
        $res = $statement->execute();
65
        if ($res === false) {
66
            $form->addError($statement->errorInfo()[2]);
67
            return;
68
        }
69
        return $res;
70
    }
71
}
72