ConfigRepository::loadAllByGroup()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 23
rs 9.7998
1
<?php
2
3
namespace Xiidea\EasyConfigBundle\Services\Repository;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Xiidea\EasyConfigBundle\Exception\UnrecognizedEntityException;
7
use Xiidea\EasyConfigBundle\Model\BaseConfig;
8
use Xiidea\EasyConfigBundle\Model\BaseConfigInterface;
9
10
class ConfigRepository implements ConfigRepositoryInterface
11
{
12
    private $repository;
13
14
    public function __construct(private EntityManagerInterface $em, private $entityClass)
15
    {
16
        $this->isRecognizedEntity($entityClass);
17
        $this->repository = $em->getRepository($entityClass);
18
    }
19
20
    /**
21
     * @param $entityClass
22
     * @return void
23
     * @throws UnrecognizedEntityException
24
     */
25
    private function isRecognizedEntity($entityClass): void
26
    {
27
        if (!new $entityClass('') instanceof BaseConfig) {
28
            throw new UnrecognizedEntityException();
29
        }
30
    }
31
32
    public function getConfigurationByUsernameAndGroup(string $username, string $groupKey)
33
    {
34
        $qb = $this->createQueryBuilder('c');
35
36
        return $qb
37
            ->setCacheable(true)
38
            ->setCacheRegion('config_group')
39
            ->where($qb->expr()->like('c.id', ':username'))
40
            ->setParameter('username', $username.'.'.$groupKey.'.%')
41
            ->orWhere(
42
                $qb->expr()->andX(
43
                    $qb->expr()->like('c.id', ':groupKey'),
44
                    $qb->expr()->eq('c.isGlobal', ':isGlobal')
45
                )
46
            )
47
            ->setParameter('groupKey', $groupKey.'.%')
48
            ->setParameter('isGlobal', 1)
49
            ->getQuery()
50
            ->getResult();
51
    }
52
53
    public function getConfigurationByUsernameAndKey(string $username, string $key)
54
    {
55
        $qb = $this->createQueryBuilder('c');
56
57
        return $qb
58
            ->setCacheable(true)
59
            ->setCacheRegion('config_key')
60
            ->where('c.id=:username')
61
            ->setParameter('username', $username.'.'.$key)
62
            ->orWhere('c.id=:key')
63
            ->setParameter('key', $key)
64
            ->getQuery()
65
            ->getResult();
66
    }
67
68
    private function createQueryBuilder(string $alias)
69
    {
70
        return $this->repository->createQueryBuilder($alias);
71
    }
72
73
    public function loadAllByGroup($groupKey, $valueOnly = false, $frontendOnly = false): array
74
    {
75
        $qb = $this->createQueryBuilder('c');
76
77
        $qb = $qb
78
            ->where($qb->expr()->like('c.id', ':group_key'))
79
            ->setCacheable(true)
80
            ->setCacheRegion('master_entity_region')
81
            ->setParameter('group_key', $groupKey.'.%');
82
83
        if ($frontendOnly) {
84
            $qb
85
                ->andWhere($qb->expr()->eq('c.frontend', ':is_frontend'))
86
                ->setParameter('is_frontend', true);
87
        }
88
89
        $configurations = $qb->getQuery()->getResult();
90
91
        if (!$configurations) {
92
            return [];
93
        }
94
95
        return $this->getNormalizedArray($groupKey, $configurations, $valueOnly);
96
    }
97
98
    private function getNormalizedArray($groupKey, $configurations, $valueOnly): array
99
    {
100
        $keyLength = strlen($groupKey) + 1;
101
        $return = [];
102
103
        foreach ($configurations as $configuration) {
104
            $return[substr($configuration->getId(), $keyLength)] = $valueOnly ? $configuration->getAs(
105
            ) : $configuration;
106
        }
107
108
        return $return;
109
    }
110
111
112
    public function getValuesByGroupKey($configurationGroup)
113
    {
114
        return array_map([$this, 'getValue'], (array)$this->loadAllByGroup($configurationGroup));
115
    }
116
117
    protected function getValue($configuration)
118
    {
119
        return $configuration->getValue();
120
    }
121
122
    public function saveMultiple($baseKey, array $values = [], array $types = [])
123
    {
124
        foreach ($values as $key => $value) {
125
            $this->save($baseKey.".{$key}", $value, isset($types[$key]) ?? $types[$key], false, false, false);
126
        }
127
128
        $this->em->flush();
129
    }
130
131
    /**
132
     * @param $key
133
     * @param $value
134
     * @param $type
135
     * @param bool $locked
136
     * @param bool $force
137
     * @param bool $flush
138
     * @return BaseConfig
139
     */
140
    public function save($key, $value, $type = null, bool $locked = false, bool $force = false, bool $flush = true)
141
    {
142
        $configuration = $this->repository->find($key);
143
144
        if (!$configuration) {
145
            $configuration = new $this->entityClass($key);
146
            $configuration->setId($key);
147
        } elseif ($configuration->isLocked() && !$force) {
148
            return $configuration;
149
        }
150
151
        $configuration->setValue($value);
152
        $configuration->setType($type);
153
        $configuration->setLocked($locked);
154
155
        $this->em->persist($configuration);
156
157
        if ($flush) {
158
            $this->em->flush($configuration);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\Persistence\ObjectManager::flush() has too many arguments starting with $configuration. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
            $this->em->/** @scrutinizer ignore-call */ 
159
                       flush($configuration);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
159
        }
160
161
        return $configuration;
162
    }
163
164
    public function removeByKey($key)
165
    {
166
        $config = $this->repository->find($key);
167
168
        if ($config) {
169
            $this->em->remove($config);
170
            $this->em->flush();
171
        }
172
    }
173
174
    public function getConfigurationValue($key)
175
    {
176
        $configuration = $this->repository->find($key);
177
178
        if (null == $configuration) {
179
            return null;
180
        }
181
182
        return $configuration->getValue();
183
    }
184
185
    public function getGlobalAndUserConfigurationByKey(string $username, string $key)
186
    {
187
        $qb = $this->createQueryBuilder('c');
188
189
        return $qb
190
            ->setCacheable(true)
191
            ->setCacheRegion('config_key')
192
            ->where('c.id=:username')
193
            ->setParameter('username', $username.'.'.$key)
194
            ->orWhere('c.id=:key')
195
            ->setParameter('key', $key)
196
            ->orderBy('c.isGlobal', 'DESC')
197
            ->getQuery()
198
            ->getResult();
199
    }
200
}
201