Completed
Push — develop ( e70242...efddeb )
by Mathias
65:39 queued 53:48
created

DependencyResult::isDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2018 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Service\EntityEraser;
12
13
/**
14
 * DependencyResult ValueObject.
15
 *
16
 * There are three modes:
17
 *
18
 * MODE_LIST: The Object holds a collection of entities which are affected.
19
 * MODE_COUNT: Instead of all entities in a collection, just the count of affected entities is available.
20
 * MODE_DELETE: Like MODE_LIST, but the EntityEraser plugin should loop through the collection and delete each
21
 *              entity. (if you just do not want to do it in the listener.)
22
 * 
23
 * @author Mathias Gelhausen <[email protected]>
24
 * @todo write test 
25
 */
26
class DependencyResult 
27
{
28
29
    const MODE_LIST = 'list';
30
    const MODE_COUNT = 'count';
31
    const MODE_DELETE = 'delete';
32
33
    /**
34
     * Name of this result.
35
     *
36
     * @var string
37
     */
38
    private $name;
39
40
    /**
41
     * Entities collection.
42
     *
43
     * @var array|\Traversable
44
     */
45
    private $entities;
46
47
    /**
48
     * ViewScript to render the entity list with.
49
     *
50
     * @var string
51
     */
52
    private $viewScript;
53
54
    /**
55
     * Description
56
     *
57
     * @var string
58
     */
59
    private $description;
60
61
    /**
62
     * Mode of this result.
63
     *
64
     * @var string
65
     */
66
    private $mode;
67
68
    /**
69
     * DependencyResult constructor.
70
     *
71
     * There are two valid options:
72
     * - 'description': Sets the description.
73
     * - 'viewScript': Sets the view script.
74
     *
75
     * @param string     $name
76
     * @param array|\Traversable  $entities
77
     * @param array|null $options
78
     */
79
    public function __construct($name, $entities, array $options = null)
80
    {
81
82
        $this->mode =
83
            isset($options['mode']) && in_array($options['mode'], [self::MODE_LIST, self::MODE_COUNT, self::MODE_DELETE])
84
                ? $options['mode']
85
                : self::MODE_LIST
86
        ;
87
88
        if (self::MODE_COUNT == $this->mode) {
89
            if (!is_int($entities)) {
90
                throw new \InvalidArgumentException('Expecting integer value in "count" mode.');
91
            }
92
        } else if (!is_array($entities) && !$entities instanceOf \Traversable) {
93
            throw new \InvalidArgumentException('Entities must be an array or an instance of \Traversable');
94
        }
95
96
        $this->name = (string) $name;
97
        $this->entities = $entities;
0 ignored issues
show
Documentation Bug introduced by
It seems like $entities can also be of type integer. However, the property $entities is declared as type array|object<Traversable>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
98
        $this->viewScript = isset($options['viewScript']) ? $options['viewScript'] : 'core/entity-eraser/dependency-list';
99
        $this->description = isset($options['description']) ? $options['description'] : '';
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getName()
106
    {
107
        return $this->name;
108
    }
109
110
    /**
111
     * @return array|\Traversable
112
     */
113
    public function getEntities()
114
    {
115
        return $this->entities;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getViewScript()
122
    {
123
        return $this->viewScript;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getDescription()
130
    {
131
        return $this->description;
132
    }
133
134
    public function isMode($mode)
135
    {
136
        return $this->mode == $mode;
137
    }
138
139
    public function isList()
140
    {
141
        return $this->isMode(self::MODE_LIST);
142
    }
143
144
    public function isCount()
145
    {
146
        return $this->isMode(self::MODE_COUNT);
147
    }
148
149
    public function isDelete()
150
    {
151
        return $this->isMode(self::MODE_DELETE);
152
    }
153
154
}
155