Passed
Push — main ( 5e0116...8ff6e1 )
by Iain
17:24
created

OdmCrudRepository::getList()   D

Complexity

Conditions 12
Paths 300

Size

Total Lines 45
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 25
c 2
b 0
f 0
nc 300
nop 6
dl 0
loc 45
rs 4.8833

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Iain Cambridge 2020-2023.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.2.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\Athena\Repository;
16
17
use Parthenon\Athena\Entity\DeletableInterface;
18
use Parthenon\Athena\Filters\DoctrineFilterInterface;
19
use Parthenon\Athena\Filters\OdmFilterInterface;
20
use Parthenon\Athena\ResultSet;
21
use Parthenon\Common\Exception\NoEntityFoundException;
22
use Parthenon\Common\Repository\OdmRepository;
23
24
class OdmCrudRepository extends OdmRepository implements CrudRepositoryInterface
25
{
26
    public function getList(array $filters = [], string $sortKey = 'id', string $sortType = 'ASC', int $limit = self::LIMIT, $lastId = null, $firstId = null): ResultSet
27
    {
28
        $sortKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $sortKey))));
29
30
        $qb = $this->documentRepository->createQueryBuilder();
31
        $qb
32
            ->sort($sortKey, $sortType)
33
            ->limit($limit + 1); // Fetch one more than required for pagination.
34
35
        $direction = 'DESC' === $sortType ? '<' : '>';
36
        $firstDirection = 'DESC' !== $sortType ? '<' : '>';
37
38
        if ($firstId) {
39
            $sortType = ('DESC' === $sortType) ? 'ASC' : 'DESC';
40
        }
41
42
        $sortKey = preg_replace('/[^A-Za-z0-9_]+/', '', $sortKey);
43
        $dm = $this->documentRepository->getDocumentManager();
44
        $columns = $dm->getClassMetadata($this->documentRepository->getClassName())->getFieldNames();
45
46
        if (!in_array($sortKey, $columns)) {
47
            throw new \InvalidArgumentException("Sort key doesn't exist");
48
        }
49
50
        if ($lastId) {
51
            $qb->where($sortKey.' '.$direction.' '.$lastId);
52
        }
53
        if ($firstId) {
54
            $qb->where($sortKey.' '.$firstDirection.' '.$firstId);
55
        }
56
57
        if (is_a($this->documentRepository->getClassName(), DeletableInterface::class, true)) {
58
            $qb->field('isDeleted')->equals(false);
59
        }
60
61
        foreach ($filters as $filter) {
62
            /** @var DoctrineFilterInterface $filter */
63
            if ($filter instanceof OdmFilterInterface && $filter->hasData()) {
64
                $filter->modifiyOdmQueryBuilder($qb);
65
            }
66
        }
67
68
        $query = $qb->getQuery();
69
70
        return new ResultSet($query->toArray(), $sortKey, $sortType, $limit);
71
    }
72
73
    public function getById($id, $includeDeleted = false)
74
    {
75
        $entity = $this->documentRepository->findOneBy(['id' => $id]);
76
77
        if (!$entity || (false == $includeDeleted && is_a($this->documentRepository->getClassName(), DeletableInterface::class, true) && $entity->isDeleted())) {
78
            throw new NoEntityFoundException();
79
        }
80
81
        return $entity;
82
    }
83
84
    public function delete($entity)
85
    {
86
        if (!$entity instanceof DeletableInterface) {
87
            throw new \InvalidArgumentException('Excepted deletable entity non given. Must implement '.DeletableInterface::class);
88
        }
89
        $entity->markAsDeleted();
90
        $this->save($entity);
91
    }
92
93
    public function undelete($entity)
94
    {
95
        if (!$entity instanceof DeletableInterface) {
96
            throw new \InvalidArgumentException('to deletable entity non given. Must implement '.DeletableInterface::class);
97
        }
98
        $entity->unmarkAsDeleted();
99
        $this->save($entity);
100
    }
101
102
    public function getByIds(array $ids): ResultSet
103
    {
104
        $qb = $this->documentRepository->createQueryBuilder();
105
        $qb->find('id')->in($ids);
106
        $query = $qb->getQuery();
107
108
        return new ResultSet($query->toArray(), 'id', 'asc', count($ids));
109
    }
110
}
111