Failed Conditions
Push — master ( 77b356...527c49 )
by Adrien
03:51 queued 01:07
created

AbstractOperator   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
dl 0
loc 50
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Definition\Operator;
6
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Doctrine\ORM\QueryBuilder;
9
use GraphQL\Doctrine\Factory\UniqueNameFactory;
10
use GraphQL\Doctrine\Types;
11
use GraphQL\Doctrine\Utils;
12
use GraphQL\Type\Definition\InputObjectType;
13
use GraphQL\Type\Definition\LeafType;
14
15
/**
16
 * Abstract class that must be implemented to define custom filter options.
17
 *
18
 * Once implemented its FQCN should be used via `API\Filter` annotation.
19
 */
20
abstract class AbstractOperator extends InputObjectType
21
{
22 30
    final public function __construct(Types $types, LeafType $leafType)
23
    {
24 30
        $config = $this->getConfiguration($types, $leafType);
25
26
        // Override type name to be predictable
27 30
        $config['name'] = Utils::getOperatorTypeName(get_class($this), $leafType);
28
29 30
        parent::__construct($config);
30 30
    }
31
32
    /**
33
     * Return the GraphQL type configuration for an `InputObjectType`.
34
     *
35
     * This should declare all custom fields needed to apply the filter. In most
36
     * cases it would include a field such as `value` or `values`, and possibly other
37
     * more specific fields.
38
     *
39
     * The type name, usually configured with the `name` key, should not be defined and
40
     * will be overridden in all cases. This is because we must have a predictable name
41
     * that is based only on the class name.
42
     *
43
     * @param Types $types
44
     * @param LeafType $leafType
45
     *
46
     * @return array
47
     */
48
    abstract protected function getConfiguration(Types $types, LeafType $leafType): array;
49
50
    /**
51
     * Return the DQL condition to apply the filter
52
     *
53
     * The query builder:
54
     *
55
     * - MUST NOT be used to apply the condition directly (with `*where()` methods). Instead the condition MUST
56
     *     be returned as string. Otherwise it will break OR/AND logic of sibling operators.
57
     * - MAY be used to inspect existing joins and add joins if needed.
58
     * - SHOULD be used to set query parameter (with the helper of `UniqueNameFactory`)
59
     *
60
     * @param UniqueNameFactory $uniqueNameFactory a helper to get unique names to be used in the query
61
     * @param ClassMetadata $metadata
62
     * @param QueryBuilder $queryBuilder
63
     * @param string $alias the alias for the entity on which to apply the filter
64
     * @param string $field the field for the entity on which to apply the filter
65
     * @param array $args all arguments specific to this operator as declared in its configuration
66
     *
67
     * @return string
68
     */
69
    abstract public function getDqlCondition(UniqueNameFactory $uniqueNameFactory, ClassMetadata $metadata, QueryBuilder $queryBuilder, string $alias, string $field, array $args): string;
70
}
71