1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\Company\Repositories; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Ramsey\Uuid\UuidInterface; |
7
|
|
|
use VSV\GVQ_API\Common\Repositories\AbstractDoctrineRepository; |
8
|
|
|
use VSV\GVQ_API\Company\Models\Company; |
9
|
|
|
use VSV\GVQ_API\Company\Repositories\Entities\CompanyEntity; |
10
|
|
|
use VSV\GVQ_API\User\Repositories\Entities\UserEntity; |
11
|
|
|
|
12
|
|
|
class CompanyDoctrineRepository extends AbstractDoctrineRepository implements CompanyRepository |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @inheritdoc |
16
|
|
|
*/ |
17
|
|
|
public function getRepositoryName(): string |
18
|
|
|
{ |
19
|
|
|
return CompanyEntity::class; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Company $company |
24
|
|
|
*/ |
25
|
|
|
public function save(Company $company): void |
26
|
|
|
{ |
27
|
|
|
$companyEntity = CompanyEntity::fromCompany($company); |
28
|
|
|
|
29
|
|
|
/** @var UserEntity $userEntity */ |
30
|
|
|
$userEntity = $this->entityManager->find( |
31
|
|
|
UserEntity::class, |
32
|
|
|
$companyEntity->getUserEntity()->getId() |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
if ($userEntity == null) { |
36
|
|
|
throw new InvalidArgumentException( |
37
|
|
|
'User with id: '. |
38
|
|
|
$companyEntity->getUserEntity()->getId(). |
39
|
|
|
' not found.' |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$companyEntity->setUserEntity($userEntity); |
44
|
|
|
|
45
|
|
|
$this->entityManager->persist($companyEntity); |
46
|
|
|
$this->entityManager->flush(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param UuidInterface $id |
51
|
|
|
* @return Company|null |
52
|
|
|
*/ |
53
|
|
|
public function getById(UuidInterface $id): ?Company |
54
|
|
|
{ |
55
|
|
|
/** @var CompanyEntity|null $companyEntity */ |
56
|
|
|
$companyEntity = $this->objectRepository->findOneBy( |
57
|
|
|
[ |
58
|
|
|
'id' => $id, |
59
|
|
|
] |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return $companyEntity ? $companyEntity->toCompany() : null; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|