Passed
Push — issue#777 ( 7b5785...f6ccb8 )
by Guilherme
05:28
created

getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\BadgesControlBundle\Model;
12
13
use LoginCidadao\BadgesControlBundle\Exception\BadgesNameCollisionException;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use LoginCidadao\BadgesControlBundle\BadgesEvents;
16
use LoginCidadao\BadgesControlBundle\Event\ListBadgesEvent;
17
use LoginCidadao\BadgesControlBundle\Event\ListBearersEvent;
18
use LoginCidadao\BadgesControlBundle\Event\EvaluateBadgesEvent;
19
20
abstract class AbstractBadgesEventSubscriber implements EventSubscriberInterface, BadgeEvaluatorInterface
21
{
22
23
    protected $badges = [];
24
    protected $name;
25
26
    abstract public function onBadgeEvaluate(EvaluateBadgesEvent $event);
27
28
    abstract public function onListBearers(ListBearersEvent $event);
29
30 7
    public function setName($name)
31
    {
32 7
        $this->name = $name;
33
34 7
        return $this;
35
    }
36
37 2
    public function getName()
38
    {
39 2
        return $this->name;
40
    }
41
42 1
    public function getAvailableBadges()
43
    {
44 1
        return $this->badges;
45
    }
46
47 2
    public static function getSubscribedEvents()
48
    {
49
        return [
50 2
            BadgesEvents::BADGES_EVALUATE => ['onBadgeEvaluate', 0],
51
            BadgesEvents::BADGES_LIST_AVAILABLE => ['onBadgeListAvailable', 0],
52
            BadgesEvents::BADGES_LIST_BEARERS => ['onListBearersPreFilter', 0],
53
        ];
54
    }
55
56
    /**
57
     * @param ListBadgesEvent $event
58
     * @throws BadgesNameCollisionException
59
     */
60 1
    public function onBadgeListAvailable(ListBadgesEvent $event)
61
    {
62 1
        $event->registerBadges($this);
63 1
    }
64
65
    /**
66
     * This method performs a check to verify if the filtered badge (if present)
67
     * belongs to the namespace of this evaluator.
68
     *
69
     * @param ListBearersEvent $event
70
     */
71 1
    public function onListBearersPreFilter(ListBearersEvent $event)
72
    {
73 1
        $filterBadge = $event->getBadge();
74 1
        if ($filterBadge instanceof BadgeInterface && $filterBadge->getNamespace() === $this->getName()) {
75 1
            $this->onListBearers($event);
76
        }
77 1
    }
78
79 6
    protected function registerBadge($name, $description, $extras = null)
80
    {
81 6
        $this->badges[$name] = array_merge(compact('description'), is_null($extras) ? [] : $extras);
82 6
    }
83
84
}
85