Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 95
0.00% covered (danger)
0.00%
0 / 21
CRAP
0.00% covered (danger)
0.00%
0 / 1
TaskElement
0.00% covered (danger)
0.00%
0 / 95
0.00% covered (danger)
0.00%
0 / 21
3540
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getMedia
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addMedia
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPriority
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setPriority
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getTo
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 isToAccount
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 isToGroup
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 isCCAccount
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 isCCGroup
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 addTo
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 addGroupTo
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 addAccountTo
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 getCC
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 addCC
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 addGroupCC
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 addAccountCC
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 getStatus
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setStatus
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 toArray
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   Modules\Tasks\Models
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace Modules\Tasks\Models;
16
17use Modules\Admin\Models\Account;
18use Modules\Admin\Models\Group;
19use Modules\Admin\Models\NullAccount;
20use Modules\Media\Models\Media;
21use phpOMS\Stdlib\Base\Exception\InvalidEnumValue;
22
23/**
24 * Task element class.
25 *
26 * @package Modules\Tasks\Models
27 * @license OMS License 2.0
28 * @link    https://jingga.app
29 * @since   1.0.0
30 */
31class TaskElement implements \JsonSerializable
32{
33    /**
34     * Id.
35     *
36     * @var int
37     * @since 1.0.0
38     */
39    public int $id = 0;
40
41    /**
42     * Description.
43     *
44     * @var string
45     * @since 1.0.0
46     */
47    public string $description = '';
48
49    /**
50     * Description raw.
51     *
52     * @var string
53     * @since 1.0.0
54     */
55    public string $descriptionRaw = '';
56
57    /**
58     * Task.
59     *
60     * @var int
61     * @since 1.0.0
62     */
63    public int $task = 0;
64
65    /**
66     * Creator.
67     *
68     * @var Account
69     * @since 1.0.0
70     */
71    public Account $createdBy;
72
73    /**
74     * Created.
75     *
76     * @var \DateTimeImmutable
77     * @since 1.0.0
78     */
79    public \DateTimeImmutable $createdAt;
80
81    /**
82     * Status.
83     *
84     * @var int
85     * @since 1.0.0
86     */
87    public int $status = TaskStatus::OPEN;
88
89    /**
90     * Due.
91     *
92     * @var null|\DateTime
93     * @since 1.0.0
94     */
95    public ?\DateTime $due = null;
96
97    /**
98     * Priority
99     *
100     * @var int
101     * @since 1.0.0
102     */
103    public int $priority = TaskPriority::NONE;
104
105    /**
106     * Media.
107     *
108     * @var Media[]
109     * @since 1.0.0
110     */
111    public array $media = [];
112
113    /**
114     * Accounts who received this task element.
115     *
116     * @var AccountRelation[]
117     * @since 1.0.0
118     */
119    public array $accRelation = [];
120
121    /**
122     * Groups who received this task element.
123     *
124     * @var GroupRelation[]
125     * @since 1.0.0
126     */
127    public array $grpRelation = [];
128
129    /**
130     * Constructor.
131     *
132     * @since 1.0.0
133     */
134    public function __construct()
135    {
136        $this->due = new \DateTime('now');
137        $this->due->modify('+1 day');
138        $this->createdAt = new \DateTimeImmutable('now');
139        $this->createdBy = new NullAccount();
140    }
141
142    /**
143     * Get all media
144     *
145     * @return Media[]
146     *
147     * @since 1.0.0
148     */
149    public function getMedia() : array
150    {
151        return $this->media;
152    }
153
154    /**
155     * Add media
156     *
157     * @param Media $media Media to add
158     *
159     * @return void
160     *
161     * @since 1.0.0
162     */
163    public function addMedia(Media $media) : void
164    {
165        $this->media[] = $media;
166    }
167
168    /**
169     * Get priority
170     *
171     * @return int
172     *
173     * @since 1.0.0
174     */
175    public function getPriority() : int
176    {
177        return $this->priority;
178    }
179
180    /**
181     * Set priority
182     *
183     * @param int $priority Task priority
184     *
185     * @return void
186     *
187     * @throws InvalidEnumValue
188     *
189     * @since 1.0.0
190     */
191    public function setPriority(int $priority) : void
192    {
193        if (!TaskPriority::isValidValue($priority)) {
194            throw new InvalidEnumValue((string) $priority);
195        }
196
197        $this->priority = $priority;
198    }
199
200    /**
201     * Get to
202     *
203     * @return RelationAbstract[]
204     *
205     * @since 1.0.0
206     */
207    public function getTo() : array
208    {
209        $to = [];
210
211        foreach ($this->accRelation as $acc) {
212            if ($acc->getDuty() === DutyType::TO) {
213                $to[] = $acc;
214            }
215        }
216
217        foreach ($this->grpRelation as $grp) {
218            if ($grp->getDuty() === DutyType::TO) {
219                $to[] = $grp;
220            }
221        }
222
223        return $to;
224    }
225
226    /**
227     * Check if user is in to
228     *
229     * @param int $id User id
230     *
231     * @return bool
232     *
233     * @since 1.0.0
234     */
235    public function isToAccount(int $id) : bool
236    {
237        foreach ($this->accRelation as $acc) {
238            if ($acc->getDuty() === DutyType::TO
239                && $acc->getRelation()->id === $id
240            ) {
241                return true;
242            }
243        }
244
245        return false;
246    }
247
248    /**
249     * Check if group is in to
250     *
251     * @param int $id Group id
252     *
253     * @return bool
254     *
255     * @since 1.0.0
256     */
257    public function isToGroup(int $id) : bool
258    {
259        foreach ($this->grpRelation as $grp) {
260            if ($grp->getDuty() === DutyType::TO
261                && $grp->getRelation()->id === $id
262            ) {
263                return true;
264            }
265        }
266
267        return false;
268    }
269
270    /**
271     * Check if user is in cc
272     *
273     * @param int $id User id
274     *
275     * @return bool
276     *
277     * @since 1.0.0
278     */
279    public function isCCAccount(int $id) : bool
280    {
281        foreach ($this->accRelation as $acc) {
282            if ($acc->getDuty() === DutyType::CC
283                && $acc->getRelation()->id === $id
284            ) {
285                return true;
286            }
287        }
288
289        return false;
290    }
291
292    /**
293     * Check if group is in cc
294     *
295     * @param int $id Group id
296     *
297     * @return bool
298     *
299     * @since 1.0.0
300     */
301    public function isCCGroup(int $id) : bool
302    {
303        foreach ($this->grpRelation as $grp) {
304            if ($grp->getDuty() === DutyType::CC
305                && $grp->getRelation()->id === $id
306            ) {
307                return true;
308            }
309        }
310
311        return false;
312    }
313
314    /**
315     * Add to
316     *
317     * @param Group|Account $to To
318     *
319     * @return void
320     *
321     * @since 1.0.0
322     */
323    public function addTo($to) : void
324    {
325        if ($to instanceof Group) {
326            $this->addGroupTo($to);
327        } elseif (($to instanceof Account)) {
328            $this->addAccountTo($to);
329        }
330    }
331
332    /**
333     * Add group as to
334     *
335     * @param Group $group Group
336     *
337     * @return void
338     *
339     * @since 1.0.0
340     */
341    public function addGroupTo(Group $group) : void
342    {
343        $groupId = $group->id;
344
345        foreach ($this->grpRelation as $grp) {
346            $grpId = $grp->getRelation()->id;
347
348            if ($grpId === $groupId && $grp->getDuty() === DutyType::TO) {
349                return;
350            }
351        }
352
353        $this->grpRelation[] = new GroupRelation($group, DutyType::TO);
354    }
355
356    /**
357     * Add account as to
358     *
359     * @param Account $account Account
360     *
361     * @return void
362     *
363     * @since 1.0.0
364     */
365    public function addAccountTo(Account $account) : void
366    {
367        $accountId = $account->id;
368
369        foreach ($this->accRelation as $acc) {
370            $accId = $acc->getRelation()->id;
371
372            if ($accId === $accountId && $acc->getDuty() === DutyType::TO) {
373                return;
374            }
375        }
376
377        $this->accRelation[] = new AccountRelation($account, DutyType::TO);
378    }
379
380    /**
381     * Get cc
382     *
383     * @return RelationAbstract[]
384     *
385     * @since 1.0.0
386     */
387    public function getCC() : array
388    {
389        $cc = [];
390
391        foreach ($this->accRelation as $acc) {
392            if ($acc->getDuty() === DutyType::CC) {
393                $cc[] = $acc;
394            }
395        }
396
397        foreach ($this->grpRelation as $grp) {
398            if ($grp->getDuty() === DutyType::CC) {
399                $cc[] = $grp;
400            }
401        }
402
403        return $cc;
404    }
405
406    /**
407     * Add cc
408     *
409     * @param Group|Account $cc CC
410     *
411     * @return void
412     *
413     * @since 1.0.0
414     */
415    public function addCC($cc) : void
416    {
417        if ($cc instanceof Group) {
418            $this->addGroupCC($cc);
419        } elseif (($cc instanceof Account)) {
420            $this->addAccountCC($cc);
421        }
422    }
423
424    /**
425     * Add group as cc
426     *
427     * @param Group $group Group
428     *
429     * @return void
430     *
431     * @since 1.0.0
432     */
433    public function addGroupCC(Group $group) : void
434    {
435        $groupId = $group->id;
436
437        foreach ($this->grpRelation as $grp) {
438            $grpId = $grp->getRelation()->id;
439
440            if ($grpId === $groupId && $grp->getDuty() === DutyType::CC) {
441                return;
442            }
443        }
444
445        $this->grpRelation[] = new GroupRelation($group, DutyType::CC);
446    }
447
448    /**
449     * Add account as cc
450     *
451     * @param Account $account Account
452     *
453     * @return void
454     *
455     * @since 1.0.0
456     */
457    public function addAccountCC(Account $account) : void
458    {
459        $accountId = $account->id;
460
461        foreach ($this->accRelation as $acc) {
462            $accId = $acc->getRelation()->id;
463
464            if ($accId === $accountId && $acc->getDuty() === DutyType::CC) {
465                return;
466            }
467        }
468
469        $this->accRelation[] = new AccountRelation($account, DutyType::CC);
470    }
471
472    /**
473     * Get status
474     *
475     * @return int
476     *
477     * @since 1.0.0
478     */
479    public function getStatus() : int
480    {
481        return $this->status;
482    }
483
484    /**
485     * Set Status
486     *
487     * @param int $status Task element status
488     *
489     * @return void
490     *
491     * @throws InvalidEnumValue
492     *
493     * @since 1.0.0
494     */
495    public function setStatus(int $status) : void
496    {
497        if (!TaskStatus::isValidValue($status)) {
498            throw new InvalidEnumValue((string) $status);
499        }
500
501        $this->status = $status;
502    }
503
504    /**
505     * {@inheritdoc}
506     */
507    public function toArray() : array
508    {
509        return [
510            'id'             => $this->id,
511            'task'           => $this->task,
512            'createdBy'      => $this->createdBy,
513            'createdAt'      => $this->createdAt,
514            'description'    => $this->description,
515            'descriptionRaw' => $this->descriptionRaw,
516            'status'         => $this->status,
517            'to'             => $this->getTo(),
518            'cc'             => $this->getCC(),
519            'due'            => $this->due,
520        ];
521    }
522
523    /**
524     * {@inheritdoc}
525     */
526    public function jsonSerialize() : mixed
527    {
528        return $this->toArray();
529    }
530}