Engagement::setUpdated()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.2
cc 4
eloc 8
nc 6
nop 1
1
<?php
2
3
namespace SDIS62\Core\Ops\Entity;
4
5
use Datetime;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use SDIS62\Core\Common\Entity\IdentityTrait;
8
use SDIS62\Core\Ops\Exception\InvalidEngagementException;
9
10
abstract class Engagement
11
{
12
    use IdentityTrait;
13
14
    /**
15
     * Création de l'engagement.
16
     *
17
     * @var Datetime
18
     */
19
    protected $created;
20
21
    /**
22
     * Date de mise à jour.
23
     *
24
     * @var Datetime
25
     */
26
    protected $updated;
27
28
    /**
29
     * Fin de l'engagement.
30
     *
31
     * @var Datetime
32
     */
33
    protected $ended;
34
35
    /**
36
     * Evenements particuliers de l'engagement.
37
     *
38
     * @var SDIS62\Core\Ops\Entity\Evenement[]
39
     */
40
    protected $evenements;
41
42
    /**
43
     * Intervention concernée.
44
     *
45
     * @var SDIS62\Core\Ops\Entity\Intervention
46
     */
47
    protected $intervention;
48
49
    /**
50
     * Ajout d'un engagement à une intervention.
51
     *
52
     * @param SDIS62\Core\Ops\Entity\Intervention $intervention
53
     */
54
    public function __construct(Intervention $intervention)
55
    {
56
        $this->intervention = $intervention;
0 ignored issues
show
Documentation Bug introduced by
It seems like $intervention of type object<SDIS62\Core\Ops\Entity\Intervention> is incompatible with the declared type object<SDIS62\Core\Ops\E...ps\Entity\Intervention> of property $intervention.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->intervention->addEngagement($this);
58
59
        $this->evenements = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type array<integer,object<SDI...\Ops\Entity\Evenement>> of property $evenements.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
61
        $this->created = new Datetime('NOW');
62
    }
63
64
    /**
65
     * Get the value of Type de l'engagement.
66
     *
67
     * @return string
68
     */
69
    final public function getType()
70
    {
71
        if (empty($this->type)) {
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72
            throw new InvalidEngagementException(get_class($this).' doit avoir un $type');
73
        }
74
75
        return $this->type;
76
    }
77
78
    /**
79
     * Get the value of Création de l'engagement.
80
     *
81
     * @return Datetime
82
     */
83
    public function getCreated()
84
    {
85
        return $this->created;
86
    }
87
88
    /**
89
     * Get the value of Date de mise à jour.
90
     *
91
     * @return Datetime|null
92
     */
93
    public function getUpdated()
94
    {
95
        return $this->updated;
96
    }
97
98
    /**
99
     * Set the value of Date de mise à jour (la date doit être supérieure à la date de création).
100
     *
101
     * @param Datetime|string updated Format d-m-Y H:i:s
102
     *
103
     * @return self
104
     */
105
    public function setUpdated($updated = null)
106
    {
107
        if (empty($updated)) {
108
            $this->setUpdated(new Datetime());
109
        } else {
110
            $updated = $updated instanceof Datetime ? $updated : DateTime::createFromFormat('d-m-Y H:i:s', (string) $updated);
111
        }
112
113
        if ($updated > $this->created) {
114
            $this->updated = $updated;
115
        }
116
117
        return $this;
118
    }
119
120
    /**
121
     * Get the value of Date de fin.
122
     *
123
     * @return Datetime|null
124
     */
125
    public function getEnded()
126
    {
127
        return $this->ended;
128
    }
129
130
    /**
131
     * Set the value of Date de fin (la date doit être supérieure à la date de création).
132
     *
133
     * @param Datetime|string ended Format d-m-Y H:i:s
134
     *
135
     * @return self
136
     */
137
    public function setEnded($ended)
138
    {
139
        $ended = $ended instanceof Datetime ? $ended : DateTime::createFromFormat('d-m-Y H:i:s', (string) $ended);
140
141
        if ($ended > $this->created) {
142
            $this->ended = $ended;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ended can also be of type false. However, the property $ended is declared as type object<DateTime>. 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...
143
            $this->setUpdated();
144
        }
145
146
        return $this;
147
    }
148
149
    /**
150
     * Retourne vrai si l'engagement est terminée.
151
     *
152
     * @return bool
153
     */
154
    public function isEnded()
155
    {
156
        return !empty($this->ended);
157
    }
158
159
    /**
160
     * Get the value of Evenements particuliers de l'engagement.
161
     *
162
     * @return SDIS62\Core\Ops\Entity\Evenement[]
163
     */
164
    public function getEvenements()
165
    {
166
        if (count($this->evenements) == 0) {
167
            return [];
168
        }
169
170
        $evenements = $this->evenements->toArray();
0 ignored issues
show
Bug introduced by
The method toArray cannot be called on $this->evenements (of type array<integer,object<SDI...\Ops\Entity\Evenement>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
171
172
        @usort($evenements, function ($a, $b) {
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
173
            return $a->getDate()->format('U') < $b->getDate()->format('U') ? -1 : 1;
174
        });
175
176
        return $evenements;
177
    }
178
179
    /**
180
     * Ajoute un evenement à l'engagement.
181
     *
182
     * @param SDIS62\Core\Ops\Entity\Evenement $evenement
183
     *
184
     * @return self
185
     */
186
    public function addEvenement(Evenement $evenement)
187
    {
188
        $this->evenements[] = $evenement;
189
190
        $this->setUpdated();
191
192
        return $this;
193
    }
194
195
    /**
196
     * Get the value of Intervention.
197
     *
198
     * @return SDIS62\Core\Ops\Entity\Intervention
199
     */
200
    public function getIntervention()
201
    {
202
        return $this->intervention;
203
    }
204
}
205