Completed
Push — master ( 0e1368...c48087 )
by Sam
05:46
created

AbstractModel::getPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Model;
6
7
use Application\Acl\Acl;
8
use Application\Utility;
9
use Cake\Chronos\Chronos;
10
use Doctrine\ORM\Mapping as ORM;
11
use GraphQL\Doctrine\Annotation as API;
12
13
/**
14
 * Base class for all objects stored in database.
15
 *
16
 * It includes an automatic mechanism to timestamp objects with date and user.
17
 *
18
 * @ORM\MappedSuperclass
19
 * @ORM\HasLifecycleCallbacks
20
 * @API\Filters({
21
 *     @API\Filter(field="custom", operator="Application\Api\Input\Operator\SearchOperatorType", type="string"),
22
 * })
23
 */
24
abstract class AbstractModel
25
{
26
    /**
27
     * @var int
28
     *
29
     * @ORM\Column(type="integer", nullable=false)
30
     * @ORM\Id
31
     * @ORM\GeneratedValue(strategy="IDENTITY")
32
     */
33
    private $id;
34
35
    /**
36
     * @var Chronos
37
     *
38
     * @ORM\Column(type="datetime", nullable=true)
39
     */
40
    private $creationDate;
41
42
    /**
43
     * @var Chronos
44
     *
45
     * @ORM\Column(type="datetime", nullable=true)
46
     */
47
    private $updateDate;
48
49
    /**
50
     * @var User
51
     *
52
     * @ORM\ManyToOne(targetEntity="User")
53
     */
54
    private $creator;
55
56
    /**
57
     * @var User
58
     *
59
     * @ORM\ManyToOne(targetEntity="User")
60
     */
61
    private $owner;
62
63
    /**
64
     * @var User
65
     *
66
     * @ORM\ManyToOne(targetEntity="User")
67
     */
68
    private $updater;
69
70
    /**
71
     * Get id
72
     *
73
     * @return null|int
74
     */
75 12
    public function getId(): ?int
76
    {
77 12
        return $this->id;
78
    }
79
80
    /**
81
     * Set creation date
82
     *
83
     * @param Chronos $creationDate
84
     */
85 1
    private function setCreationDate(Chronos $creationDate = null): void
86
    {
87 1
        $this->creationDate = $creationDate;
88 1
    }
89
90
    /**
91
     * Get creation date
92
     *
93
     * @return null|Chronos
94
     */
95
    public function getCreationDate(): ?Chronos
96
    {
97
        return $this->creationDate;
98
    }
99
100
    /**
101
     * Set update date
102
     *
103
     * @param Chronos $updateDate
104
     */
105 3
    private function setUpdateDate(Chronos $updateDate = null): void
106
    {
107 3
        $this->updateDate = $updateDate;
108 3
    }
109
110
    /**
111
     * Get update date
112
     *
113
     * @return null|Chronos
114
     */
115
    public function getUpdateDate(): ?Chronos
116
    {
117
        return $this->updateDate;
118
    }
119
120
    /**
121
     * Set creator
122
     *
123
     * @param User $creator
124
     */
125 1
    private function setCreator(User $creator = null): void
126
    {
127 1
        $this->creator = $creator;
128 1
    }
129
130
    /**
131
     * Get creator
132
     *
133
     * @return null|User
134
     */
135
    public function getCreator(): ?User
136
    {
137
        return $this->creator;
138
    }
139
140
    /**
141
     * Set owner
142
     *
143
     * @API\Exclude
144
     *
145
     * @param User $owner
146
     */
147 1
    public function setOwner(User $owner = null): void
148
    {
149 1
        $this->owner = $owner;
150 1
    }
151
152
    /**
153
     * Get owner
154
     *
155
     * @return null|User
156
     */
157
    public function getOwner(): ?User
158
    {
159
        return $this->owner;
160
    }
161
162
    /**
163
     * Set updater
164
     *
165
     * @param null|User $updater
166
     */
167 3
    private function setUpdater(User $updater = null): void
168
    {
169 3
        $this->updater = $updater;
170 3
    }
171
172
    /**
173
     * Get updater
174
     *
175
     * @return null|User
176
     */
177
    public function getUpdater(): ?User
178
    {
179
        return $this->updater;
180
    }
181
182
    /**
183
     * Automatically called by Doctrine when the object is saved for the first time
184
     *
185
     * @ORM\PrePersist
186
     */
187 1
    public function timestampCreation(): void
188
    {
189 1
        $this->setCreationDate(Utility::getNow());
190 1
        $this->setCreator(User::getCurrent());
191 1
        $this->setOwner(User::getCurrent());
192 1
    }
193
194
    /**
195
     * Automatically called by Doctrine when the object is updated
196
     *
197
     * @ORM\PreUpdate
198
     */
199 3
    public function timestampUpdate(): void
200
    {
201 3
        $this->setUpdateDate(Utility::getNow());
202 3
        $this->setUpdater(User::getCurrent());
203 3
    }
204
205
    /**
206
     * Get permissions on this object for the current user
207
     *
208
     * @API\Field(type="Permissions")
209
     *
210
     * @return array
211
     */
212 1
    public function getPermissions(): array
213
    {
214 1
        $acl = new Acl();
215
216
        return [
217 1
            'create' => $acl->isCurrentUserAllowed($this, 'create'),
218 1
            'read' => $acl->isCurrentUserAllowed($this, 'read'),
219 1
            'update' => $acl->isCurrentUserAllowed($this, 'update'),
220 1
            'delete' => $acl->isCurrentUserAllowed($this, 'delete'),
221
        ];
222
    }
223
}
224