rich-id/terms-module

View on GitHub
src/Infrastructure/ValidatorConstraints/CantContentChangeIfTermsPublished.php

Summary

Maintainability
B
4 hrs
Test Coverage
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Infrastructure\ValidatorConstraints;

use RichId\TermsModuleBundle\Domain\Model\TermsEdition;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/** @Annotation */
class CantContentChangeIfTermsPublished extends Constraint implements ConstraintValidatorInterface
{
    public const MESSAGE = 'terms_edition.validation.cant_content_change_if_terms_published';

    /** @var ExecutionContextInterface */
    protected $context;

    public function initialize(ExecutionContextInterface $context): void
    {
        $this->context = $context;
    }

    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }

    public function validate($value, Constraint $constraint): void
    {
        if (!$value instanceof TermsEdition || !$value->getEntity()->isEnabled()) {
            return;
        }

        $originalTermsVersion = $value->getEntity();

        if ($value->getContent() !== $originalTermsVersion->getContent()) {
            $this->context->buildViolation(self::MESSAGE)
                ->atPath('content')
                ->setTranslationDomain('terms_module_validation')
                ->addViolation();
        }
    }

    public function validatedBy(): string
    {
        return self::class;
    }
}