ToggleListener::onClassParsed()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 18
cp 0
rs 7.7777
cc 8
eloc 11
nc 9
nop 1
crap 72
1
<?php
2
/**
3
 * zf2-featureflags.
4
 *
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
11
 * SOFTWARE.
12
 *
13
 * @copyright 2016 MehrAlsNix (http://www.mehralsnix.de)
14
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
15
 *
16
 * @link      http://github.com/MehrAlsNix/zf2-featureflags
17
 */
18
19
namespace MehrAlsNix\FeatureToggle\Listener;
20
21
use MehrAlsNix\FeatureToggle\Annotation\Toggle;
22
use MehrAlsNix\FeatureToggle\Traits\ToggleTrait;
23
use Zend\EventManager\AbstractListenerAggregate;
24
use Zend\EventManager\EventManagerInterface;
25
use ZfAnnotation\Event\ParseEvent;
26
27
/**
28
 * Abstract aggregate listener
29
 */
30
class ToggleListener extends AbstractListenerAggregate
31
{
32
    use ToggleTrait;
33
34
    /**
35
     * @param EventManagerInterface $events
36
     */
37
    public function attach(EventManagerInterface $events, $priority = 1)
38
    {
39
        $this->listeners[] = $events->attach(ParseEvent::EVENT_CLASS_PARSED, [$this, 'onClassParsed']);
40
    }
41
42
    public function onClassParsed(ParseEvent $event)
43
    {
44
        $classHolder = $event->getTarget();
45
        $classAnnotations = $classHolder->getAnnotations();
46
        foreach ($classAnnotations as $annotation) {
47
            if ($annotation instanceof Toggle && !$this->isActive($annotation->getName())) {
0 ignored issues
show
Bug introduced by
Consider using $annotation->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
48
                throw new \RuntimeException();
49
            }
50
        }
51
52
        $methodHolders = $classHolder->getMethods();
53
        foreach ($methodHolders as $methodHolder) {
54
            foreach ($methodHolder->getAnnotations() as $annotation) {
55
                if ($annotation instanceof Toggle && !$this->isActive($annotation->getName())) {
0 ignored issues
show
Bug introduced by
Consider using $annotation->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
56
                    throw new \RuntimeException();
57
                }
58
            }
59
        }
60
    }
61
}
62