Completed
Push — master ( 3e38c4...3f7c7b )
by Michal
02:49
created

RedisEditDatabaseForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace UniMan\Drivers\Redis\Forms;
4
5
use Exception;
6
use Nette\Application\UI\Form;
7
use Nette\Utils\ArrayHash;
8
use UniMan\Core\Forms\DatabaseForm\DatabaseFormInterface;
9
use UniMan\Drivers\Redis\RedisDatabaseAliasStorage;
10
11
class RedisEditDatabaseForm implements DatabaseFormInterface
12
{
13
    private $database;
14
15
    private $databaseAliasStorage;
16
17
    public function __construct($database, RedisDatabaseAliasStorage $databaseAliasStorage)
18
    {
19
        $this->database = $database;
20
        $this->databaseAliasStorage = $databaseAliasStorage;
21
    }
22
23
    public function addFieldsToForm(Form $form)
24
    {
25
        try {
26
            $this->databaseAliasStorage->check();
27
        } catch (Exception $e) {
28
            $form->addError($e->getMessage());
29
        }
30
31
        $form->addText('database', 'redis.database_form.database.label')
32
            ->setRequired('redis.database_form.database.required')
0 ignored issues
show
Documentation introduced by
'redis.database_form.database.required' 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...
33
            ->setDisabled();
34
35
        $form->addText('alias', 'redis.database_form.alias.label')
36
            ->setOption('description', 'redis.database_form.alias.description');
37
38
        $form->setDefaults([
39
            'database' => $this->database,
40
            'alias' => $this->databaseAliasStorage->load($this->database),
41
        ]);
42
    }
43
44
    public function submit(Form $form, ArrayHash $values)
45
    {
46
        try {
47
            $this->databaseAliasStorage->check();
48
        } catch (Exception $e) {
49
            $form->addError($e->getMessage());
50
            return;
51
        }
52
        $aliases = $this->databaseAliasStorage->loadAll();
53
        $aliases[$this->database] = $values['alias'];
54
        if (!$values['alias']) {
55
            unset($aliases[$this->database]);
56
        }
57
        return $this->databaseAliasStorage->save($aliases);
58
    }
59
}
60