Passed
Push — refactoring/typo3v10 ( e1b223...e74936 )
by Felix
30:10 queued 24:35
created

FeatureFlagRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 46.67%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 105
ccs 21
cts 45
cp 0.4667
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hideEntries() 0 4 2
A showEntries() 0 4 2
A findByFlag() 0 8 1
A getUpdateEntriesUids() 0 13 3
A initializeObject() 0 7 1
A updateFeatureFlagStatusForTable() 0 16 1
A __construct() 0 5 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\FeatureFlag;
29
use Aoe\FeatureFlag\Service\FeatureFlagService;
30
use Aoe\FeatureFlag\System\Db\FeatureFlagData;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
use TYPO3\CMS\Extbase\Object\ObjectManager;
33
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
34
use TYPO3\CMS\Extbase\Persistence\Repository;
35
36
class FeatureFlagRepository extends Repository
37
{
38
    /**
39
     * @var FeatureFlagData
40
     */
41
    private $featureFlagData;
42
43
    public function __construct(FeatureFlagData $featureFlagData)
44
    {
45
        $this->featureFlagData = $featureFlagData;
46
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
47
        parent::__construct($objectManager);
48
    }
49
50
    /**
51
     * @return void
52
     */
53
    public function initializeObject()
54
    {
55
        /** @var $defaultQuerySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */
56
        $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

56
        $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...
57
        $defaultQuerySettings->setRespectStoragePage(false);
58
        $defaultQuerySettings->setRespectSysLanguage(false);
59
        $this->setDefaultQuerySettings($defaultQuerySettings);
60
    }
61
62
    /**
63
     * @param string $flag
64
     * @return FeatureFlag
65
     */
66 1
    public function findByFlag($flag)
67
    {
68 1
        $query = $this->createQuery();
69 1
        $query->getQuerySettings()->setRespectSysLanguage(false);
70 1
        $query->getQuerySettings()->setRespectStoragePage(false);
71 1
        $query->matching($query->equals('flag', $flag));
72
73 1
        return $query->execute()->getFirst();
74
    }
75
76
    /**
77
     * @param string $table
78
     */
79
    public function updateFeatureFlagStatusForTable($table)
80
    {
81
        $this->hideEntries(
82
            $table,
83
            $this->getUpdateEntriesUids($table, FeatureFlagService::BEHAVIOR_HIDE, 1)
84
        );
85
        $this->hideEntries(
86
            $table,
87
            $this->getUpdateEntriesUids($table, FeatureFlagService::BEHAVIOR_SHOW, 0)
88
        );
89
        $this->showEntries(
90
            $table,
91
            $this->getUpdateEntriesUids($table, FeatureFlagService::BEHAVIOR_SHOW, 1)
92
        );
93
        $this->showEntries($table,
94
            $this->getUpdateEntriesUids($table, FeatureFlagService::BEHAVIOR_HIDE, 0)
95
        );
96
    }
97
98
    /**
99
     * @param string $table
100
     * @param array $uids
101
     * @return void
102
     */
103 4
    private function hideEntries($table, array $uids)
104
    {
105 4
        if (!empty($uids)) {
106 2
            $this->featureFlagData->updateContentElements($table, $uids, false);
107
        }
108 4
    }
109
110
    /**
111
     * @param string $table
112
     * @param array $uids
113
     * @return void
114
     */
115 4
    private function showEntries($table, array $uids)
116
    {
117 4
        if (!empty($uids)) {
118 2
            $this->featureFlagData->updateContentElements($table, $uids, true);
119
        }
120 4
    }
121
122
    /**
123
     * @param string $table
124
     * @param int $behavior
125
     * @param int $enabled
126
     * @return array
127
     */
128 4
    private function getUpdateEntriesUids($table, $behavior, $enabled)
129
    {
130 4
        $rows = $this->featureFlagData->getContentElements($table, $behavior, $enabled);
131
132 4
        if(empty($rows)) {
133 4
            return $rows;
134
        }
135
136 4
        foreach ($rows as $row) {
137 4
            $uids[] = $row['uid'];
138
        }
139
140 4
        return $uids;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uids seems to be defined by a foreach iteration on line 136. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
141
    }
142
}
143