Passed
Pull Request — master (#2073)
by Tarmo
09:32
created

RestResource::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Rest/RestResource.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Rest;
10
11
use App\DTO\RestDtoInterface;
12
use App\Repository\Interfaces\BaseRepositoryInterface;
13
use App\Rest\Interfaces\RestResourceInterface;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
use Symfony\Contracts\Service\Attribute\Required;
16
use UnexpectedValueException;
17
use function array_keys;
18
use function sprintf;
19
20
/**
21
 * Class RestResource
22
 *
23
 * @package App\Rest
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 */
26
abstract class RestResource implements RestResourceInterface
27
{
28
    use Traits\RestResourceBaseMethods;
29
30
    private ValidatorInterface $validator;
31
    private string $dtoClass = '';
32
33 780
    public function __construct(
34
        protected readonly BaseRepositoryInterface $repository,
35
    ) {
36
    }
37
38 91
    public function getSerializerContext(): array
39
    {
40 91
        return [];
41
    }
42
43 669
    public function getRepository(): BaseRepositoryInterface
44
    {
45 669
        $exception = new UnexpectedValueException('Repository not set on constructor');
46
47 669
        return property_exists($this, 'repository') ? $this->repository ?? throw $exception : throw $exception;
48
    }
49
50 64
    public function getValidator(): ValidatorInterface
51
    {
52 64
        return $this->validator;
53
    }
54
55 777
    #[Required]
56
    public function setValidator(ValidatorInterface $validator): self
57
    {
58 777
        $this->validator = $validator;
59
60 777
        return $this;
61
    }
62
63 2
    public function getDtoClass(): string
64
    {
65 2
        if ($this->dtoClass === '') {
66 1
            $message = sprintf(
67
                'DTO class not specified for \'%s\' resource',
68
                static::class
69
            );
70
71 1
            throw new UnexpectedValueException($message);
72
        }
73
74 1
        return $this->dtoClass;
75
    }
76
77 2
    public function setDtoClass(string $dtoClass): RestResourceInterface
78
    {
79 2
        $this->dtoClass = $dtoClass;
80
81 2
        return $this;
82
    }
83
84 122
    public function getEntityName(): string
85
    {
86 122
        return $this->getRepository()->getEntityName();
87
    }
88
89 264
    public function getReference(string $id): ?object
90
    {
91 264
        return $this->getRepository()->getReference($id);
92
    }
93
94 1
    public function getAssociations(): array
95
    {
96 1
        return array_keys($this->getRepository()->getAssociations());
97
    }
98
99 28
    public function getDtoForEntity(
100
        string $id,
101
        string $dtoClass,
102
        RestDtoInterface $dto,
103
        ?bool $patch = null
104
    ): RestDtoInterface {
105 28
        $patch ??= false;
106
107
        // Fetch entity
108 28
        $entity = $this->getEntity($id);
109
110
        /**
111
         * Create new instance of DTO and load entity to that.
112
         *
113
         * @var RestDtoInterface $restDto
114
         * @var class-string<RestDtoInterface> $dtoClass
115
         */
116 27
        $restDto = (new $dtoClass())
117 27
            ->setId($id);
118
119 27
        if ($patch === true) {
120 12
            $restDto->load($entity);
121
        }
122
123 27
        $restDto->patch($dto);
124
125 27
        return $restDto;
126
    }
127
}
128