Passed
Pull Request — master (#16)
by steven
04:56
created

CompanyEntity::getNumberOfEmployees()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Company\Repositories\Entities;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
use Ramsey\Uuid\Uuid;
9
use VSV\GVQ_API\Common\Repositories\Entities\Entity;
10
use VSV\GVQ_API\Common\ValueObjects\NotEmptyString;
11
use VSV\GVQ_API\Company\Models\Company;
12
use VSV\GVQ_API\Company\Models\TranslatedAlias;
13
use VSV\GVQ_API\Company\Models\TranslatedAliases;
14
use VSV\GVQ_API\Company\ValueObjects\PositiveNumber;
15
use VSV\GVQ_API\User\Repositories\Entities\UserEntity;
16
17
/**
18
 * @ORM\Entity()
19
 * @ORM\Table(name="company")
20
 */
21
class CompanyEntity extends Entity
22
{
23
    /**
24
     * @var string
25
     * @ORM\Column(type="string", length=255, nullable=false)
26
     */
27
    private $name;
28
29
    /**
30
     * @var int
31
     * @ORM\Column(type="integer", nullable=false)
32
     */
33
    private $numberOfEmployees;
34
35
    /**
36
     * @var Collection
37
     * @ORM\OneToMany(targetEntity="TranslatedAliasEntity", mappedBy="companyEntity", cascade={"all"})
38
     *
39
     */
40
    private $translatedAliasEntities;
41
42
    /**
43
     * @var UserEntity
44
     * @ORM\ManyToOne(targetEntity="VSV\GVQ_API\User\Repositories\Entities\UserEntity", fetch="EAGER")
45
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
46
     */
47
    private $userEntity;
48
49
    /**
50
     * @param string $id
51
     * @param string $name
52
     * @param int $numberOfEmployees
53
     * @param Collection $translatedAliasEntities
54
     * @param UserEntity $user
55
     */
56
    public function __construct(
57
        string $id,
58
        string $name,
59
        int $numberOfEmployees,
60
        Collection $translatedAliasEntities,
61
        UserEntity $user
62
    ) {
63
        parent::__construct($id);
64
65
        $this->name = $name;
66
        $this->numberOfEmployees = $numberOfEmployees;
67
        $this->translatedAliasEntities = $translatedAliasEntities;
68
        $this->userEntity = $user;
69
70
        foreach ($translatedAliasEntities as $translatedAliasEntity) {
71
            $translatedAliasEntity->setCompanyEntity($this);
72
        }
73
    }
74
75
    /**
76
     * @param Company $company
77
     * @return CompanyEntity
78
     */
79
    public static function fromCompany(Company $company): CompanyEntity
80
    {
81
        /** @var TranslatedAliasEntity[] $translatedAliasEntities */
82
        $translatedAliasEntities = array_map(
83
            function (TranslatedAlias $translatedAlias) {
84
                return TranslatedAliasEntity::fromTranslatedAlias($translatedAlias);
85
            },
86
            $company->getTranslatedAliases()->toArray()
87
        );
88
89
        $companyEntity = new CompanyEntity(
90
            $company->getId()->toString(),
91
            $company->getName()->toNative(),
92
            $company->getNumberOfEmployees()->toNative(),
93
            new ArrayCollection($translatedAliasEntities),
94
            UserEntity::fromUser($company->getUser())
95
        );
96
97
        return $companyEntity;
98
    }
99
100
    /**
101
     * @return Company
102
     */
103
    public function toCompany(): Company
104
    {
105
        $translatedAliases = new TranslatedAliases(
106
            ...array_map(
107
                function (TranslatedAliasEntity $translatedAliasEntity) {
108
                    return $translatedAliasEntity->toTranslatedAlias();
109
                },
110
                $this->getTranslatedAliasEntities()->toArray()
111
            )
112
        );
113
114
        return new Company(
115
            Uuid::fromString($this->getId()),
116
            new NotEmptyString($this->getName()),
117
            new PositiveNumber($this->getNumberOfEmployees()),
118
            $translatedAliases,
119
            $this->getUserEntity()->toUser()
120
        );
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getName(): string
127
    {
128
        return $this->name;
129
    }
130
131
    /**
132
     * @return int
133
     */
134
    public function getNumberOfEmployees(): int
135
    {
136
        return $this->numberOfEmployees;
137
    }
138
139
    /**
140
     * @return Collection
141
     */
142
    public function getTranslatedAliasEntities(): Collection
143
    {
144
        return $this->translatedAliasEntities;
145
    }
146
147
    /**
148
     * @return UserEntity
149
     */
150
    public function getUserEntity(): UserEntity
151
    {
152
        return $this->userEntity;
153
    }
154
155
    /**
156
     * @param UserEntity $userEntity
157
     */
158
    public function setUserEntity(UserEntity $userEntity): void
159
    {
160
        $this->userEntity = $userEntity;
161
    }
162
}
163