CoreExtension::getAvailableRules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Obblm\Core\Twig;
4
5
use Obblm\Core\Event\TwigCollectorEvent;
6
use Obblm\Core\Helper\CoreTranslation;
7
use Obblm\Core\Helper\LocaleHelper;
8
use Obblm\Core\Helper\RuleHelper;
9
use Obblm\Core\Twig\Parts\NavigationCollection;
10
use Obblm\Core\Twig\Parts\NavigationElementInterface;
11
use Obblm\Core\Twig\Parts\NavigationLink;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\Routing\RouterInterface;
14
use Twig\Extension\AbstractExtension;
15
use Twig\TwigFilter;
16
use Twig\TwigFunction;
17
18
class CoreExtension extends AbstractExtension
19
{
20
    private $ruleHelper;
21
    private $dispatcher;
22
    private $localeHelper;
23
    private $router;
24
25
    public function __construct(RuleHelper $ruleHelper, EventDispatcherInterface $dispatcher, LocaleHelper $localeHelper, RouterInterface $router)
26
    {
27
        $this->ruleHelper = $ruleHelper;
28
        $this->dispatcher = $dispatcher;
29
        $this->localeHelper = $localeHelper;
30
        $this->router = $router;
31
    }
32
33
    public function getFilters()
34
    {
35
        return [
36
            new TwigFilter('is_collection', [$this, 'isCollection']),
37
            new TwigFilter('is_link', [$this, 'isLink']),
38
            new TwigFilter('price', [$this, 'formatPrice']),
39
            new TwigFilter('yesno', [$this, 'formatBooleanToString']),
40
        ];
41
    }
42
43
    public function getFunctions()
44
    {
45
        return [
46
            new TwigFunction('get_navigation_links', [$this, 'getNavigationLinks']),
47
            new TwigFunction('get_admin_links', [$this, 'getAdminLinks']),
48
            new TwigFunction('rules_navigation', [$this, 'getRulesNavigation']),
49
            new TwigFunction('available_rules', [$this, 'getAvailableRules']),
50
            new TwigFunction('get_available_locales', [$this, 'getAvailableLocales']),
51
        ];
52
    }
53
54
    public function getAvailableLocales($current = null)
0 ignored issues
show
Unused Code introduced by
The parameter $current is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

54
    public function getAvailableLocales(/** @scrutinizer ignore-unused */ $current = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        return $this->localeHelper->getLocalizedRoutes();
57
    }
58
59
    public function getRulesNavigation()
60
    {
61
        $rulesCollection = new NavigationCollection('obblm.forms.team.create.select.rules', 'list');
62
        foreach ($this->getAvailableRules() as $rule) {
63
            $ruleName = CoreTranslation::getRuleTitle($rule->getRuleKey());
64
            $rulesCollection->addToCollection(
65
                (new NavigationLink('obblm_team_create_rule', $ruleName, ['rule' => $rule->getId()]))
66
                    ->setTranslationDomain($rule->getRuleKey())
67
            );
68
        }
69
70
        $rulesNavigation = (new NavigationCollection())
71
            ->addToCollection($rulesCollection);
72
        $collector = new TwigCollectorEvent($rulesNavigation);
73
        $this->dispatcher->dispatch($collector, TwigCollectorEvent::COLLECT_TEAM_CREATION_BAR);
74
        return $rulesNavigation->getCollection();
75
    }
76
77
    public function getAvailableRules()
78
    {
79
        return $this->ruleHelper->getRulesAvailableForTeamCreation();
80
    }
81
82
    public function getNavigationLinks():array
83
    {
84
        $collection = new NavigationCollection();
85
        $this->dispatcher->dispatch(new TwigCollectorEvent($collection), TwigCollectorEvent::COLLECT_NAV_BAR);
86
        return $collection->getCollection();
87
    }
88
89
    public function isCollection(NavigationElementInterface $item)
90
    {
91
        return $item instanceof NavigationCollection;
92
    }
93
94
    public function isLink(NavigationElementInterface $item)
95
    {
96
        return $item instanceof NavigationLink;
97
    }
98
99
    public function getAdminLinks():array
100
    {
101
        $collection = new NavigationCollection();
102
        $this->dispatcher->dispatch(new TwigCollectorEvent($collection), TwigCollectorEvent::COLLECT_ADMIN_BAR);
103
        return $collection->getCollection();
104
    }
105
106
    public function formatPrice($number, $decimals = 0, $decPoint = '.', $thousandsSep = ','):string
107
    {
108
        if ($number === '') {
109
            return '';
110
        }
111
        return number_format($number, $decimals, $decPoint, $thousandsSep);
112
    }
113
114
    public function formatBooleanToString(bool $var):string
115
    {
116
        return ($var) ? 'yes' : 'no';
117
    }
118
}
119