Passed
Pull Request — main (#9)
by
unknown
02:33
created

  A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 13
rs 9.9332
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
9
class ConfigRepository implements ConfigRepositoryInterface
10
{
11
    private $repository;
12
13
    public function __construct(private EntityManagerInterface $em, private $entityClass)
14
    {
15
        $this->isRecognizedEntity($entityClass);
16
        $this->repository = $em->getRepository($entityClass);
17
    }
18
19
    /**
20
     * @param $entityClass
21
     * @return void
22
     * @throws UnrecognizedEntityException
23
     */
24
    private function isRecognizedEntity($entityClass): void
25
    {
26
        if (!new $entityClass('') instanceof BaseConfig) {
27
            throw new UnrecognizedEntityException();
28
        }
29
    }
30
31
    public function getConfigurationByUsernameAndGroup(string $username, string $groupKey)
32
    {
33
        $qb = $this->createQueryBuilder('c');
34
35
        return $qb
36
            ->setCacheable(true)
37
            ->setCacheRegion('config_group')
38
            ->where($qb->expr()->like('c.id', ':username'))
39
            ->setParameter('username', $username.'.'.$groupKey.'.%')
40
            ->orWhere(
41
                $qb->expr()->andX(
42
                    $qb->expr()->like('c.id', ':groupKey'),
43
                    $qb->expr()->eq('c.isGlobal', ':isGlobal')
44
                )
45
            )
46
            ->setParameter('groupKey', $groupKey.'.%')
47
            ->setParameter('isGlobal', 1)
48
            ->getQuery()
49
            ->getResult();
50
    }
51
52
    public function getGlobalAndUserConfigurationByKey(string $username, string $key)
53
    {
54
        $qb = $this->createQueryBuilder('c');
55
56
        return $qb
57
            ->setCacheable(true)
58
            ->setCacheRegion('config_key')
59
            ->where('c.id=:username')
60
            ->setParameter('username', $username.'.'.$key)
61
            ->orWhere('c.id=:key')
62
            ->setParameter('key', $key)
63
            ->orderBy('c.isGlobal', 'DESC')
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