Test Failed
Pull Request — master (#19)
by Florian
02:37
created

ServiceTask::collectServices()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 24
ccs 0
cts 14
cp 0
crap 30
rs 9.5222
1
<?php
2
/**
3
 * Copyright (c) Florian Krämer
4
 * Licensed under The MIT License
5
 * For full copyright and license information, please see the LICENSE.txt
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright     Copyright (c) Florian Krämer
9
 * @link          https://github.com/burzum/cakephp-service-layer
10
 * @since         1.0.0
11
 * @license       https://opensource.org/licenses/mit-license.php MIT License
12
 */
13
declare(strict_types = 1);
14
15
namespace Burzum\Cake\Generator\Task;
16
17
use Cake\Core\App;
18
use Cake\Core\Plugin;
19
use Cake\Filesystem\Folder;
20
use IdeHelper\Generator\Directive\Override;
21
use IdeHelper\Generator\Task\TaskInterface;
22
23
/**
24
 * ServiceTask
25
 */
26
class ServiceTask implements TaskInterface
27
{
28
    /**
29
     * Aliases
30
     *
31
     * @var string[]
32
     */
33
    protected $aliases = [
34
        '\Burzum\Cake\Service\ServiceAwareTrait::loadService(0)',
35
    ];
36
37
    /**
38
     * Buffer
39
     *
40
     * @var string[]|null
41
     */
42
    protected static $services;
43
44
    /**
45
     * @return void
46
     */
47
    public static function clearBuffer()
48
    {
49
        static::$services = null;
50
    }
51
52
    /**
53
     * @return \IdeHelper\Generator\Directive\BaseDirective[]
54
     */
55
    public function collect(): array
56
    {
57
        $map = [];
58
59
        $services = $this->collectServices();
60
        foreach ($services as $service => $className) {
61
            $map[$service] = '\\' . $className . '::class';
62
        }
63
64
        $result = [];
65
        foreach ($this->aliases as $alias) {
66
            $directive = new Override($alias, $map);
67
            $result[$directive->key()] = $directive;
68
        }
69
70
        return $result;
71
    }
72
73
    /**
74
     * @return string[]
75
     */
76
    protected function collectServices()
77
    {
78
        if (static::$services !== null) {
79
            return static::$services;
80
        }
81
82
        $services = [];
83
84
        $folders = App::path('Service');
85
        foreach ($folders as $folder) {
86
            $services = $this->addServices($services, $folder);
87
        }
88
89
        $plugins = Plugin::loaded();
90
        foreach ($plugins as $plugin) {
91
            $folders = App::path('Service', $plugin);
92
            foreach ($folders as $folder) {
93
                $services = $this->addServices($services, $folder, null, $plugin);
94
            }
95
        }
96
97
        static::$services = $services;
98
99
        return $services;
100
    }
101
102
    /**
103
     * @param string[] $services Services array
104
     * @param string $path Path
105
     * @param string|null $subFolder Sub folder
106
     * @param string|null $plugin Plugin
107
     * @return string[]
108
     */
109
    protected function addServices(array $services, $path, $subFolder = null, $plugin = null)
110
    {
111
        $folderContent = (new Folder($path))->read(Folder::SORT_NAME, true);
112
113
        foreach ($folderContent[1] as $file) {
114
            preg_match('/^(.+)Service\.php$/', $file, $matches);
115
            if (!$matches) {
116
                continue;
117
            }
118
            $service = $matches[1];
119
            if ($subFolder) {
120
                $service = $subFolder . '/' . $service;
121
            }
122
123
            if ($plugin) {
124
                $service = $plugin . '.' . $service;
125
            }
126
127
            $className = App::className($service, 'Service', 'Service');
128
            if (!$className) {
129
                continue;
130
            }
131
132
            $services[$service] = $className;
133
        }
134
135
        foreach ($folderContent[0] as $subDirectory) {
136
            $nextSubFolder = $subFolder ? $subFolder . '/' . $subDirectory : $subDirectory;
137
            $services = $this->addServices($services, $path . $subDirectory . DS, $nextSubFolder, $plugin);
138
        }
139
140
        return $services;
141
    }
142
}
143