Completed
Push — master ( bb6905...021686 )
by Grégoire
18s queued 12s
created

src/Validator/FormatValidator.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Validator;
15
16
use Sonata\MediaBundle\Model\GalleryInterface;
17
use Sonata\MediaBundle\Provider\MediaProviderInterface;
18
use Sonata\MediaBundle\Provider\Pool;
19
use Symfony\Component\Validator\Constraint;
20
use Symfony\Component\Validator\ConstraintValidator;
21
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
22
23
/**
24
 * @final since sonata-project/media-bundle 3.21.0
25
 */
26
class FormatValidator extends ConstraintValidator
27
{
28
    /**
29
     * @var Pool
30
     */
31
    protected $pool;
32
33
    public function __construct(Pool $pool)
34
    {
35
        $this->pool = $pool;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function validate($value, Constraint $constraint): void
42
    {
43
        $formats = $this->pool->getFormatNamesByContext($value->getContext());
44
45
        if (!$value instanceof GalleryInterface) {
46
            // Interface compatibility, support for LegacyExecutionContextInterface can be removed when support for Symfony <2.5 is dropped
47
            if ($this->context instanceof LegacyExecutionContextInterface) {
0 ignored issues
show
The class Symfony\Component\Valida...ecutionContextInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
48
                $this->context->addViolationAt('defaultFormat', 'Invalid instance, expected GalleryInterface');
49
            } else {
50
                $this->context->buildViolation('Invalid instance, expected GalleryInterface')
51
                   ->atPath('defaultFormat')
52
                   ->addViolation();
53
            }
54
        }
55
56
        $galleryDefaultFormat = $value->getDefaultFormat();
57
58
        if (MediaProviderInterface::FORMAT_REFERENCE !== $galleryDefaultFormat
59
            && !($formats && \array_key_exists($galleryDefaultFormat, $formats))) {
60
            $this->context->addViolation('invalid format');
61
        }
62
    }
63
}
64