ModuleFactory::getFilesystem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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(string $version)
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
 * @method void addAttribute(string $name)
46
 * @method void addDto(string $name)
47
 * @method void addGuard(string $name)
48
 *
49
 */
50
class ModuleFactory
51
{
52
53
    /**
54
     * The module that will created.
55
     *
56
     * @var Module
57
     */
58
    protected $module;
59
60
    /**
61
     * The laravel filesystem instance.
62
     *
63
     * @var Filesystem
64
     */
65
    protected $filesystem;
66
67
    /**
68
     * Array of resources that will be generated upon execution.
69
     *
70
     * @var array
71
     */
72
    protected $pipeline = [];
73
74
    /**
75
     * The constructor.
76
     * @param string $moduleName
77
     * @throws FileAlreadyExistException
78
     */
79 2
    public function __construct(string $moduleName)
80
    {
81 2
        $this->module = $this->createModule($moduleName);
82 2
        $this->filesystem = new Filesystem();
83 2
    }
84
85 2
    private function createModule($moduleName): Module
86
    {
87 2
        $moduleName = Str::studly($moduleName);
88 2
        $path = Larapi::getModulesBasePath() . '/' . $moduleName;
89 2
        if (file_exists($path)) {
90
            throw new FileAlreadyExistException("Module exists already. Please remove the directory first");
91
        }
92 2
        return new Module($moduleName, Larapi::getModulesBasePath() . '/' . $moduleName);
93
    }
94
95 2
    public function __call($name, $arguments)
96
    {
97 2
        if (Str::startsWith($name, 'add'))
98 2
            $this->pipeline[] = [
99 2
                "name" => Str::replaceFirst('add', '', $name),
100 2
                "arguments" => $arguments
101
            ];
102
103 2
    }
104
105 1
    public function build()
106
    {
107 1
        foreach ($this->pipeline as $command) {
108 1
            $manager = GeneratorManager::module($this->getModule()->getName());
109 1
            $method = 'create' . $command['name'];
110 1
            call_user_func_array(array($manager, $method), $command['arguments']);
111
        }
112 1
    }
113
114
    /**
115
     * Get the name of module will created. By default in studly case.
116
     *
117
     * @return string
118
     */
119
    protected function getName()
120
    {
121
        return $this->module->getName();
122
    }
123
124
    /**
125
     * Get the laravel filesystem instance.
126
     *
127
     * @return Filesystem
128
     */
129
    protected function getFilesystem()
130
    {
131
        return $this->filesystem;
132
    }
133
134
    /**
135
     * @return Module
136
     */
137 1
    public function getModule(): Module
138
    {
139 1
        return $this->module;
140
    }
141
142
    /**
143
     * @return array
144
     */
145 1
    public function getPipeline(): array
146
    {
147 1
        return $this->pipeline;
148
    }
149
}
150