Completed
Push — master ( 9af059...286024 )
by Luc
12s
created

CompanyDoctrineRepository::getById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
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