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

ControllerNameParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 8
c 5
b 0
f 0
lcom 1
cbo 0
dl 0
loc 77
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 18 3
A build() 0 6 1
A getPartsFromControllerName() 0 21 3
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\Module\Controller;
12
13
use Zend\ModuleManager\ModuleManagerInterface;
14
15
/**
16
 * ControllerNameParser converts controller from the short notation a:b:c
17
 * (BlogModule:Post:index) to a fully-qualified class::method string
18
 * (Module\BlogModule\Controller\PostController::indexAction).
19
 *
20
 * @author     Fabien Potencier <[email protected]>
21
 * @author     Vítor Brandão <[email protected]>
22
 * @author     Paul Dragoonis <[email protected]>
23
 */
24
class ControllerNameParser
25
{
26
    protected $moduleManager;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param ModuleManagerInterface $moduleManager A ModuleManagerInterface instance
32
     */
33
    public function __construct(ModuleManagerInterface $moduleManager)
34
    {
35
        $this->moduleManager = $moduleManager;
36
    }
37
38
    /**
39
     * Converts a short notation a:b:c to a class::method.
40
     *
41
     * @param string $controller A short notation controller (a:b:c)
42
     *
43
     * @throws \InvalidArgumentException when the specified module is not enabled
44
     *                                   or the controller cannot be found
45
     *
46
     * @return string A string with class::method
47
     */
48
    public function parse($controller)
49
    {
50
        list($module, $moduleName, $controller, $action) = $this->getPartsFromControllerName($controller);
51
52
        if (null === $module) {
53
            // this throws an exception if there is no such module
54
            $msg = sprintf('Unable to find controller "%s:%s" - module alias "%s" does not exist.', $moduleName, $controller, $moduleAlias);
0 ignored issues
show
Bug introduced by
The variable $moduleAlias does not exist. Did you mean $module?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
55
        } else {
56
            $class = $module->getNamespace() . '\\Controller\\' . $controller;
57
            if (class_exists($class)) {
58
                return $class . '::' . $action . 'Action';
59
            }
60
61
            $msg = sprintf('Unable to find controller "%s:%s" - class "%s" does not exist.', $moduleName, $controller, $class);
62
        }
63
64
        throw new \InvalidArgumentException($msg);
65
    }
66
67
    public function build($controller)
68
    {
69
        list($module, $moduleName, $controllerName, $actionName) = $this->getPartsFromControllerName($controller);
0 ignored issues
show
Unused Code introduced by
The assignment to $module is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
70
71
        return sprintf('%s:%s:%s', $moduleName, $controllerName, $actionName);
72
    }
73
74
    /**
75
     * @param string $controller
76
     *
77
     * @return array
78
     */
79
    private function getPartsFromControllerName($controller)
80
    {
81
        if (3 != count($parts = explode(':', $controller))) {
82
            throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid a:b:c controller string.', $controller));
83
        }
84
85
        list($moduleName, $controller, $action) = $parts;
86
        $controller = str_replace('/', '\\', $controller);
87
        $module     = $this->moduleManager->getModule($moduleName);
0 ignored issues
show
Bug introduced by
The method getModule() does not exist on Zend\ModuleManager\ModuleManagerInterface. Did you maybe mean getModules()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
88
89
        if (!is_object($module)) {
90
            throw new \RuntimeException(sprintf(
91
                'Unable to locate module: %s, when parsing controller: %s',
92
                $moduleName, $controller
93
            ));
94
        }
95
96
        $moduleName = $module->getName();
97
98
        return [$module, $moduleName, $controller, $action];
99
    }
100
}
101