FeatureFlagRepository::initializeObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Aoe\FeatureFlag\Domain\Repository;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2024 AOE GmbH <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
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\Persistence\Generic\Typo3QuerySettings;
33
use TYPO3\CMS\Extbase\Persistence\Repository;
34
35
class FeatureFlagRepository extends Repository
36
{
37
    public function __construct(
38
        private readonly FeatureFlagData $featureFlagData
39
    ) {
40
        parent::__construct();
41
    }
42
43
    public function initializeObject(): void
44
    {
45
        /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings $defaultQuerySettings */
46
        $defaultQuerySettings = GeneralUtility::makeInstance(Typo3QuerySettings::class);
47
        $defaultQuerySettings->setRespectStoragePage(false);
48
        $defaultQuerySettings->setRespectSysLanguage(false);
49
        $this->setDefaultQuerySettings($defaultQuerySettings);
50
    }
51
52
    public function findByFlag(string $flag): ?object
53
    {
54
        $query = $this->createQuery();
55
        $query->getQuerySettings()
56
            ->setRespectSysLanguage(false);
57
        $query->getQuerySettings()
58
            ->setRespectStoragePage(false);
59
        $query->matching($query->equals('flag', $flag));
60
61
        return $query->execute()
62
            ->getFirst();
63
    }
64
65
    public function updateFeatureFlagStatusForTable(string $table): void
66
    {
67
        $this->hideEntries(
68
            $table,
69
            $this->getUpdateEntriesUids($table, FeatureFlagService::BEHAVIOR_HIDE, 1)
70
        );
71
        $this->hideEntries(
72
            $table,
73
            $this->getUpdateEntriesUids($table, FeatureFlagService::BEHAVIOR_SHOW, 0)
74
        );
75
        $this->showEntries(
76
            $table,
77
            $this->getUpdateEntriesUids($table, FeatureFlagService::BEHAVIOR_SHOW, 1)
78
        );
79
        $this->showEntries(
80
            $table,
81
            $this->getUpdateEntriesUids($table, FeatureFlagService::BEHAVIOR_HIDE, 0)
82
        );
83
    }
84
85
    private function hideEntries(string $table, array $uids): void
86
    {
87
        if ($uids !== []) {
88
            $this->featureFlagData->updateContentElements($table, $uids, false);
89
        }
90
    }
91
92
    private function showEntries(string $table, array $uids): void
93
    {
94
        if ($uids !== []) {
95
            $this->featureFlagData->updateContentElements($table, $uids, true);
96
        }
97
    }
98
99
    private function getUpdateEntriesUids(string $table, int $behavior, int $enabled): array
100
    {
101
        $rows = $this->featureFlagData->getContentElements($table, $behavior, $enabled);
102
103
        if ($rows === []) {
104
            return $rows;
105
        }
106
107
        $uids = [];
108
        foreach ($rows as $row) {
109
            $uids[] = $row['uid'];
110
        }
111
112
        return $uids;
113
    }
114
}
115