Passed
Push — feature/php-8 ( fc1925 )
by Felix
11:18
created

MappingRepository::getHashedMappings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Aoe\FeatureFlag\Domain\Repository;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2021 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\FeatureFlag\Domain\Model\Mapping;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
31
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
32
use TYPO3\CMS\Extbase\Persistence\Repository;
33
34
class MappingRepository extends Repository
35
{
36
    /**
37
     * @return void
38
     */
39 3
    public function initializeObject()
40
    {
41
        /** @var $defaultQuerySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */
42 3
        $defaultQuerySettings = GeneralUtility::makeInstance(Typo3QuerySettings::class);
43 3
        $defaultQuerySettings->setRespectStoragePage(false);
44 3
        $defaultQuerySettings->setRespectSysLanguage(false);
45 3
        $defaultQuerySettings->setIgnoreEnableFields(false)->setIncludeDeleted(false);
46 3
        $this->setDefaultQuerySettings($defaultQuerySettings);
47
    }
48
49
    /**
50
     * @param int $foreignTableUid
51
     * @param string $foreignTableName
52
     * @return Mapping
53
     */
54 1
    public function findOneByForeignTableNameAndUid($foreignTableUid, $foreignTableName)
55
    {
56 1
        return $this->findAllByForeignTableNameAndUid($foreignTableUid, $foreignTableName)->getFirst();
57
    }
58
59
    /**
60
     * @param int $foreignTableUid
61
     * @param string $foreignTableName
62
     * @return QueryResultInterface
63
     */
64 2
    public function findAllByForeignTableNameAndUid($foreignTableUid, $foreignTableName)
65
    {
66 2
        $query = $this->createQuery();
67 2
        $query->matching(
68 2
            $query->logicalAnd($query->equals('foreign_table_uid', $foreignTableUid),
69 2
            $query->equals('foreign_table_name', $foreignTableName))
0 ignored issues
show
Unused Code introduced by
The call to TYPO3\CMS\Extbase\Persis...Interface::logicalAnd() has too many arguments starting with $query->equals('foreign_...me', $foreignTableName). ( Ignorable by Annotation )

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

69
            $query->/** @scrutinizer ignore-call */ 
70
                    logicalAnd($query->equals('foreign_table_uid', $foreignTableUid),

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...
70
        );
71 2
        return $query->execute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->execute() also could return the type array<mixed,object> which is incompatible with the documented return type TYPO3\CMS\Extbase\Persistence\QueryResultInterface.
Loading history...
72
    }
73
74
    /**
75
     * @param $featureFlagId
76
     * @return array|QueryResultInterface
77
     */
78
    public function findAllByFeatureFlag($featureFlagId)
79
    {
80
        $query = $this->createQuery();
81
        $query->getQuerySettings()->setRespectSysLanguage(false);
82
        $query->getQuerySettings()->setRespectStoragePage(false);
83
        $query->matching($query->equals('feature_flag', $featureFlagId));
84
        return $query->execute();
85
    }
86
87
    /**
88
     * @return array
89
     */
90 1
    public function getHashedMappings()
91
    {
92 1
        $mappings = $this->createQuery()->execute(true);
93 1
        $prepared = [];
94 1
        foreach ($mappings as $mapping) {
95 1
            $identifier = sha1($mapping['foreign_table_uid'] . '_' . $mapping['foreign_table_name']);
96 1
            $prepared[$identifier] = $identifier;
97
        }
98 1
        return $prepared;
99
    }
100
}
101