• 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

0.0
/src/General/Application/Validator/Constraints/EntityReferenceExistsValidator.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\General\Application\Validator\Constraints;
6

7
use App\General\Domain\Entity\Interfaces\EntityInterface;
8
use Closure;
9
use Doctrine\ORM\EntityNotFoundException;
10
use Psr\Log\LoggerInterface;
11
use Symfony\Component\Validator\Constraint;
12
use Symfony\Component\Validator\ConstraintValidator;
13
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
14
use Symfony\Component\Validator\Exception\UnexpectedValueException;
15

16
use function array_filter;
17
use function array_map;
18
use function count;
19
use function implode;
20
use function is_array;
21
use function str_replace;
22

23
/**
24
 * Class EntityReferenceExistsValidator
25
 *
26
 * @package App\General
27
 */
28
class EntityReferenceExistsValidator extends ConstraintValidator
29
{
30
    public function __construct(
31
        private readonly LoggerInterface $logger,
32
    ) {
33
    }
×
34

35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function validate(mixed $value, Constraint $constraint): void
39
    {
40
        if (!$constraint instanceof EntityReferenceExists) {
×
41
            throw new UnexpectedTypeException($constraint, EntityReferenceExists::class);
×
42
        }
43

44
        $values = $this->normalize($constraint->entityClass, $value);
×
45
        $this->check($values);
×
46
    }
47

48
    /**
49
     * Checks if the passed value is valid.
50
     *
51
     * @param EntityInterface|array<int, EntityInterface>|mixed $input
52
     *
53
     * @return array<int, EntityInterface>
54
     */
55
    private function normalize(string $target, mixed $input): array
56
    {
57
        $values = is_array($input) ? $input : [$input];
×
58

59
        foreach ($values as $value) {
×
60
            if (!$value instanceof $target) {
×
61
                throw new UnexpectedValueException($value, $target);
×
62
            }
63

64
            if (!$value instanceof EntityInterface) {
×
65
                throw new UnexpectedValueException($value, EntityInterface::class);
×
66
            }
67
        }
68

69
        return $values;
×
70
    }
71

72
    /**
73
     * @param array<int, EntityInterface> $entities
74
     */
75
    private function check(array $entities): void
76
    {
77
        $invalidIds = $this->getInvalidValues($entities);
×
78

79
        if ($invalidIds !== []) {
×
80
            $message = count($invalidIds) === 1
×
81
                ? EntityReferenceExists::MESSAGE_SINGLE
×
82
                : EntityReferenceExists::MESSAGE_MULTIPLE;
×
83
            $entity = $entities[0]::class;
×
84
            $parameterEntity = str_replace('Proxies\\__CG__\\', '', $entity);
×
85
            $parameterId = count($invalidIds) > 1 ? implode('", "', $invalidIds) : $invalidIds[0];
×
86

87
            $this->context
×
88
                ->buildViolation($message)
×
89
                ->setParameter('{{ entity }}', $parameterEntity)
×
90
                ->setParameter('{{ id }}', $parameterId)
×
91
                ->setCode(EntityReferenceExists::ENTITY_REFERENCE_EXISTS_ERROR)
×
92
                ->addViolation();
×
93
        }
94
    }
95

96
    /**
97
     * @param array<int, EntityInterface> $entities
98
     *
99
     * @return array<int, string>
100
     */
101
    private function getInvalidValues(array $entities): array
102
    {
103
        return array_map(
×
104
            static fn (EntityInterface $entity): string => $entity->getId(),
×
105
            array_filter($entities, $this->getFilterClosure())
×
106
        );
×
107
    }
108

109
    /**
110
     * Method to return used filter closure.
111
     */
112
    private function getFilterClosure(): Closure
113
    {
114
        return function (EntityInterface $entity): bool {
×
115
            $output = false;
×
116

117
            try {
118
                $entity->getCreatedAt();
×
119
            } catch (EntityNotFoundException $exception) {
×
120
                $this->logger->error($exception->getMessage());
×
121
                $output = true;
×
122
            }
123

124
            return $output;
×
125
        };
×
126
    }
127
}
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

© 2025 Coveralls, Inc