OdmCrudRepository::getByIds()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2025 Iain Cambridge
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
10
 * the Free Software Foundation, either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\Athena\Repository;
23
24
use Parthenon\Athena\Entity\DeletableInterface;
25
use Parthenon\Athena\Filters\DoctrineFilterInterface;
26
use Parthenon\Athena\Filters\OdmFilterInterface;
27
use Parthenon\Athena\ResultSet;
28
use Parthenon\Common\Exception\NoEntityFoundException;
29
use Parthenon\Common\Repository\OdmRepository;
30
31
class OdmCrudRepository extends OdmRepository implements CrudRepositoryInterface
32
{
33
    public function getList(array $filters = [], string $sortKey = 'id', string $sortType = 'ASC', int $limit = self::LIMIT, $lastId = null, $firstId = null): ResultSet
34
    {
35
        $sortKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $sortKey))));
36
37
        $qb = $this->documentRepository->createQueryBuilder();
38
        $qb
39
            ->sort($sortKey, $sortType)
40
            ->limit($limit + 1); // Fetch one more than required for pagination.
41
42
        $direction = 'DESC' === $sortType ? '<' : '>';
43
        $firstDirection = 'DESC' !== $sortType ? '<' : '>';
44
45
        if ($firstId) {
46
            $sortType = ('DESC' === $sortType) ? 'ASC' : 'DESC';
47
        }
48
49
        $sortKey = preg_replace('/[^A-Za-z0-9_]+/', '', $sortKey);
50
        $dm = $this->documentRepository->getDocumentManager();
51
        $columns = $dm->getClassMetadata($this->documentRepository->getClassName())->getFieldNames();
52
53
        if (!in_array($sortKey, $columns)) {
54
            throw new \InvalidArgumentException("Sort key doesn't exist");
55
        }
56
57
        if ($lastId) {
58
            $qb->where($sortKey.' '.$direction.' '.$lastId);
59
        }
60
        if ($firstId) {
61
            $qb->where($sortKey.' '.$firstDirection.' '.$firstId);
62
        }
63
64
        if (is_a($this->documentRepository->getClassName(), DeletableInterface::class, true)) {
65
            $qb->field('isDeleted')->equals(false);
66
        }
67
68
        foreach ($filters as $filter) {
69
            /** @var DoctrineFilterInterface $filter */
70
            if ($filter instanceof OdmFilterInterface && $filter->hasData()) {
71
                $filter->modifiyOdmQueryBuilder($qb);
72
            }
73
        }
74
75
        $query = $qb->getQuery();
76
77
        return new ResultSet($query->toArray(), $sortKey, $sortType, $limit);
78
    }
79
80
    public function getById($id, $includeDeleted = false)
81
    {
82
        $entity = $this->documentRepository->findOneBy(['id' => $id]);
83
84
        if (!$entity || (false == $includeDeleted && is_a($this->documentRepository->getClassName(), DeletableInterface::class, true) && $entity->isDeleted())) {
85
            throw new NoEntityFoundException();
86
        }
87
88
        return $entity;
89
    }
90
91
    public function delete($entity)
92
    {
93
        if (!$entity instanceof DeletableInterface) {
94
            throw new \InvalidArgumentException('Excepted deletable entity non given. Must implement '.DeletableInterface::class);
95
        }
96
        $entity->markAsDeleted();
97
        $this->save($entity);
98
    }
99
100
    public function undelete($entity)
101
    {
102
        if (!$entity instanceof DeletableInterface) {
103
            throw new \InvalidArgumentException('to deletable entity non given. Must implement '.DeletableInterface::class);
104
        }
105
        $entity->unmarkAsDeleted();
106
        $this->save($entity);
107
    }
108
109
    public function getByIds(array $ids): ResultSet
110
    {
111
        $qb = $this->documentRepository->createQueryBuilder();
112
        $qb->find('id')->in($ids);
113
        $query = $qb->getQuery();
114
115
        return new ResultSet($query->toArray(), 'id', 'asc', count($ids));
116
    }
117
}
118