Completed
Push — master ( 878b1b...cb4691 )
by Anton
09:55 queued 08:01
created

Row::beforeInsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 12
ccs 0
cts 9
cp 0
crap 2
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link      https://github.com/bluzphp/skeleton
5
 */
6
7
declare(strict_types=1);
8
9
namespace Application\Roles;
10
11
use Bluz\Validator\Traits\Validator;
12
13
/**
14
 * User Role
15
 *
16
 * @package Application\Roles
17
 *
18
 * @property integer $id
19
 * @property string  $name
20
 */
21
class Row extends \Bluz\Db\Row
22
{
23
    use Validator;
24
25
    /**
26
     * {@inheritdoc}
27
     *
28
     * @throws \Bluz\Validator\Exception\ComponentException
29
     */
30
    protected function beforeInsert(): void
31
    {
32
        $this->addValidator('name')
33
            ->required()
34
            ->latin()
35
            ->callback(
36
                function ($name) {
37
                    return !Table::findRowWhere(['name' => $name]);
38
                }
39
            )
40
            ->setDescription('Role already exists');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     *
46
     * @throws \Bluz\Validator\Exception\ComponentException
47
     */
48
    protected function beforeUpdate(): void
49
    {
50
        $this->addValidator('name')
51
            ->required()
52
            ->latin()
53
            ->callback(
54
                function ($name) {
55
                    return !in_array(strtolower($name), Table::getInstance()->getBasicRoles(), false);
56
                },
57
                'Role is basic and can\'t be editable'
58
            )
59
            ->callback(
60
                function ($name) {
61
                    return $this->clean['name'] != $name;
62
                },
63
                'Role name is the same as original'
64
            )
65
            ->callback(
66
                function ($name) {
67
                    return !Table::findRowWhere(['name' => $name]);
68
                },
69
                'Role already exists'
70
            );
71
    }
72
73
    /**
74
     * isBasic
75
     *
76
     * @return boolean
77
     */
78 1
    public function isBasic(): bool
79
    {
80 1
        return in_array(strtolower($this->name), Table::getInstance()->getBasicRoles(), false);
81
    }
82
}
83