Passed
Push — main ( 400cf4...75e45b )
by Felix
03:05
created

findOneByForeignTableNameAndUid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Aoe\FeatureFlag\Domain\Repository;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2021 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\FeatureFlag\Domain\Model\Mapping;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
32
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
33
use TYPO3\CMS\Extbase\Persistence\Repository;
34
35
class MappingRepository extends Repository
36
{
37
    public function initializeObject()
38
    {
39
        /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings $defaultQuerySettings */
40
        $defaultQuerySettings = GeneralUtility::makeInstance(Typo3QuerySettings::class);
41
        $defaultQuerySettings->setRespectStoragePage(false);
42
        $defaultQuerySettings->setRespectSysLanguage(false);
43
        $defaultQuerySettings->setIgnoreEnableFields(false)
44
            ->setIncludeDeleted(false);
45
        $this->setDefaultQuerySettings($defaultQuerySettings);
46
    }
47
48
    /**
49
     * @param int $foreignTableUid
50
     * @param string $foreignTableName
51
     * @return Mapping
52
     */
53
    public function findOneByForeignTableNameAndUid($foreignTableUid, $foreignTableName)
54
    {
55
        return $this->findAllByForeignTableNameAndUid($foreignTableUid, $foreignTableName)
56
            ->getFirst();
57
    }
58
59
    /**
60
     * @param int $foreignTableUid
61
     * @param string $foreignTableName
62
     * @return QueryResultInterface
63
     */
64
    public function findAllByForeignTableNameAndUid($foreignTableUid, $foreignTableName)
65
    {
66
        $query = $this->createQuery();
67
        $query->matching(
68
            $query->logicalAnd(
69
                $query->equals('foreign_table_uid', $foreignTableUid),
70
                $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

70
            $query->/** @scrutinizer ignore-call */ 
71
                    logicalAnd(

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...
71
            )
72
        );
73
        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...
74
    }
75
76
    /**
77
     * @param $featureFlagId
78
     * @return array|QueryResultInterface
79
     */
80
    public function findAllByFeatureFlag($featureFlagId)
81
    {
82
        $query = $this->createQuery();
83
        $query->getQuerySettings()
84
            ->setRespectSysLanguage(false);
85
        $query->getQuerySettings()
86
            ->setRespectStoragePage(false);
87
        $query->matching($query->equals('feature_flag', $featureFlagId));
88
        return $query->execute();
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function getHashedMappings()
95
    {
96
        $mappings = $this->createQuery()
97
            ->execute(true);
98
        $prepared = [];
99
        foreach ($mappings as $mapping) {
100
            $identifier = sha1($mapping['foreign_table_uid'] . '_' . $mapping['foreign_table_name']);
101
            $prepared[$identifier] = $identifier;
102
        }
103
        return $prepared;
104
    }
105
}
106