Passed
Push — feature/VSVGVQ-20 ( 985c44...f6eeee )
by steven
02:52
created

CompanyEntity   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 117
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getTranslatedAliasEntities() 0 3 1
A setUserEntity() 0 3 1
A toCompany() 0 16 1
A fromCompany() 0 18 1
A getName() 0 3 1
A getUserEntity() 0 3 1
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\User\Repositories\Entities\UserEntity;
15
16
/**
17
 * @ORM\Entity()
18
 * @ORM\Table(name="company")
19
 */
20
class CompanyEntity extends Entity
21
{
22
    /**
23
     * @var string
24
     * @ORM\Column(type="string", length=255, nullable=false)
25
     */
26
    private $name;
27
28
    /**
29
     * @var Collection
30
     * @ORM\OneToMany(targetEntity="TranslatedAliasEntity", mappedBy="companyEntity", cascade={"all"})
31
     *
32
     */
33
    private $translatedAliasEntities;
34
35
    /**
36
     * @var UserEntity
37
     * @ORM\ManyToOne(targetEntity="VSV\GVQ_API\User\Repositories\Entities\UserEntity", fetch="EAGER")
38
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
39
     */
40
    private $userEntity;
41
42
    /**
43
     * @param string $id
44
     * @param string $name
45
     * @param Collection $translatedAliasEntities
46
     * @param UserEntity $user
47
     */
48
    public function __construct(
49
        string $id,
50
        string $name,
51
        Collection $translatedAliasEntities,
52
        UserEntity $user
53
    ) {
54
        parent::__construct($id);
55
56
        $this->name = $name;
57
        $this->translatedAliasEntities = $translatedAliasEntities;
58
        $this->userEntity = $user;
59
    }
60
61
    /**
62
     * @param Company $company
63
     * @return CompanyEntity
64
     */
65
    public static function fromCompany(Company $company): CompanyEntity
66
    {
67
        /** @var TranslatedAliasEntity[] $translatedAliasEntities */
68
        $translatedAliasEntities = array_map(
69
            function (TranslatedAlias $translatedAlias) {
70
                return TranslatedAliasEntity::fromTranslatedAlias($translatedAlias);
71
            },
72
            $company->getTranslatedAliases()->toArray()
73
        );
74
75
        $companyEntity = new CompanyEntity(
76
            $company->getId()->toString(),
77
            $company->getName()->toNative(),
78
            new ArrayCollection($translatedAliasEntities),
79
            UserEntity::fromUser($company->getUser())
80
        );
81
82
        return $companyEntity;
83
    }
84
85
    /**
86
     * @return Company
87
     */
88
    public function toCompany(): Company
89
    {
90
        $translatedAliases = new TranslatedAliases(
91
            ...array_map(
92
                function (TranslatedAliasEntity $translatedAliasEntity) {
93
                    return $translatedAliasEntity->toTranslatedAlias();
94
                },
95
                $this->getTranslatedAliasEntities()->toArray()
96
            )
97
        );
98
99
        return new Company(
100
            Uuid::fromString($this->getId()),
101
            new NotEmptyString($this->getName()),
102
            $translatedAliases,
103
            $this->getUserEntity()->toUser()
104
        );
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getName(): string
111
    {
112
        return $this->name;
113
    }
114
115
    /**
116
     * @return Collection
117
     */
118
    public function getTranslatedAliasEntities(): Collection
119
    {
120
        return $this->translatedAliasEntities;
121
    }
122
123
    /**
124
     * @return UserEntity
125
     */
126
    public function getUserEntity(): UserEntity
127
    {
128
        return $this->userEntity;
129
    }
130
131
    /**
132
     * @param UserEntity $userEntity
133
     */
134
    public function setUserEntity(UserEntity $userEntity): void
135
    {
136
        $this->userEntity = $userEntity;
137
    }
138
}
139