Issues (141)

server/Application/Model/AbstractModel.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Model;
6
7
use Application\Acl\Acl;
8
use Application\Api\Input\Operator\SearchOperatorType;
9
use Application\Api\Input\Sorting\Random;
10
use Cake\Chronos\Chronos;
0 ignored issues
show
The type Cake\Chronos\Chronos was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Doctrine\ORM\Event\PreUpdateEventArgs;
12
use Doctrine\ORM\Mapping as ORM;
13
use Ecodev\Felix\Api\Output\PermissionsType;
14
use Ecodev\Felix\Model\HasOwner;
15
use Ecodev\Felix\Model\Model;
16
use GraphQL\Doctrine\Attribute as API;
17
18
/**
19
 * Base class for all objects stored in database.
20
 *
21
 * It includes an automatic mechanism to timestamp objects with date and user.
22
 */
23
#[ORM\Index(name: 'creation_date', columns: ['creation_date'])]
24
#[ORM\Index(name: 'update_date', columns: ['update_date'])]
25
#[API\Filter(field: 'custom', operator: SearchOperatorType::class, type: 'string')]
26
#[API\Sorting(Random::class)]
27
#[ORM\MappedSuperclass]
28
#[ORM\HasLifecycleCallbacks]
29
abstract class AbstractModel implements HasOwner, Model
30
{
31
    #[ORM\Column(type: 'integer')]
32
    #[ORM\Id]
33
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
34
    private ?int $id = null;
35
36
    #[ORM\Column(type: 'datetime', nullable: true)]
37
    private ?Chronos $creationDate = null;
38
39
    #[ORM\Column(type: 'datetime', nullable: true)]
40
    private ?Chronos $updateDate = null;
41
42
    #[ORM\ManyToOne(targetEntity: User::class)]
43
    private ?User $creator = null;
44
45
    #[ORM\ManyToOne(targetEntity: User::class)]
46
    private ?User $owner = null;
47
48
    #[ORM\ManyToOne(targetEntity: User::class)]
49
    private ?User $updater = null;
50
51
    /**
52
     * Get id.
53
     */
54 102
    public function getId(): ?int
55
    {
56 102
        return $this->id;
57
    }
58
59
    /**
60
     * Set creation date.
61
     */
62 24
    private function setCreationDate(Chronos $creationDate): void
63
    {
64 24
        $this->creationDate = $creationDate;
65
    }
66
67
    /**
68
     * Get creation date.
69
     */
70
    public function getCreationDate(): ?Chronos
71
    {
72
        return $this->creationDate;
73
    }
74
75
    /**
76
     * Set update date.
77
     */
78 29
    private function setUpdateDate(Chronos $updateDate): void
79
    {
80 29
        $this->updateDate = $updateDate;
81
    }
82
83
    /**
84
     * Get update date.
85
     */
86
    public function getUpdateDate(): ?Chronos
87
    {
88
        return $this->updateDate;
89
    }
90
91
    /**
92
     * Set creator.
93
     */
94 24
    private function setCreator(?User $creator): void
95
    {
96 24
        $this->creator = $creator;
97
    }
98
99
    /**
100
     * Get creator.
101
     */
102 2
    public function getCreator(): ?User
103
    {
104 2
        return $this->creator;
105
    }
106
107
    /**
108
     * Set owner.
109
     */
110 25
    #[API\Exclude]
111
    public function setOwner(?User $owner): void
112
    {
113 25
        $this->owner = $owner;
114
    }
115
116
    /**
117
     * Get owner.
118
     */
119 9
    public function getOwner(): ?User
120
    {
121 9
        return $this->owner;
122
    }
123
124
    /**
125
     * Set updater.
126
     */
127 29
    private function setUpdater(?User $updater): void
128
    {
129 29
        $this->updater = $updater;
130
    }
131
132
    /**
133
     * Get updater.
134
     */
135 2
    public function getUpdater(): ?User
136
    {
137 2
        return $this->updater;
138
    }
139
140
    /**
141
     * Automatically called by Doctrine when the object is saved for the first time.
142
     */
143 24
    #[ORM\PrePersist]
144
    public function timestampCreation(): void
145
    {
146 24
        $now = new Chronos();
147 24
        $user = User::getCurrent();
148 24
        $this->setCreationDate($now);
149 24
        $this->setUpdateDate($now);
150 24
        $this->setCreator($user);
151 24
        $this->setUpdater($user);
152
153 24
        $this->setOwner(User::getCurrent());
154
    }
155
156
    /**
157
     * Automatically called by Doctrine when the object is updated.
158
     */
159 10
    #[ORM\PreUpdate]
160
    public function timestampUpdate(PreUpdateEventArgs $event): void
161
    {
162 10
        if (!$event->getEntityChangeSet()) {
163 2
            return;
164
        }
165
166 8
        $this->setUpdateDate(new Chronos());
167 8
        $this->setUpdater(User::getCurrent());
168
    }
169
170
    /**
171
     * Get permissions on this object for the current user.
172
     */
173 2
    #[API\Field(type: PermissionsType::class)]
174
    public function getPermissions(): array
175
    {
176 2
        $acl = new Acl();
177
178 2
        return [
179 2
            'create' => $acl->isCurrentUserAllowed($this, 'create'),
180 2
            'read' => $acl->isCurrentUserAllowed($this, 'read'),
181 2
            'update' => $acl->isCurrentUserAllowed($this, 'update'),
182 2
            'delete' => $acl->isCurrentUserAllowed($this, 'delete'),
183 2
        ];
184
    }
185
}
186