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

ModuleManager::locateResource()   C

Complexity

Conditions 16
Paths 118

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 52
rs 5.4897
cc 16
eloc 32
nc 118
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
12
13
use Zend\ModuleManager\ModuleManager as BaseModuleManager;
14
15
/**
16
 * ModuleManager.
17
 *
18
 * @author     Paul Dragoonis <[email protected]>
19
 * @author     Vítor Brandão <[email protected]>
20
 */
21
class ModuleManager extends BaseModuleManager
22
{
23
    /**
24
     * Returns an array of paths to modules.
25
     *
26
     * @return array An array of paths to each loaded module
27
     */
28 View Code Duplication
    public function getModulesPath()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        $paths = array();
31
        foreach ($this->getLoadedModules(true) as $module) {
32
            $paths[$module->getName()] = $module->getPath();
33
        }
34
35
        return $paths;
36
    }
37
38
    /**
39
     * Returns the file path for a given resource.
40
     *
41
     * A Resource can be a file or a directory.
42
     *
43
     * The resource name must follow the following pattern:
44
     *
45
     *     @<ModuleName>/path/to/a/file.something
46
     *
47
     * where ModuleName is the name of the module
48
     * and the remaining part is the relative path in the module.
49
     *
50
     * If $dir is passed, and the first segment of the path is "Resources",
51
     * this method will look for a file named:
52
     *
53
     *     $dir/<ModuleName>/path/without/Resources
54
     *
55
     * before looking in the module resource folder.
56
     *
57
     * @param string $name  A resource name to locate
58
     * @param string $dir   A directory where to look for the resource first
59
     * @param bool   $first Whether to return the first path or paths for all matching modules
60
     *
61
     * @throws \InvalidArgumentException if the file cannot be found or the name is not valid
62
     * @throws \RuntimeException         if the name contains invalid/unsafe
63
     * @throws \RuntimeException         if a custom resource is hidden by a resource in a derived module
64
     *
65
     * @return string|array The absolute path of the resource or an array if $first is false
66
     *
67
     * @api
68
     */
69
    public function locateResource($name, $dir = null, $first = true)
70
    {
71
        if ('@' !== $name[0]) {
72
            throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name));
73
        }
74
75
        if (false !== strpos($name, '..')) {
76
            throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name));
77
        }
78
        $moduleName = substr($name, 1);
79
        $path       = '';
80
        if (false !== strpos($moduleName, '/')) {
81
            list($moduleName, $path) = explode('/', $moduleName, 2);
82
        }
83
84
        $isResource     = 0 === strpos($path, 'Resources') && null !== $dir;
85
        $overridePath   = substr($path, 9);
86
        $resourceModule = null;
87
        $modules        = array($this->getModule($moduleName));
88
        $files          = array();
89
90
        foreach ($modules as $module) {
91
            if ($isResource && file_exists($file = $dir . '/' . $module->getName() . $overridePath)) {
92
                if (null !== $resourceModule) {
93
                    throw new \RuntimeException(sprintf('"%s" resource is hidden by a resource from the "%s" derived module. Create a "%s" file to override the module resource.',
94
                        $file,
95
                        $resourceModule,
96
                        $dir . '/' . $modules[0]->getName() . $overridePath
97
                    ));
98
                }
99
100
                if ($first) {
101
                    return $file;
102
                }
103
                $files[] = $file;
104
            }
105
106
            if (file_exists($file = $this->getResourcesPath($module) . '/' . $path)) {
107
                if ($first && !$isResource) {
108
                    return $file;
109
                }
110
                $files[]        = $file;
111
                $resourceModule = $module->getName();
112
            }
113
        }
114
115
        if (count($files) > 0) {
116
            return $first && $isResource ? $files[0] : $files;
117
        }
118
119
        throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $name));
120
    }
121
122
    protected function getResourcesPath($module)
123
    {
124
        if (is_callable(array($module, 'getResourcesPath'))) {
125
            $resourcesPath  = $module->getResourcesPath();
126
        } else {
127
            $resourcesPath = $module->getPath();
128
        }
129
130
        return $resourcesPath;
131
    }
132
}
133