Completed
Push — master ( d535d3...8c6cf6 )
by Felix
16:53
created

Tx_FeatureFlag_Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
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 5
    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 5
        $this->featureFlagRepository = $featureFlagRepository;
74 5
        $this->persistenceManager = $persistenceManager;
75 5
        $this->configuration = $configuration;
76 5
    }
77
78
    /**
79
     * @param $flag
80
     * @return Tx_FeatureFlag_Domain_Model_FeatureFlag
81
     * @throws Tx_FeatureFlag_Service_Exception_FeatureNotFound
82
     * @return boolean
83
     */
84 3
    protected function getFeatureFlag($flag)
85
    {
86 3
        if (false === array_key_exists($flag, $this->cachedFlags)) {
87 3
            $flagModel = $this->featureFlagRepository->findByFlag($flag);
88 3
            if (false === $flagModel instanceof Tx_FeatureFlag_Domain_Model_FeatureFlag) {
89 1
                throw new Tx_FeatureFlag_Service_Exception_FeatureNotFound('Feature Flag not found: "' . $flag . '"', 1383842028);
90
            }
91 2
            $this->cachedFlags[$flag] = $flagModel;
92 2
        }
93 2
        return $this->cachedFlags[$flag];
94
    }
95
96
    /**
97
     * @param $flag
98
     * @return bool
99
     * @throws Tx_FeatureFlag_Service_Exception_FeatureNotFound
100
     */
101 3
    public function isFeatureEnabled($flag)
102
    {
103 3
        return $this->getFeatureFlag($flag)->isEnabled();
104
    }
105
106
    /**
107
     * @param $flag
108
     * @param $enabled
109
     * @throws Tx_FeatureFlag_Service_Exception_FeatureNotFound
110
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
111
     */
112 1
    public function updateFeatureFlag($flag, $enabled)
113
    {
114 1
        $flagModel = $this->getFeatureFlag($flag);
115 1
        $flagModel->setEnabled($enabled);
116
117 1
        $this->featureFlagRepository->update($flagModel);
118 1
        $this->persistenceManager->persistAll();
119
120 1
        $this->cachedFlags[$flag] = $flagModel;
121 1
    }
122
123
    /**
124
     * Flags entries in database
125
     */
126 1
    public function flagEntries()
127
    {
128 1
        foreach ($this->configuration->getTables() as $table) {
129 1
            $this->featureFlagRepository->updateFeatureFlagStatusForTable($table);
130 1
        }
131
    }
132
}