Passed
Push — TYPO3_11 ( e86067...d12e01 )
by Felix
07:45
created

FeatureFlagRepository::findByFlag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 = GeneralUtility::makeInstance(Typo3QuerySettings::class);
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
    }
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
    }
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