Passed
Push — refactoring/typo3v10 ( f3523a...0d15b9 )
by Felix
21:30
created

MappingRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeObject() 0 8 1
A findAllByForeignTableNameAndUid() 0 8 1
A findAllByFeatureFlag() 0 7 1
A __construct() 0 4 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
     *
39
     */
40
    public function __construct()
41
    {
42
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
43
        parent::__construct($objectManager);
44
    }
45
46
    /**
47
     * @return void
48
     */
49
    public function initializeObject()
50
    {
51
        /** @var $defaultQuerySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */
52
        $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

52
        $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...
53
        $defaultQuerySettings->setRespectStoragePage(false);
54
        $defaultQuerySettings->setRespectSysLanguage(false);
55
        $defaultQuerySettings->setIgnoreEnableFields(false)->setIncludeDeleted(false);
56
        $this->setDefaultQuerySettings($defaultQuerySettings);
57
    }
58
59
    /**
60
     * @param $foreignTableUid
61
     * @param $foreignTableName
62
     * @return Mapping
63
     */
64
    public function findOneByForeignTableNameAndUid($foreignTableUid, $foreignTableName)
65
    {
66
        return $this->findAllByForeignTableNameAndUid($foreignTableUid, $foreignTableName)->getFirst();
67
    }
68
69
    /**
70
     * @param $foreignTableUid
71
     * @param $foreignTableName
72
     * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
73
     */
74
    public function findAllByForeignTableNameAndUid($foreignTableUid, $foreignTableName)
75
    {
76
        $query = $this->createQuery();
77
        $query->matching(
78
            $query->logicalAnd($query->equals('foreign_table_uid', $foreignTableUid),
79
            $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

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