Config::getUseHydratorCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApiSkeletons\Doctrine\ORM\GraphQL;
6
7
use ApiSkeletons\Doctrine\ORM\GraphQL\Filter\Filters;
8
use InvalidArgumentException;
9
10
use function array_merge;
11
use function property_exists;
12
13
/**
14
 * This class is used for setting parameters when
15
 * creating the driver
16
 */
17
class Config
18
{
19
    /**
20
     * @var string The GraphQL group. This allows multiple GraphQL
21
     *             configurations within the same application or
22
     *             even within the same group of entities and Object Manager.
23
     */
24
    protected readonly string $group;
25
26
    /**
27
     * @var string|null The group is usually suffixed to GraphQL type names.
28
     *                  You may specify a different string for the group suffix
29
     *                  or you my supply an empty string to exclude the suffix.
30
     *                  Be warned, using the same groupSuffix with two different
31
     *                  groups can cause collisions.
32
     */
33
    protected readonly string|null $groupSuffix;
34
35
    /**
36
     * @var bool When set to true hydrator results will be cached for the
37
     *           duration of the request thereby saving multiple extracts for
38
     *           the same entity.
39
     */
40
    protected readonly bool $useHydratorCache;
41
42
    /** @var int A hard limit for fetching any collection within the schema */
43
    protected readonly int $limit;
44
45
    /**
46
     * @var bool When set to true all fields and all associations will be
47
     *           enabled.  This is best used as a development setting when
48
     *           the entities are subject to change.
49
     */
50
    protected readonly bool $globalEnable;
51
52
    /** @var string[] An array of field names to ignore when using globalEnable. */
53
    protected readonly array $ignoreFields;
54
55
    /**
56
     * @var bool|null When set to true, all entities will be extracted by value
57
     *                across all hydrators in the driver.  When set to false,
58
     *                all hydrators will extract by reference.  This overrides
59
     *                per-entity attribute configuration.
60
     */
61
    protected readonly bool|null $globalByValue;
62
63
    /**
64
     * @var string|null When set, the entityPrefix will be removed from each
65
     *                  type name.  This simplifies type names and makes reading
66
     *                  the GraphQL documentation easier.
67
     */
68
    protected readonly string|null $entityPrefix;
69
70
    /**
71
     * @var bool|null When set to true entity fields will be
72
     *                sorted alphabetically
73
     */
74
    protected readonly bool|null $sortFields;
75
76
    /**
77
     * @var Filters[] An array of filters to exclude from
78
     *                available filters for all fields and
79
     *                associations in every entity
80
     */
81
    protected readonly array $excludeFilters;
82
83
    /** @param mixed[] $config */
84
    public function __construct(array $config = [])
85
    {
86
        $default = [
87
            'group' => 'default',
88
            'groupSuffix' => null,
89
            'useHydratorCache' => false,
90
            'limit' => 1000,
91
            'globalEnable' => false,
92
            'ignoreFields' => [],
93
            'globalByValue' => null,
94
            'entityPrefix' => null,
95
            'sortFields' => null,
96
            'excludeFilters' => [],
97
        ];
98
99
        $mergedConfig = array_merge($default, $config);
100
101
        foreach ($mergedConfig as $field => $value) {
102
            if (! property_exists($this, $field)) {
103
                throw new InvalidArgumentException('Invalid configuration setting: ' . $field);
104
            }
105
106
            $this->$field = $value;
107
        }
108
    }
109
110
    public function getGroup(): string
111
    {
112
        return $this->group;
113
    }
114
115
    public function getGroupSuffix(): string|null
116
    {
117
        return $this->groupSuffix;
118
    }
119
120
    public function getUseHydratorCache(): bool
121
    {
122
        return $this->useHydratorCache;
123
    }
124
125
    public function getLimit(): int
126
    {
127
        return $this->limit;
128
    }
129
130
    public function getGlobalEnable(): bool
131
    {
132
        return $this->globalEnable;
133
    }
134
135
    /** @return string[] */
136
    public function getIgnoreFields(): array
137
    {
138
        return $this->ignoreFields;
139
    }
140
141
    public function getGlobalByValue(): bool|null
142
    {
143
        return $this->globalByValue;
144
    }
145
146
    public function getEntityPrefix(): string|null
147
    {
148
        return $this->entityPrefix;
149
    }
150
151
    public function getSortFields(): bool|null
152
    {
153
        return $this->sortFields;
154
    }
155
156
    /** @return Filters[] */
157
    public function getExcludeFilters(): array
158
    {
159
        return $this->excludeFilters;
160
    }
161
}
162