Passed
Push — feature/VSVGVQ-51 ( 4c3c8b )
by steven
03:08
created

RegistrationDoctrineRepository::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Registration\Repositories;
4
5
use VSV\GVQ_API\Common\Repositories\AbstractDoctrineRepository;
6
use VSV\GVQ_API\Registration\Models\Registration;
7
use VSV\GVQ_API\Registration\Repositories\Entities\RegistrationEntity;
8
use VSV\GVQ_API\User\Repositories\Entities\UserEntity;
9
10
class RegistrationDoctrineRepository extends AbstractDoctrineRepository implements RegistrationRepository
11
{
12
    /**
13
     * @inheritdoc
14
     */
15
    protected function getRepositoryName(): string
16
    {
17
        return RegistrationEntity::class;
18
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function save(Registration $registration): void
24
    {
25
        $registrationEntity = RegistrationEntity::fromRegistration($registration);
26
27
        /** @var UserEntity $userEntity */
28
        $userEntity = $this->entityManager->find(
29
            UserEntity::class,
30
            $registrationEntity->getUserEntity()->getId()
31
        );
32
33
        if ($userEntity == null) {
34
            throw new \InvalidArgumentException(
35
                'User with id: '.$registrationEntity->getUserEntity()->getId().' not found.'
36
            );
37
        }
38
39
        $registrationEntity->setUserEntity($userEntity);
40
41
        $this->entityManager->persist($registrationEntity);
42
        $this->entityManager->flush();
43
    }
44
45
    /**
46
     * @param string $hashCode
47
     * @return Registration
48
     */
49
    public function getByHashCode(string $hashCode): ?Registration
50
    {
51
        /** @var RegistrationEntity|null $registrationEntity */
52
        $registrationEntity = $this->objectRepository->findOneBy(
53
            [
54
                'hashCode' => $hashCode,
55
            ]
56
        );
57
58
        return $registrationEntity ? $registrationEntity->toRegistration() : null;
59
    }
60
}
61