Passed
Branch main (a47c5a)
by AOEPeople
15:53 queued 02:59
created

Tx_FeatureFlag_Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 2
b 0
f 1
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
/***************************************************************
4
 *  Copyright notice
5
 *
6
 *  (c) 2016 AOE GmbH <[email protected]>
7
 *
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
/**
28
 * @package FeatureFlag
29
 */
30
class Tx_FeatureFlag_Service
31
{
32
    /**
33
     * @var int
34
     */
35
    const BEHAVIOR_HIDE = 0;
36
37
    /**
38
     * @var int
39
     */
40
    const BEHAVIOR_SHOW = 1;
41
42
    /**
43
     * @var Tx_FeatureFlag_Domain_Repository_FeatureFlag
44
     */
45
    private $featureFlagRepository;
46
47
    /**
48
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
49
     */
50
    private $persistenceManager;
51
52
    /**
53
     * @var Tx_FeatureFlag_System_Typo3_Configuration
54
     */
55
    private $configuration;
56
57
    /**
58
     * @var array
59
     */
60
    private $cachedFlags = array();
61
62
    /**
63
     * Tx_FeatureFlag_Service constructor.
64
     * @param Tx_FeatureFlag_Domain_Repository_FeatureFlag $featureFlagRepository
65
     * @param \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager
66
     * @param Tx_FeatureFlag_System_Typo3_Configuration $configuration
67
     */
68
    public function __construct(
69
        Tx_FeatureFlag_Domain_Repository_FeatureFlag $featureFlagRepository,
70
        \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $persistenceManager,
71
        Tx_FeatureFlag_System_Typo3_Configuration $configuration
72
    ) {
73
        $this->featureFlagRepository = $featureFlagRepository;
74
        $this->persistenceManager = $persistenceManager;
75
        $this->configuration = $configuration;
76
    }
77
78
    /**
79
     * @param string $flag
80
     * @return Tx_FeatureFlag_Domain_Model_FeatureFlag
81
     * @throws Tx_FeatureFlag_Service_Exception_FeatureNotFound
82
     * @throws RuntimeException
83
     * @return boolean
84
     */
85
    protected function getFeatureFlag($flag)
86
    {
87
        if (false === is_array($GLOBALS['TCA']) || false === isset($GLOBALS['TCA']['tx_featureflag_domain_model_featureflag'])) {
88
            // This can happen, when we call a REST-endpoint (by using restler-extension without initialized FE) and TYPO3 7:
89
            // Without TCA, we would load (in this and ALL other following PHP-requests) the featureFlag without initialized
90
            // 'property-values' (method 'Tx_FeatureFlag_Domain_Model_FeatureFlag::isEnabled' would return NULL), so we MUST
91
            // avoid to load any featureFlag without correct loaded TCA!
92
            throw new RuntimeException('TCA is not loaded - we can\'t load featureFlag "'.$flag.'"');
93
        }
94
95
        if (false === array_key_exists($flag, $this->cachedFlags)) {
96
            $flagModel = $this->featureFlagRepository->findByFlag($flag);
97
            if (false === $flagModel instanceof Tx_FeatureFlag_Domain_Model_FeatureFlag) {
98
                throw new Tx_FeatureFlag_Service_Exception_FeatureNotFound('Feature Flag not found: "' . $flag . '"', 1383842028);
99
            }
100
            $this->cachedFlags[$flag] = $flagModel;
101
        }
102
        return $this->cachedFlags[$flag];
103
    }
104
105
    /**
106
     * @param $flag
107
     * @return bool
108
     * @throws Tx_FeatureFlag_Service_Exception_FeatureNotFound
109
     */
110
    public function isFeatureEnabled($flag)
111
    {
112
        return $this->getFeatureFlag($flag)->isEnabled();
113
    }
114
115
    /**
116
     * @param $flag
117
     * @param $enabled
118
     * @throws Tx_FeatureFlag_Service_Exception_FeatureNotFound
119
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
120
     */
121
    public function updateFeatureFlag($flag, $enabled)
122
    {
123
        $flagModel = $this->getFeatureFlag($flag);
124
        $flagModel->setEnabled($enabled);
125
126
        $this->featureFlagRepository->update($flagModel);
127
        $this->persistenceManager->persistAll();
128
129
        $this->cachedFlags[$flag] = $flagModel;
130
    }
131
132
    /**
133
     * Flags entries in database
134
     */
135
    public function flagEntries()
136
    {
137
        foreach ($this->configuration->getTables() as $table) {
138
            $this->featureFlagRepository->updateFeatureFlagStatusForTable($table);
139
        }
140
    }
141
}