Passed
Push — master ( ff4e9f...624a9a )
by Arthur
35:46
created

ModuleFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 98
ccs 25
cts 30
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

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