Completed
Pull Request — master (#295)
by Anton
11:59
created

Row::isBasic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
crap 1
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
     * Before Insert/Update
27
     *
28
     * @return void
29
     */
30
    protected function beforeInsert()
31
    {
32
        $this->addValidator('name')
0 ignored issues
show
Bug introduced by
It seems like required() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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
     * Before Update
45
     *
46
     * @return void
47
     */
48
    protected function beforeUpdate()
49
    {
50
        $this->addValidator('name')
0 ignored issues
show
Bug introduced by
It seems like required() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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
    public function isBasic() : bool
79 1
    {
80
        return in_array(strtolower($this->name), Table::getInstance()->getBasicRoles(), false);
81 1
    }
82
}
83