Completed
Push — master ( a5d24e...028b40 )
by Gabriel
02:43
created

GroupEntityTrait::getUsers()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
namespace Sinergi\Users\Group;
4
5
use DateTime;
6
use Sinergi\Users\User\UserEntityInterface;
7
use Sinergi\Users\User\UserRepositoryInterface;
8
9
trait GroupEntityTrait
10
{
11
    protected $id;
12
    /** @var UserEntityInterface[] */
13
    protected $users;
14
    protected $creationDatetime;
15
    protected $modificationDatetime;
16
    /** @var UserRepositoryInterface */
17
    protected $userRepository;
18
19
    public function __construct(UserRepositoryInterface $userRepository = null)
0 ignored issues
show
Unused Code introduced by
The parameter $userRepository is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20
    {
21
        $this->setCreationDatetime(new DateTime());
22
        $this->setModificationDatetime(new DateTime());
23
    }
24
25
    /** @return int */
26
    public function getId()
27
    {
28
        return $this->id;
29
    }
30
31
    public function setId(int $id): GroupEntityInterface
32
    {
33
        $this->id = $id;
34
        return $this;
35
    }
36
37
    /** @return UserEntityInterface[] */
38
    public function getUsers()
39
    {
40
        if (is_array($this->users)) {
41
            return $this->users;
42
        } elseif (!$this->userRepository) {
43
            throw new \Exception('Cannot fetch users without user repository');
44
        }
45
        return $this->users = $this->userRepository->findByGroupId($this->getId());
46
    }
47
48
    public function getCreationDatetime(): DateTime
49
    {
50
        return $this->creationDatetime;
51
    }
52
53
    public function setCreationDatetime(DateTime $creationDatetime): GroupEntityInterface
54
    {
55
        $this->creationDatetime = $creationDatetime;
56
        return $this;
57
    }
58
59
    public function getModificationDatetime(): DateTime
60
    {
61
        return $this->modificationDatetime;
62
    }
63
64
    public function setModificationDatetime(DateTime $modificationDatetime): GroupEntityInterface
65
    {
66
        $this->modificationDatetime = $modificationDatetime;
67
        return $this;
68
    }
69
70
    public function setUserRepository(UserRepositoryInterface $userRepository): GroupEntityInterface
71
    {
72
        $this->userRepository = $userRepository;
73
        return $this;
74
    }
75
76
    public function toArray(): array
77
    {
78
        return [
79
            'id' => $this->getId(),
80
            'creationDatetime' => $this->getCreationDatetime()->format('Y-m-d H:i:s'),
81
            'modificationDatetime' => $this->getModificationDatetime()->format('Y-m-d H:i:s'),
82
        ];
83
    }
84
85
    public function jsonSerialize()
86
    {
87
        return $this->toArray();
88
    }
89
}
90