Completed
Push — feature-extra-extensions ( 2f2a70 )
by Cees-Jan
13:52 queued 11:55
created

Scanner   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 98.11%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 0
loc 154
ccs 52
cts 53
cp 0.9811
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A iteratePath() 0 4 1
A setupIterator() 0 13 1
C all() 0 23 8
A clearEmptySections() 0 10 2
A pluginsWithTemplates() 0 16 2
A plugin() 0 10 2
B walkIterator() 0 21 5
1
<?php
2
3
namespace WyriHaximus\TwigView\Lib;
4
5
use Cake\Core\App;
6
use Cake\Core\Plugin;
7
use WyriHaximus\TwigView\View\TwigView;
8
9
/**
10
 * Class Scanner
11
 * @package WyriHaximus\TwigView\Lib
12
 */
13
class Scanner
14
{
15
    /**
16
     * Return all sections (app & plugins) with an Template directory.
17
     *
18
     * @return array
19
     */
20
    // @codingStandardsIgnoreStart
21 5
    public static function all()
22
    {
23
        // @codingStandardsIgnoreEnd
24 5
        $sections = [];
25
26 5
        foreach (App::path('Template') as $path) {
27 3
            if (is_dir($path)) {
28 3
                $sections['APP'] = isset($sections['APP']) ? $sections['APP'] : [];
29 3
                $sections['APP'] = array_merge($sections['APP'], static::iteratePath($path));
30
            }
31
        }
32
33 5
        foreach (static::pluginsWithTemplates() as $plugin) {
34 5
            foreach (App::path('Template', $plugin) as $path) {
35 5
                if (is_dir($path)) {
36 5
                    $sections[$plugin] = isset($sections[$plugin]) ? $sections[$plugin] : [];
37 5
                    $sections[$plugin] = array_merge($sections[$plugin], static::iteratePath($path));
38
                }
39
            }
40
        }
41
42 5
        return static::clearEmptySections($sections);
43
    }
44
45
    /**
46
     * Check sections a remove the ones without anything in them.
47
     *
48
     * @param array $sections Sections to check.
49
     *
50
     * @return array
51
     */
52 5
    protected static function clearEmptySections(array $sections)
53
    {
54
        array_walk($sections, function ($templates, $index) use (&$sections) {
55 5
            if (count($templates) == 0) {
56 5
                unset($sections[$index]);
57
            }
58 5
        });
59
60 5
        return $sections;
61
    }
62
63
    /**
64
     * Finds all plugins with a Template directory.
65
     *
66
     * @return array
67
     */
68 5
    protected static function pluginsWithTemplates()
69
    {
70 5
        $plugins = Plugin::loaded();
71
72
        array_walk($plugins, function ($plugin, $index) use (&$plugins) {
0 ignored issues
show
Unused Code introduced by
The parameter $index is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73 5
            $paths = App::path('Template', $plugin);
74
75
            array_walk($paths, function ($path, $index) use (&$paths) {
76 5
                if (!is_dir($path)) {
77 5
                    unset($paths[$index]);
78
                }
79 5
            });
80 5
        });
81
82 5
        return $plugins;
83
    }
84
85
    /**
86
     * Return all templates for a given plugin.
87
     *
88
     * @param string $plugin The plugin to find all templates for.
89
     *
90
     * @return mixed
91
     */
92 4
    public static function plugin($plugin)
93
    {
94 4
        $templates = [];
95
96 4
        foreach (App::path('Template', $plugin) as $path) {
97 3
            $templates = array_merge($templates, static::iteratePath($path));
98
        }
99
100 3
        return $templates;
101
    }
102
103
    /**
104
     * Iterage over the given path and return all matching .tpl files in it.
105
     *
106
     * @param string $path Path to iterate over.
107
     *
108
     * @return array
109
     */
110 8
    protected static function iteratePath($path)
111
    {
112 8
        return static::walkIterator(static::setupIterator($path));
113
    }
114
115
    /**
116
     * Setup iterator for given path.
117
     *
118
     * @param string $path Path to setup iterator for.
119
     *
120
     * @return \Iterator
121
     */
122 8
    protected static function setupIterator($path)
123
    {
124 8
        return new \RegexIterator(new \RecursiveIteratorIterator(
125 8
            new \RecursiveDirectoryIterator(
126
                $path,
127 8
                \FilesystemIterator::KEY_AS_PATHNAME |
128 8
                \FilesystemIterator::CURRENT_AS_FILEINFO |
129 8
                \FilesystemIterator::SKIP_DOTS
130
            ),
131 8
            \RecursiveIteratorIterator::CHILD_FIRST,
132 8
            \RecursiveIteratorIterator::CATCH_GET_CHILD
133 8
        ), '/.*?' . TwigView::EXT . '$/', \RegexIterator::GET_MATCH);
134
    }
135
136
    /**
137
     * Walk over the iterator and compile all templates.
138
     *
139
     * @param \Iterator $iterator Iterator to walk.
140
     *
141
     * @return array
142
     */
143
    // @codingStandardsIgnoreStart
144 8
    protected static function walkIterator(\Iterator $iterator)
145
    {
146 8
        $items = [];
147
148 8
        $array = iterator_to_array($iterator);
149 8
        uasort($array, function ($a, $b) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
150 6
            if ($a == $b) {
151
                return 0;
152
            }
153
154 6
            return ($a < $b) ? -1 : 1;
155 8
        });
156
157 8
        foreach ($array as $paths) {
158 6
            foreach ($paths as $path) {
159 6
                $items[] = $path;
160
            }
161
        }
162
163 8
        return $items;
164
    }
165
    // @codingStandardsIgnoreEnd
166
}
167