Passed
Branch main (b6a268)
by Iain
04:04
created

OdmCrudRepository::getList()   B

Complexity

Conditions 8
Paths 26

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 22
c 1
b 0
f 0
nc 26
nop 5
dl 0
loc 38
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Humbly Arrogant Ltd 2020-2022.
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.0.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): ResultSet
27
    {
28
        $sortKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $sortKey))));
29
30
        $parts = explode('\\', $this->documentRepository->getClassName());
31
        $name = end($parts);
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
32
        $qb = $this->documentRepository->createQueryBuilder();
33
        $qb
34
            ->sort($sortKey, $sortType)
35
            ->limit($limit + 1); // Fetch one more than required for pagination.
36
37
        $direction = 'DESC' === $sortType ? '<' : '>';
38
        $sortKey = preg_replace('/[^A-Za-z0-9_]+/', '', $sortKey);
39
        $dm = $this->documentRepository->getDocumentManager();
40
        $columns = $dm->getClassMetadata($this->documentRepository->getClassName())->getFieldNames();
41
42
        if (!in_array($sortKey, $columns)) {
43
            throw new \InvalidArgumentException("Sort key doesn't exist");
44
        }
45
46
        if ($lastId) {
47
            $qb->where($sortKey.' '.$direction.' '.$lastId);
48
        }
49
50
        if (is_a($this->documentRepository->getClassName(), DeletableInterface::class, true)) {
51
            $qb->field('isDeleted')->equals(false);
52
        }
53
54
        foreach ($filters as $filter) {
55
            if ($filter instanceof OdmFilterInterface && $filter->hasData()) {
56
                $filter->modifiyOdmQueryBuilder($qb);
57
            }
58
        }
59
60
        /** @var DoctrineFilterInterface $filter */
61
        $query = $qb->getQuery();
62
63
        return new ResultSet($query->toArray(), $sortKey, $sortType, $limit);
64
    }
65
66
    public function getById($id, $includeDeleted = false)
67
    {
68
        $entity = $this->documentRepository->findOneBy(['id' => $id]);
69
70
        if (!$entity || (false == $includeDeleted && is_a($this->documentRepository->getClassName(), DeletableInterface::class, true) && $entity->isDeleted())) {
71
            throw new NoEntityFoundException();
72
        }
73
74
        return $entity;
75
    }
76
77
    public function delete($entity)
78
    {
79
        if (!$entity instanceof DeletableInterface) {
80
            throw new \InvalidArgumentException('Excepted deletable entity non given. Must implement '.DeletableInterface::class);
81
        }
82
        $entity->markAsDeleted();
83
        $this->save($entity);
84
    }
85
86
    public function undelete($entity)
87
    {
88
        if (!$entity instanceof DeletableInterface) {
89
            throw new \InvalidArgumentException('to deletable entity non given. Must implement '.DeletableInterface::class);
90
        }
91
        $entity->unmarkAsDeleted();
92
        $this->save($entity);
93
    }
94
}
95