ViewHelperPlugin::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 4
nop 3
1
<?php
2
namespace Fwk\Core\Plugins;
3
4
use Fwk\Core\Application;
5
use Fwk\Core\Components\ViewHelper\EmbedViewHelper;
6
use Fwk\Core\Components\ViewHelper\EscapeViewHelper;
7
use Fwk\Core\Components\ViewHelper\ViewHelperListener;
8
use Fwk\Core\Components\ViewHelper\ViewHelperService;
9
use Fwk\Core\Components\UrlRewriter\UrlViewHelper;
10
use Fwk\Core\Plugin;
11
use Fwk\Di\ClassDefinition;
12
use Fwk\Di\Container;
13
14
class ViewHelperPlugin implements Plugin
15
{
16
    const SERVICE_NAME = 'viewHelper';
17
18
    /**
19
     * List of Default ViewHelpers
20
     * @var array
21
     */
22
    private $helpers = array();
23
24
    private $propName = ViewHelperService::DEFAULT_PROP_NAME;
25
26
    private $throwExceptions = true;
27
28
    /**
29
     */
30
    public function __construct(array $helpers = null, $propName = null, $throwExceptions = true)
31
    {
32
        if (null === $helpers) {
33
            $helpers = $this->getDefaultViewHelpers();
34
        }
35
36
        if (null !== $propName) {
37
            $this->propName = $propName;
38
        }
39
40
        $this->throwExceptions = $throwExceptions;
41
        $this->helpers = $helpers;
42
    }
43
44
    /**
45
     * Apply Plugin's services to the existing Container
46
     *
47
     * @param Container $container App's Services Container
48
     *
49
     * @return void
50
     */
51
    public function loadServices(Container $container)
52
    {
53
        $definition = new ClassDefinition('\Fwk\Core\Components\ViewHelper\ViewHelperService', array(
54
            $this->propName,
55
            $this->throwExceptions
56
        ), true);
0 ignored issues
show
Unused Code introduced by
The call to ClassDefinition::__construct() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
57
58
        foreach ($this->helpers as $name => $helper) {
59
            $definition->addMethodCall('add', array($name, $helper));
60
        }
61
62
        $container->set(
63
            self::SERVICE_NAME,
64
            $definition,
65
            true
66
        );
67
    }
68
69
    /**
70
     * Returns a list of Actions for this plugin
71
     *
72
     * @param Application $app The running Application
73
     *
74
     * @return void
75
     */
76
    public function load(Application $app)
77
    {
78
        $app->addListener(new ViewHelperListener(self::SERVICE_NAME));
79
    }
80
81
    protected function getDefaultViewHelpers()
82
    {
83
        return array(
84
            'url'       => new UrlViewHelper(RequestMatcherPlugin::SERVICE_NAME, UrlRewriterPlugin::SERVICE_NAME),
85
            'embed'     => new EmbedViewHelper(),
86
            'escape'    => new EscapeViewHelper()
87
        );
88
    }
89
}