Completed
Push — master ( 5ea837...9dec3c )
by Paul
9s
created

TemplateNameParser::parse()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 39
rs 5.3846
cc 8
eloc 23
nc 8
nop 1
1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright  Copyright (c) 2011-2016 Paul Dragoonis <[email protected]>
6
 * @license    http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link       http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\View;
12
13
use PPI\Framework\Module\ModuleManager;
14
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser as BaseTemplateNameParser;
15
use Symfony\Component\Templating\TemplateReferenceInterface;
16
17
/**
18
 * TemplateNameParser converts template names from the short notation
19
 * "bundle:section:template.format.engine" to TemplateReferenceInterface
20
 * instances.
21
 *
22
 * @author Fabien Potencier <[email protected]>
23
 * @author     Paul Dragoonis <[email protected]>
24
 */
25
class TemplateNameParser extends BaseTemplateNameParser
26
{
27
    /**
28
     * @var ModuleManager
29
     */
30
    protected $moduleManager;
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param ModuleManager $moduleManager A ModuleManager instance
36
     */
37
    public function __construct(ModuleManager $moduleManager)
38
    {
39
        $this->moduleManager = $moduleManager;
40
        $this->cache         = array();
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function parse($name)
47
    {
48
        if ($name instanceof TemplateReferenceInterface) {
49
            return $name;
50
        } elseif (isset($this->cache[$name])) {
51
            return $this->cache[$name];
52
        }
53
54
        // normalize name
55
        $name = str_replace(':/', ':', preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')));
56
57
        if (false !== strpos($name, '..')) {
58
            throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name));
59
        }
60
61
        $parts = explode(':', $name);
62
        if (3 !== count($parts)) {
63
            throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name));
64
        }
65
66
        $elements = explode('.', $parts[2]);
67
        if (3 > count($elements)) {
68
            throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name));
69
        }
70
        $engine = array_pop($elements);
71
        $format = array_pop($elements);
72
73
        $template = new TemplateReference($parts[0], $parts[1], implode('.', $elements), $format, $engine);
74
75
        if ($template->get('module')) {
76
            try {
77
                $this->moduleManager->getModule($template->get('module'));
0 ignored issues
show
Unused Code introduced by
The call to the method PPI\Framework\Module\ModuleManager::getModule() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
78
            } catch (\Exception $e) {
79
                throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name), 0, $e);
80
            }
81
        }
82
83
        return $this->cache[$name] = $template;
84
    }
85
}
86