Passed
Pull Request — master (#25)
by mark
09:02
created

BakeServiceCommand::bake()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
15
 */
16
namespace Burzum\CakeServiceLayer\Command;
17
18
use Bake\Command\BakeCommand;
19
use Bake\Command\SimpleBakeCommand;
20
use Cake\Console\Arguments;
21
use Cake\Console\ConsoleIo;
22
use Cake\Core\Configure;
23
use Cake\Event\Event;
24
use Cake\Event\EventManager;
25
26
/**
27
 * Command class for generating migration snapshot files.
28
 */
29
class BakeServiceCommand extends SimpleBakeCommand
30
{
31
    /**
32
     * Task name used in path generation.
33
     *
34
     * @var string
35
     */
36
    public $pathFragment = 'Service/';
37
38
    /**
39
     * @var string
40
     */
41
    protected $_name;
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public static function defaultName(): string
47
    {
48
        return 'bake service';
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function bake(string $name, Arguments $args, ConsoleIo $io): void
55
    {
56
        EventManager::instance()->on('Bake.initialize', function (Event $event) {
57
            $event->getSubject()->loadHelper('Burzum/CakeServiceLayer.Service');
58
        });
59
        $this->_name = $name;
60
61
        parent::bake($name, $args, $io);
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function template(): string
68
    {
69
        return 'Burzum/CakeServiceLayer.Service/service';
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function templateData(Arguments $arguments): array
76
    {
77
        $name = $this->_name;
78
        $namespace = Configure::read('App.namespace');
79
        $pluginPath = '';
80
        if ($this->plugin) {
81
            $namespace = $this->_pluginNamespace($this->plugin);
82
            $pluginPath = $this->plugin . '.';
83
        }
84
85
        $namespace .= '\\Service';
86
87
        $namespacePart = null;
88
        if (strpos($name, '/') !== false) {
89
            $parts = explode('/', $name);
90
            $name = array_pop($parts);
91
            $namespacePart = implode('\\', $parts);
92
        }
93
        if ($namespacePart) {
94
            $namespace .= '\\' . $namespacePart;
95
        }
96
97
        return [
98
            'plugin' => $this->plugin,
99
            'pluginPath' => $pluginPath,
100
            'namespace' => $namespace,
101
            'name' => $name,
102
        ];
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108
    public function name(): string
109
    {
110
        return 'service';
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function fileName(string $name): string
117
    {
118
        return $name . 'Service.php';
119
    }
120
}
121