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

updateFeatureFlagStatusForTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 16
rs 9.9
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
44
    public function __construct(FeatureFlagData $featureFlagData)
45
    {
46
        $this->featureFlagData = $featureFlagData;
47
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
48
        parent::__construct($objectManager);
49
    }
50
51
    /**
52
     * @return void
53
     */
54
    public function initializeObject()
55
    {
56
        /** @var $defaultQuerySettings \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings */
57
        $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

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