Completed
Pull Request — master (#35)
by
unknown
02:56
created

TaskFinder::getAppPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Queue\Queue;
3
4
use Cake\Core\App;
5
use Cake\Core\Plugin;
6
use Cake\Filesystem\Folder;
7
8
class TaskFinder
9
{
10
11
    /**
12
     *
13
     * @var array|null
14
     */
15
    protected $tasks;
16
17
    /**
18
     * Returns all possible Queue tasks.
19
     *
20
     * Makes sure that app tasks are prioritized over plugin ones.
21
     *
22
     * @return array
23
     */
24
    public function allAppAndPluginTasks()
25
    {
26
        if ($this->tasks !== null) {
27
            return $this->tasks;
28
        }
29
30
        $paths = App::path('Shell/Task');
31
        $this->tasks = [];
32
33
        foreach ($paths as $path) {
34
            $folder = new Folder($path);
35
            $this->tasks = $this->getAppPaths($folder);
36
        }
37
        $plugins = Plugin::loaded();
38
        foreach ($plugins as $plugin) {
39
            $pluginPaths = App::path('Shell/Task', $plugin);
40
            foreach ($pluginPaths as $pluginPath) {
41
                $folder = new Folder($pluginPath);
42
                $pluginTasks = $this->getPluginPaths($folder, $plugin);
43
                $this->tasks = array_merge($this->tasks, $pluginTasks);
44
            }
45
        }
46
47
        return $this->tasks;
48
    }
49
50
    /**
51
     *
52
     * @param \Cake\Filesystem\Folder $folder The directory
53
     *
54
     * @return array
55
     */
56
    protected function getAppPaths(Folder $folder)
57
    {
58
        $res = array_merge($this->tasks, $folder->find('Queue.+\.php'));
0 ignored issues
show
Bug introduced by
It seems like $this->tasks can also be of type null; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $res = array_merge(/** @scrutinizer ignore-type */ $this->tasks, $folder->find('Queue.+\.php'));
Loading history...
59
        foreach ($res as &$r) {
60
            $r = basename($r, 'Task.php');
61
        }
62
63
        return $res;
64
    }
65
66
    /**
67
     *
68
     * @param \Cake\Filesystem\Folder $folder The directory
69
     * @param string $plugin The plugin name
70
     *
71
     * @return array
72
     */
73
    protected function getPluginPaths(Folder $folder, $plugin)
74
    {
75
        $res = $folder->find('Queue.+Task\.php');
76
        foreach ($res as $key => $r) {
77
            $name = basename($r, 'Task.php');
78
            if (in_array($name, $this->tasks)) {
0 ignored issues
show
Bug introduced by
It seems like $this->tasks can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            if (in_array($name, /** @scrutinizer ignore-type */ $this->tasks)) {
Loading history...
79
                unset($res[$key]);
80
                continue;
81
            }
82
            $res[$key] = $plugin . '.' . $name;
83
        }
84
85
        return $res;
86
    }
87
}
88