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

systemsdk / docker-symfony-api / #75

pending completion
#75

push

DKravtsov
Fixed issue with REDIS volumes, updated dependencies, refactoring

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

1470 of 2656 relevant lines covered (55.35%)

23.64 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\Application\Validator\Constraints\EntityReferenceExists as Constraint;
8
use App\General\Domain\Entity\Interfaces\EntityInterface;
9
use Closure;
10
use Doctrine\ORM\EntityNotFoundException;
11
use Psr\Log\LoggerInterface;
12
use Symfony\Component\Validator\Constraint as BaseConstraint;
13
use Symfony\Component\Validator\ConstraintValidator;
14
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
15
use Symfony\Component\Validator\Exception\UnexpectedValueException;
16

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

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

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

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

49
    /**
50
     * Checks if the passed value is valid.
51
     *
52
     * @return array<array-key, EntityInterface>
53
     */
54
    private function normalize(string $target, mixed $input): array
55
    {
56
        return array_map(
×
57
            static function ($value) use ($target) {
×
58
                if (!$value instanceof $target) {
×
59
                    throw new UnexpectedValueException($value, $target);
×
60
                }
61

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

66
                return $value;
×
67
            },
×
68
            is_array($input) ? $input : [$input]
×
69
        );
×
70
    }
71

72
    /**
73
     * @param array<array-key, 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 ? Constraint::MESSAGE_SINGLE : Constraint::MESSAGE_MULTIPLE;
×
81
            $entity = $entities[0]::class;
×
82
            $parameterEntity = str_replace('Proxies\\__CG__\\', '', $entity);
×
83
            $parameterId = count($invalidIds) > 1 ? implode('", "', $invalidIds) : $invalidIds[0];
×
84

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

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

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

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

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