Passed
Push — master ( 9da6fd...0354d3 )
by Rafael
05:30
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Definition\Plugin;
12
13
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface;
14
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
15
use Ynlo\GraphQLBundle\Model\ConstraintViolation;
16
use Ynlo\GraphQLBundle\Model\ConstraintViolationParameter;
17
18
/**
19
 * This plugin remove the field constraintViolations and all related types
20
 * when violations should be located only in errors.
21
 */
22
class ConstraintViolationsDefinitionPlugin extends AbstractDefinitionPlugin
23
{
24
    protected $config;
25
26 2
    public function __construct($config = [])
27
    {
28 2
        $this->config = $config;
29 2
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34 2
    public function configureEndpoint(Endpoint $endpoint): void
35
    {
36 2
        if (($this->config['validation_messages'] ?? null) !== 'error') {
37 2
            return;
38
        }
39
40
        if ($endpoint->hasTypeForClass(ConstraintViolation::class)) {
41
            $violationType = $endpoint->getType(ConstraintViolation::class);
42
            foreach ($endpoint->allTypes() as $type) {
43
                if ($type instanceof FieldsAwareDefinitionInterface) {
44
                    foreach ($type->getFields() as $field) {
45
                        if ($endpoint->hasType($field->getType())) {
46
                            if ($endpoint->getType($field->getType())->getName() === $violationType->getName()) {
47
                                $type->removeField($field->getName());
48
                            }
49
                        }
50
                    }
51
                }
52
            }
53
54
            $endpoint->removeType($violationType->getName());
55
        }
56
57
        if ($endpoint->hasTypeForClass(ConstraintViolationParameter::class)) {
58
            $endpoint->removeType($endpoint->getTypeForClass(ConstraintViolationParameter::class));
59
        }
60
    }
61
}
62