FootnoteRepository   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
eloc 53
c 0
b 0
f 0
dl 0
loc 124
ccs 0
cts 59
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 12 2
A initializeObject() 0 9 1
A getLowestFreeIndexNumber() 0 33 5
A usortFootnotesByUids() 0 10 2
A getFootnotesByUids() 0 7 1
A getFootnoteByUid() 0 9 1
A sortFootnotesByUids() 0 8 2
1
<?php
2
3
namespace AOE\HappyFeet\Domain\Repository;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2020 AOE GmbH <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
use AOE\HappyFeet\Domain\Model\Footnote;
30
use Doctrine\DBAL\Query\QueryBuilder;
31
use PDO;
32
use TYPO3\CMS\Core\Database\ConnectionPool;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException;
35
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
36
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
37
use TYPO3\CMS\Extbase\Persistence\Repository;
38
39
/**
40
 * Repository for Footnote objects.
41
 */
42
class FootnoteRepository extends Repository
43
{
44
    public static array $uids = [];
45
46
    protected string $tableName = 'tx_happyfeet_domain_model_footnote';
47
48
    public function initializeObject(): void
49
    {
50
        /** @var Typo3QuerySettings $defaultQuerySettings */
51
        $defaultQuerySettings = GeneralUtility::makeInstance(Typo3QuerySettings::class);
52
        $defaultQuerySettings->setRespectStoragePage(false);
53
        $defaultQuerySettings->setRespectSysLanguage(false);
54
        $defaultQuerySettings->setIgnoreEnableFields(false)
55
            ->setIncludeDeleted(false);
56
        $this->setDefaultQuerySettings($defaultQuerySettings);
57
    }
58
59
    /**
60
     * Returns the smallest index which is not used.
61
     */
62
    public function getLowestFreeIndexNumber(): int
63
    {
64
        /** @var QueryBuilder $queryBuilder */
65
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
66
67
        $results = $queryBuilder
68
            ->select('index_number')
69
            ->from($this->tableName)
70
            ->where(
71
                $queryBuilder->expr()
72
                    ->eq('deleted', $queryBuilder->createNamedParameter(0, PDO::PARAM_INT))
73
            )
74
            ->executeQuery()
75
            ->fetchAllAssociative();
76
77
        $index = 1;
78
        if (count($results) < 1) {
79
            return $index;
80
        }
81
82
        $indexes = [];
83
        foreach ($results as $result) {
84
            $indexes[] = (int) $result['index_number'];
85
        }
86
87
        $indexCount = count($indexes);
88
        for ($index = 1; $index <= $indexCount + 1; ++$index) {
89
            if (!in_array($index, $indexes, true)) {
90
                break;
91
            }
92
        }
93
94
        return $index;
95
    }
96
97
    /**
98
     * @param Footnote $object
99
     */
100
    public function add($object): void
101
    {
102
        /** @var Footnote $object */
103
        if (!($object instanceof Footnote)) {
0 ignored issues
show
introduced by
$object is always a sub-type of AOE\HappyFeet\Domain\Model\Footnote.
Loading history...
104
            throw new IllegalObjectTypeException(
105
                'The object given to add() was not of the type (' . $this->objectType . ') this repository manages.',
106
                1392911702
107
            );
108
        }
109
110
        $object->setIndexNumber($this->getLowestFreeIndexNumber());
111
        parent::add($object);
112
    }
113
114
    /**
115
     * @param integer $uid
116
     * @return Footnote|null
117
     */
118
    public function getFootnoteByUid($uid)
119
    {
120
        $query = $this->createQuery();
121
        $query->setQuerySettings($this->defaultQuerySettings);
122
123
        /** @var Footnote */
124
        return $query->matching($query->equals('uid', $uid))
125
            ->execute()
126
            ->getFirst();
127
    }
128
129
    /**
130
     * @return array|QueryResultInterface
131
     */
132
    public function getFootnotesByUids(array $uids)
133
    {
134
        self::$uids = $uids;
135
        $query = $this->createQuery();
136
        $query->setQuerySettings($this->defaultQuerySettings);
137
        $query->matching($query->in('uid', $uids));
138
        return $this->sortFootnotesByUids($query->execute(), $uids);
139
    }
140
141
    /**
142
     * @param array|QueryResultInterface $queryResult
143
     * @param array $uids
144
     * @return mixed
145
     */
146
    public function sortFootnotesByUids($queryResult, $uids)
0 ignored issues
show
Unused Code introduced by
The parameter $uids is not used and could be removed. ( Ignorable by Annotation )

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

146
    public function sortFootnotesByUids($queryResult, /** @scrutinizer ignore-unused */ $uids)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
147
    {
148
        if ($queryResult instanceof QueryResultInterface) {
149
            $queryResult = $queryResult->toArray();
150
        }
151
152
        usort($queryResult, fn (Footnote $a, Footnote $b): int => $this::usortFootnotesByUids($a, $b));
153
        return $queryResult;
154
    }
155
156
    public static function usortFootnotesByUids(
157
        Footnote $a,
158
        Footnote $b
159
    ): int {
160
        $map = array_flip(self::$uids);
161
        if ($map[$a->getUid()] >= $map[$b->getUid()]) {
162
            return 1;
163
        }
164
165
        return -1;
166
    }
167
}
168