• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemsdk / docker-symfony-api / #74

pending completion
#74

push

DKravtsov
Php 8.2, symfony 6.2, updated RabbitMQ, updated composer dependencies, refactoring.

51 of 51 new or added lines in 44 files covered. (100.0%)

1479 of 2668 relevant lines covered (55.43%)

23.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

65.79
/src/General/Application/Rest/Traits/RestResourceBaseMethods.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\General\Application\Rest\Traits;
6

7
use App\General\Application\DTO\Interfaces\RestDtoInterface;
8
use App\General\Application\Exception\ValidatorException;
9
use App\General\Domain\Entity\Interfaces\EntityInterface;
10
use App\General\Domain\Repository\Interfaces\BaseRepositoryInterface;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
use Symfony\Component\Validator\ConstraintViolationListInterface;
13
use Symfony\Component\Validator\Validator\ValidatorInterface;
14
use Symfony\Contracts\Service\Attribute\Required;
15
use Throwable;
16
use UnexpectedValueException;
17

18
use function array_keys;
19
use function sprintf;
20

21
/**
22
 * Trait RestResourceBaseMethods
23
 *
24
 * @package App\General
25
 */
26
trait RestResourceBaseMethods
27
{
28
    private ValidatorInterface $validator;
29
    private string $dtoClass = '';
30

31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getSerializerContext(): array
35
    {
36
        return [];
39✔
37
    }
38

39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getRepository(): BaseRepositoryInterface
43
    {
44
        $exception = new UnexpectedValueException('Repository not set on constructor');
141✔
45

46
        return property_exists($this, 'repository') ? $this->repository ?? throw $exception : throw $exception;
141✔
47
    }
48

49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getValidator(): ValidatorInterface
53
    {
54
        return $this->validator;
46✔
55
    }
56

57
    /**
58
     * {@inheritdoc}
59
     */
60
    #[Required]
61
    public function setValidator(ValidatorInterface $validator): self
62
    {
63
        $this->validator = $validator;
145✔
64

65
        return $this;
145✔
66
    }
67

68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getDtoClass(): string
72
    {
73
        if ($this->dtoClass === '') {
×
74
            $message = sprintf(
×
75
                'DTO class not specified for \'%s\' resource',
×
76
                static::class
×
77
            );
×
78

79
            throw new UnexpectedValueException($message);
×
80
        }
81

82
        return $this->dtoClass;
×
83
    }
84

85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function setDtoClass(string $dtoClass): self
89
    {
90
        $this->dtoClass = $dtoClass;
×
91

92
        return $this;
×
93
    }
94

95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getEntityName(): string
99
    {
100
        return $this->getRepository()->getEntityName();
39✔
101
    }
102

103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getReference(string $id, ?string $entityManagerName = null): ?object
107
    {
108
        return $this->getRepository()->getReference($id, $entityManagerName);
126✔
109
    }
110

111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function getAssociations(?string $entityManagerName = null): array
115
    {
116
        return array_keys($this->getRepository()->getAssociations($entityManagerName));
×
117
    }
118

119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getDtoForEntity(
123
        string $id,
124
        string $dtoClass,
125
        RestDtoInterface $dto,
126
        ?bool $patch = null,
127
        ?string $entityManagerName = null
128
    ): RestDtoInterface {
129
        $patch ??= false;
12✔
130
        // Fetch entity
131
        $entity = $this->getEntity($id, $entityManagerName);
12✔
132

133
        /**
134
         * Create new instance of DTO and load entity to that.
135
         *
136
         * @var RestDtoInterface $restDto
137
         * @var class-string<RestDtoInterface> $dtoClass
138
         */
139
        $restDto = (new $dtoClass())
12✔
140
            ->setId($id);
12✔
141

142
        if ($patch === true) {
12✔
143
            $restDto->load($entity);
6✔
144
        }
145

146
        $restDto->patch($dto);
12✔
147

148
        return $restDto;
12✔
149
    }
150

151
    /**
152
     * @throws NotFoundHttpException
153
     * @throws Throwable
154
     */
155
    protected function getEntity(string $id, ?string $entityManagerName): EntityInterface
156
    {
157
        $entity = $this->getRepository()->find(id: $id, entityManagerName: $entityManagerName);
16✔
158

159
        if ($entity === null) {
16✔
160
            throw new NotFoundHttpException('Not found');
×
161
        }
162

163
        return $entity;
16✔
164
    }
165

166
    /**
167
     * Helper method to validate given DTO class.
168
     *
169
     * @throws Throwable
170
     */
171
    protected function validateDto(RestDtoInterface $dto, bool $skipValidation): void
172
    {
173
        /** @var ConstraintViolationListInterface|null $errors */
174
        $errors = $skipValidation ? null : $this->getValidator()->validate($dto);
21✔
175

176
        // Oh noes, we have some errors
177
        if ($errors !== null && $errors->count() > 0) {
21✔
178
            throw new ValidatorException($dto::class, $errors);
3✔
179
        }
180
    }
181

182
    /**
183
     * Method to validate specified entity.
184
     *
185
     * @throws Throwable
186
     */
187
    protected function validateEntity(EntityInterface $entity, bool $skipValidation): void
188
    {
189
        $errors = $skipValidation ? null : $this->getValidator()->validate($entity);
139✔
190

191
        // Oh noes, we have some errors
192
        if ($errors !== null && $errors->count() > 0) {
139✔
193
            throw new ValidatorException($entity::class, $errors);
×
194
        }
195
    }
196

197
    /**
198
     * @throws NotFoundHttpException
199
     */
200
    protected function checkThatEntityExists(bool $throwExceptionIfNotFound, ?EntityInterface $entity): void
201
    {
202
        // Entity not found
203
        if ($throwExceptionIfNotFound && $entity === null) {
50✔
204
            throw new NotFoundHttpException('Not found');
×
205
        }
206
    }
207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc