Completed
Pull Request — master (#165)
by Paul
03:21
created

RoutePluginManager::setInvokableClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 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\Router;
12
13
use Zend\ServiceManager\AbstractPluginManager;
14
use Zend\ServiceManager\Exception\RuntimeException;
15
16
/**
17
 * Plugin manager implementation for routes.
18
 *
19
 * Enforces that routes retrieved are instances of RouteInterface. It overrides
20
 * createFromInvokable() to call the route's factory method in order to get an
21
 * instance. The manager is marked to not share by default, in order to allow
22
 * multiple route instances of the same type.
23
 */
24
class RoutePluginManager extends AbstractPluginManager
25
{
26
    /**
27
     * Do not share instances.
28
     *
29
     * @var bool
30
     */
31
    protected $shareByDefault = false;
32
33
    /**
34
     * Override setInvokableClass().
35
     *
36
     * Performs normal operation, but also auto-aliases the class name to the
37
     * service name. This ensures that providing the FQCN does not trigger an
38
     * abstract factory later.
39
     *
40
     * @param string    $name
41
     * @param string    $invokableClass
42
     * @param null|bool $shared
43
     *
44
     * @return RoutePluginManager
45
     */
46
    public function setInvokableClass($name, $invokableClass, $shared = null)
47
    {
48
        parent::setInvokableClass($name, $invokableClass, $shared);
49
        if ($name != $invokableClass) {
50
            $this->setAlias($invokableClass, $name);
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * Validate the plugin.
58
     *
59
     * Checks that the filter loaded is either a valid callback or an instance
60
     * of FilterInterface.
61
     *
62
     * @param mixed $plugin
63
     *
64
     * @throws RuntimeException if invalid
65
     */
66
    public function validatePlugin($plugin)
67
    {
68
        if ($plugin instanceof RouteInterface) {
0 ignored issues
show
Bug introduced by
The class PPI\Framework\Router\RouteInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
69
            // we're okay
70
            return;
71
        }
72
73
        throw new RuntimeException(sprintf(
74
            'Plugin of type %s is invalid; must implement %s\RouteInterface',
75
            (is_object($plugin) ? get_class($plugin) : gettype($plugin)),
76
            __NAMESPACE__
77
        ));
78
    }
79
80
    /**
81
     * Attempt to create an instance via an invokable class.
82
     *
83
     * Overrides parent implementation by invoking the route factory,
84
     * passing $creationOptions as the argument.
85
     *
86
     * @param string $canonicalName
87
     * @param string $requestedName
88
     *
89
     * @throws Exception\RuntimeException If resolved class does not exist, or does not implement RouteInterface
90
     *
91
     * @return null|\stdClass
92
     */
93
    protected function createFromInvokable($canonicalName, $requestedName)
94
    {
95
        $invokable = $this->invokableClasses[$canonicalName];
96
        if (!class_exists($invokable)) {
97
            throw new RuntimeException(sprintf(
98
                '%s: failed retrieving "%s%s" via invokable class "%s"; class does not exist',
99
                __METHOD__,
100
                $canonicalName,
101
                ($requestedName ? '(alias: ' . $requestedName . ')' : ''),
102
                $invokable
103
            ));
104
        }
105
106
        if (!static::isSubclassOf($invokable, __NAMESPACE__ . '\RouteInterface')) {
0 ignored issues
show
Deprecated Code introduced by
The method Zend\ServiceManager\ServiceManager::isSubclassOf() has been deprecated with message: since zf 2.3 requires PHP >= 5.3.23 this method is being deprecated as of zendframework 2.2, and may be removed in future major versions

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
107
            throw new RuntimeException(sprintf(
108
                '%s: failed retrieving "%s%s" via invokable class "%s"; class does not implement %s\RouteInterface',
109
                __METHOD__,
110
                $canonicalName,
111
                ($requestedName ? '(alias: ' . $requestedName . ')' : ''),
112
                $invokable,
113
                __NAMESPACE__
114
            ));
115
        }
116
117
        return $invokable::factory($this->creationOptions);
118
    }
119
}
120