AbstractOperator   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 1
eloc 4
c 2
b 0
f 0
dl 0
loc 46
ccs 4
cts 4
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 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` attribute.
19
 */
20
abstract class AbstractOperator extends InputObjectType
21
{
22 59
    final public function __construct(
23
        protected readonly Types $types,
24
        LeafType $leafType,
25
    ) {
26 59
        $config = $this->getConfiguration($leafType);
27
28
        // Override type name to be predictable
29 59
        $config['name'] = Utils::getOperatorTypeName(static::class, $leafType);
30
31 59
        parent::__construct($config);
32
    }
33
34
    /**
35
     * Return the GraphQL type configuration for an `InputObjectType`.
36
     *
37
     * This should declare all custom fields needed to apply the filter. In most
38
     * cases it would include a field such as `value` or `values`, and possibly other
39
     * more specific fields.
40
     *
41
     * The type name, usually configured with the `name` key, should not be defined and
42
     * will be overridden in all cases. This is because we must have a predictable name
43
     * that is based only on the class name.
44
     */
45
    abstract protected function getConfiguration(LeafType $leafType): array;
46
47
    /**
48
     * Return the DQL condition to apply the filter.
49
     *
50
     * In most cases a DQL condition should be returned as a string, but it might be useful to
51
     * return null if the filter is not applicable (eg: a search term with empty string).
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 string $alias the alias for the entity on which to apply the filter
62
     * @param string $field the field for the entity on which to apply the filter
63
     * @param null|array $args all arguments specific to this operator as declared in its configuration
64
     */
65
    abstract public function getDqlCondition(UniqueNameFactory $uniqueNameFactory, ClassMetadata $metadata, QueryBuilder $queryBuilder, string $alias, string $field, ?array $args): string;
66
}
67