Passed
Push — main ( d4334e...7381d9 )
by Felix
09:59
created

MappingRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 65
ccs 0
cts 38
cp 0
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findAllByForeignTableNameAndUid() 0 8 1
A findAllByFeatureFlag() 0 7 1
A getHashedMappings() 0 9 2
A findOneByForeignTableNameAndUid() 0 3 1
A initializeObject() 0 8 1
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\Extbase\Persistence\Generic\Typo3QuerySettings;
30
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
31
use TYPO3\CMS\Extbase\Persistence\Repository;
32
33
class MappingRepository extends Repository
34
{
35
    /**
36
     * @return void
37
     */
38
    public function initializeObject()
39
    {
40
        /** @var $defaultQuerySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */
41
        $defaultQuerySettings = $this->objectManager->get(Typo3QuerySettings::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

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

41
        $defaultQuerySettings = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(Typo3QuerySettings::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
42
        $defaultQuerySettings->setRespectStoragePage(false);
43
        $defaultQuerySettings->setRespectSysLanguage(false);
44
        $defaultQuerySettings->setIgnoreEnableFields(false)->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)->getFirst();
56
    }
57
58
    /**
59
     * @param int $foreignTableUid
60
     * @param string $foreignTableName
61
     * @return QueryResultInterface
62
     */
63
    public function findAllByForeignTableNameAndUid($foreignTableUid, $foreignTableName)
64
    {
65
        $query = $this->createQuery();
66
        $query->matching(
67
            $query->logicalAnd($query->equals('foreign_table_uid', $foreignTableUid),
68
            $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

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