Passed
Push — master ( 40ce26...965fb6 )
by Arthur
22:04
created

ModuleFactory::getFilesystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: tony
5
 * Date: 12.03.19
6
 * Time: 16:01
7
 */
8
9
namespace Foundation\Generator\Factories;
10
11
12
use Foundation\Core\Larapi;
13
use Foundation\Core\Module;
14
use Foundation\Generator\Managers\GeneratorManager;
15
use Illuminate\Filesystem\Filesystem;
16
use Illuminate\Support\Str;
17
use Nwidart\Modules\Exceptions\FileAlreadyExistException;
18
19
/**
20
 * Class User.
21
 *
22
 * @method void addMigration(string $name, string $table, bool $mongo)
23
 * @method void addController(string $name)
24
 * @method void addPolicy(string $name)
25
 * @method void addEvent(string $name)
26
 * @method void addNotification(string $name)
27
 * @method void addServiceProvider(string $name)
28
 * @method void addSeeder(string $name)
29
 * @method void addMiddleware(string $name)
30
 * @method void addRequest(string $name)
31
 * @method void addRule(string $name)
32
 * @method void addRoute()
33
 * @method void addComposer()
34
 * @method void addTest(string $name, string $type)
35
 * @method void addFactory(string $modelName)
36
 * @method void addTransformer(string $name, string $model)
37
 * @method void addListener(string $name, string $event, bool $queued = false)
38
 * @method void addJob(string $name, bool $sync = false)
39
 * @method void addCommand(string $name, ?string $commandName = null)
40
 * @method void addModel(string $name, bool $mongo = false, bool $migration = true)
41
 * @method void addService(string $name)
42
 * @method void addServiceContract(string $name)
43
 * @method void addException(string $name)
44
 * @method void addPermission(string $name)
45
 *
46
 */
47
class ModuleFactory
48
{
49
50
    /**
51
     * The module that will created.
52
     *
53
     * @var Module
54
     */
55
    protected $module;
56
57
    /**
58
     * The laravel filesystem instance.
59
     *
60
     * @var Filesystem
61
     */
62
    protected $filesystem;
63
64
    /**
65
     * Array of resources that will be generated upon execution.
66
     *
67
     * @var array
68
     */
69
    protected $pipeline = [];
70
71
    /**
72
     * The constructor.
73
     * @param string $moduleName
74
     * @throws FileAlreadyExistException
75
     */
76
    public function __construct(string $moduleName)
77
    {
78
        $this->module = $this->createModule($moduleName);
79
        $this->filesystem = new Filesystem();
80
    }
81
82
    private function createModule($moduleName): Module
83
    {
84
        $moduleName = Str::studly($moduleName);
85
        $path = Larapi::getModulesBasePath() . '/' . $moduleName;
86
        if (file_exists($path)) {
87
            throw new FileAlreadyExistException("Module exists already. Please remove the directory first");
88
        }
89
        return new Module($moduleName, Larapi::getModulesBasePath() . '/' . $moduleName);
90
    }
91
92
    public function __call($name, $arguments)
93
    {
94
        if (Str::startsWith($name, 'add'))
95
            $this->pipeline[] = [
96
                "name" => Str::replaceFirst('add', '', $name),
97
                "arguments" => $arguments
98
            ];
99
100
    }
101
102
    public function build()
103
    {
104
        foreach ($this->pipeline as $command) {
105
            $manager = GeneratorManager::module($this->getModule()->getName());
106
            $method = 'create' . $command['name'];
107
            call_user_func_array(array($manager, $method), $command['arguments']);
108
        }
109
    }
110
111
    /**
112
     * Get the name of module will created. By default in studly case.
113
     *
114
     * @return string
115
     */
116
    protected function getName()
117
    {
118
        return $this->module->getName();
119
    }
120
121
    /**
122
     * Get the laravel filesystem instance.
123
     *
124
     * @return Filesystem
125
     */
126
    protected function getFilesystem()
127
    {
128
        return $this->filesystem;
129
    }
130
131
    /**
132
     * @return Module
133
     */
134
    public function getModule(): Module
135
    {
136
        return $this->module;
137
    }
138
139
    /**
140
     * @return array
141
     */
142
    public function getPipeline(): array
143
    {
144
        return $this->pipeline;
145
    }
146
}
147