Completed
Push — master ( 62da74...554a7a )
by Anton
11s
created

Row   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 51
rs 10
ccs 18
cts 21
cp 0.8571
wmc 4
lcom 2
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A afterRead() 0 4 1
A beforeSave() 0 13 1
A beforeInsert() 0 10 1
A beforeUpdate() 0 4 1
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application\Options;
11
12
use Bluz\Validator\Traits\Validator;
13
use Bluz\Validator\Validator as v;
14
15
/**
16
 * Options Row
17
 *
18
 * @property string $namespace
19
 * @property string $key
20
 * @property string $value
21
 * @property string $description
22
 * @property string $created
23
 * @property string $updated
24
 *
25
 * @category Application
26
 * @package  Options
27
 */
28
class Row extends \Bluz\Db\Row
29
{
30
    use Validator;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    protected function afterRead()
36
    {
37 2
        $this->value = unserialize($this->value);
38 2
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1
    protected function beforeSave()
44
    {
45 1
        $this->value = serialize($this->value);
46
47 1
        $this->addValidator(
48 1
            'namespace',
49 1
            v::required()->slug()
50
        );
51 1
        $this->addValidator(
52 1
            'key',
53 1
            v::required()->slug()
54
        );
55 1
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    protected function beforeInsert()
61
    {
62
        // unique validator
63 1
        $this->addValidator(
64 1
            'key',
65 1
            v::callback(function () {
66
                return !$this->getTable()->findRowWhere(['key' => $this->key, 'namespace' => $this->namespace]);
67 1
            })->setError('Key name "{{input}}" already exists')
68
        );
69 1
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function beforeUpdate()
75
    {
76
        $this->updated = gmdate('Y-m-d H:i:s');
77
    }
78
}
79