Passed
Push — refactoring/typo3v10 ( fa15e2...b994b5 )
by Felix
21:19
created

MappingRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 65
ccs 0
cts 37
cp 0
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeObject() 0 8 1
A findAllByForeignTableNameAndUid() 0 8 1
A findAllByFeatureFlag() 0 7 1
A getHashedMappings() 0 9 2
A findOneByForeignTableNameAndUid() 0 3 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\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Extbase\Object\ObjectManager;
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
    /**
38
     * @return void
39
     */
40
    public function initializeObject()
41
    {
42
        /** @var $defaultQuerySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */
43
        $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

43
        $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...
44
        $defaultQuerySettings->setRespectStoragePage(false);
45
        $defaultQuerySettings->setRespectSysLanguage(false);
46
        $defaultQuerySettings->setIgnoreEnableFields(false)->setIncludeDeleted(false);
47
        $this->setDefaultQuerySettings($defaultQuerySettings);
48
    }
49
50
    /**
51
     * @param int $foreignTableUid
52
     * @param string $foreignTableName
53
     * @return Mapping
54
     */
55
    public function findOneByForeignTableNameAndUid($foreignTableUid, $foreignTableName)
56
    {
57
        return $this->findAllByForeignTableNameAndUid($foreignTableUid, $foreignTableName)->getFirst();
58
    }
59
60
    /**
61
     * @param int $foreignTableUid
62
     * @param string $foreignTableName
63
     * @return QueryResultInterface
64
     */
65
    public function findAllByForeignTableNameAndUid($foreignTableUid, $foreignTableName)
66
    {
67
        $query = $this->createQuery();
68
        $query->matching(
69
            $query->logicalAnd($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($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...
71
        );
72
        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...
73
    }
74
75
    /**
76
     * @param $featureFlagId
77
     * @return array|QueryResultInterface
78
     */
79
    public function findAllByFeatureFlag($featureFlagId)
80
    {
81
        $query = $this->createQuery();
82
        $query->getQuerySettings()->setRespectSysLanguage(false);
83
        $query->getQuerySettings()->setRespectStoragePage(false);
84
        $query->matching($query->equals('feature_flag', $featureFlagId));
85
        return $query->execute();
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function getHashedMappings()
92
    {
93
        $mappings = $this->createQuery()->execute(true);
94
        $prepared = [];
95
        foreach ($mappings as $mapping) {
96
            $identifier = sha1($mapping['foreign_table_uid'] . '_' . $mapping['foreign_table_name']);
97
            $prepared[$identifier] = $identifier;
98
        }
99
        return $prepared;
100
    }
101
}
102