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

systemsdk / docker-symfony-api / #83

pending completion
#83

push

DKravtsov
Updated composer dependencies, refactoring.

45 of 45 new or added lines in 12 files covered. (100.0%)

1483 of 2844 relevant lines covered (52.14%)

22.41 hits per line

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

64.86
/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 Throwable;
15
use UnexpectedValueException;
16

17
use function array_keys;
18
use function sprintf;
19

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

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

38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getRepository(): BaseRepositoryInterface
42
    {
43
        return $this->repository;
144✔
44
    }
45

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

54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function setValidator(ValidatorInterface $validator): self
58
    {
59
        $this->validator = $validator;
148✔
60

61
        return $this;
148✔
62
    }
63

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

75
            throw new UnexpectedValueException($message);
×
76
        }
77

78
        return $this->dtoClass;
×
79
    }
80

81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function setDtoClass(string $dtoClass): self
85
    {
86
        $this->dtoClass = $dtoClass;
×
87

88
        return $this;
×
89
    }
90

91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getEntityName(): string
95
    {
96
        return $this->getRepository()->getEntityName();
61✔
97
    }
98

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

107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getAssociations(?string $entityManagerName = null): array
111
    {
112
        return array_keys($this->getRepository()->getAssociations($entityManagerName));
×
113
    }
114

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

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

138
        if ($patch === true) {
12✔
139
            $restDto->load($entity);
6✔
140
        }
141

142
        $restDto->patch($dto);
12✔
143

144
        return $restDto;
12✔
145
    }
146

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

155
        if ($entity === null) {
16✔
156
            throw new NotFoundHttpException('Not found');
×
157
        }
158

159
        return $entity;
16✔
160
    }
161

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

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

178
    /**
179
     * Method to validate specified entity.
180
     *
181
     * @throws Throwable
182
     */
183
    protected function validateEntity(EntityInterface $entity, bool $skipValidation): void
184
    {
185
        $errors = $skipValidation ? null : $this->getValidator()->validate($entity);
142✔
186

187
        // Oh noes, we have some errors
188
        if ($errors !== null && $errors->count() > 0) {
142✔
189
            throw new ValidatorException($entity::class, $errors);
×
190
        }
191
    }
192

193
    /**
194
     * @throws NotFoundHttpException
195
     */
196
    protected function checkThatEntityExists(bool $throwExceptionIfNotFound, ?EntityInterface $entity): void
197
    {
198
        // Entity not found
199
        if ($throwExceptionIfNotFound && $entity === null) {
50✔
200
            throw new NotFoundHttpException('Not found');
×
201
        }
202
    }
203
}
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