Completed
Push — master ( 921d57...18f6a2 )
by Michal
02:03
created

RedisSortedSetForm::addFieldsToForm()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 14
cp 0
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace UniMan\Drivers\Redis\Forms;
4
5
use Nette\Application\UI\Form;
6
use Nette\Utils\ArrayHash;
7
use RedisProxy\RedisProxy;
8
use UniMan\Core\Forms\TableForm\TableFormInterface;
9
10
class RedisSortedSetForm implements TableFormInterface
11
{
12
    private $connection;
13
14
    private $key;
15
16
    public function __construct(RedisProxy $connection, string $key = null)
17
    {
18
        $this->connection = $connection;
19
        $this->key = $key;
20
    }
21
22
    public function addFieldsToForm(Form $form)
23
    {
24
        $form->addText('key', 'redis.sorted_set_form.key.label')
25
            ->setRequired('redis.sorted_set_form.key.required');
26
27
        $form->addTextArea('members', 'redis.sorted_set_form.members.label')
28
            ->setOption('description', 'redis.sorted_set_form.members.description')
29
            ->setRequired('redis.sorted_set_form.members.required');
30
31
        if ($this->key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->key of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
32
            $members = [];
33
            foreach ($this->connection->zrange($this->key, 0, -1, true) as $member => $score) {
34
                $members[] = $member . ':' . $score;
35
            }
36
            $form->setDefaults([
37
                'key' => $this->key,
38
                'members' => implode(',', $members),
39
            ]);
40
        }
41
    }
42
43
    public function submit(Form $form, ArrayHash $values)
44
    {
45
        if ($this->key && $this->key !== $values->key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->key of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
46
            $this->connection->rename($this->key, $values->key);
47
        }
48
49
        $members = [];
50
        foreach (array_map('trim', explode(',', $values->members)) as $item) {
51
            $member = $item;
52
            $score = 0;
53
            if (strpos($item, ':')) {
54
                list($member, $score) = explode(':', $item, 2);
55
            }
56
            $members[trim($member)] = floatval(trim($score));
57
        }
58
        $this->connection->zadd($values->key, $members);
59
    }
60
}
61