ProxyFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 3
Metric Value
wmc 9
c 4
b 1
f 3
cbo 6
dl 0
loc 60
ccs 22
cts 22
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D factory() 0 31 9
1
<?php
2
/**
3
 * Fwk
4
 *
5
 * Copyright (c) 2011-2012, Julien Ballestracci <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
12
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
15
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
17
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
21
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22
 * POSSIBILITY OF SUCH DAMAGE.
23
 *
24
 * PHP Version 5.3
25
 *
26
 * @category  Core
27
 * @package   Fwk\Core
28
 * @author    Julien Ballestracci <[email protected]>
29
 * @copyright 2011-2014 Julien Ballestracci <[email protected]>
30
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
31
 * @link      http://www.fwk.pw
32
 */
33
namespace Fwk\Core\Action;
34
35
use Fwk\Core\ActionProxy;
36
use Fwk\Core\Exception;
37
38
/**
39
 * Strategy Factory utility to help loading the correct ActionProxy depending
40
 * on the argument passed to the factory() method.
41
 *
42
 * @category ActionProxy
43
 * @package  Fwk\Core
44
 * @author   Julien Ballestracci <[email protected]>
45
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD License
46
 * @link     http://www.fwk.pw
47
 */
48
class ProxyFactory
0 ignored issues
show
Coding Style introduced by
ProxyFactory does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
49
{
50
    /**
51
     * Factory Strategy to load the correct ActionProxy. A sort of "shortcut
52
     * utility".
53
     * 
54
     * If $callableOrShortcut is:<br />
55
     * <ol>
56
     * <li>a <i>\Closure</i> or a <i>Callable</i> will return a 
57
     * <i>CallableActionProxy</i>
58
     * <br />(ex: array($object, 'method')</li>
59
     * <li>a <i>string starting with '@' followed by a name WITHOUT ':' in it</i>
60
     * will return a <i>ServiceActionProxy</i><br />(ex: @MyServiceReference)</li>
61
     * <li><i>string starting with '@' followed by a name WITH ':' in it</i>
62
     * will return a <i>ServiceControllerActionProxy</i> 
63
     * <br />(ex: @MyControllerReference:methodName)</li>
64
     * <li><i>string WITH ':' in it</i> will return a 
65
     * <i>ControllerActionProxy</i><br />(ex: MyApp\\Controllers\\Home:show)</li>
66
     * <li>a <i>string starting with '+' followed by a path to a php file</i>
67
     * will return an <i>IncludeActionProxy</i><br />
68
     * (ex: +/path/to/myscript.php)</li>
69
     * </ol>
70
     * 
71
     * @param mixed $callableOrShortcut See documentation above
72
     * 
73
     * @return ActionProxy The according ActionProxy instance
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use CallableActionProxy|Serv...roxy|IncludeActionProxy.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
74
     * @throws Exception   When the shortcut is incorrect
75
     */
76 35
    public static function factory($callableOrShortcut)
77
    {
78
        if ($callableOrShortcut instanceof \Closure 
79 35
            || is_callable($callableOrShortcut)
80 35
        ) {
81 27
            return new CallableActionProxy($callableOrShortcut);
82 24
        } elseif (is_string($callableOrShortcut)) {
83 23
            if (strpos($callableOrShortcut, '@', 0) !== false) {
84 19
                if (strpos($callableOrShortcut, ':') === false) {
85 19
                    return new ServiceActionProxy(substr($callableOrShortcut, 1));
86 16
                } elseif (strpos($callableOrShortcut, ':') !== false) {
87 16
                    list($service, $method) = explode(':', $callableOrShortcut);
88 16
                    return new ServiceControllerActionProxy(
89 16
                        substr($service, 1), 
90
                        $method
91 16
                    );
92
                }
93 23
            } elseif (strpos($callableOrShortcut, ':') !== false) {
94 19
                list($className, $method) = explode(':', $callableOrShortcut);
95 19
                return new ControllerActionProxy($className, $method);
96 4
            } elseif (strpos($callableOrShortcut, '+', 0) !== false) {
97 3
                return new IncludeActionProxy(substr($callableOrShortcut, 1));
98
            }
99
            
100 1
            throw new Exception(
101 1
                sprintf('Invalid ActionProxy shortcut: '. $callableOrShortcut)
102 1
            );
103
        }
104
        
105 1
        throw new Exception(sprintf('Incorrect ActionProxy shortcut'));
106
    }
107
}
108