Passed
Push — master ( 0d6a5d...f5c22c )
by Chris
16:10
created

ModelCommand::runTests()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 13
ccs 0
cts 9
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Leonidas\Console\Command\Make;
4
5
use Leonidas\Console\Command\HopliteCommand;
6
use Leonidas\Console\Library\ModelCollectionFactoryPrinter;
7
use Leonidas\Console\Library\ModelCollectionsFactory;
8
use Leonidas\Console\Library\ModelConverterPrinter;
9
use Leonidas\Console\Library\ModelGetAccessProviderPrinter;
10
use Leonidas\Console\Library\ModelInterfacePrinter;
11
use Leonidas\Console\Library\ModelPrinter;
12
use Leonidas\Console\Library\ModelQueryFactoryPrinter;
13
use Leonidas\Console\Library\ModelRepositoryInterfacePrinter;
14
use Leonidas\Console\Library\ModelRepositoryPrinter;
15
use Leonidas\Console\Library\ModelSetAccessProviderPrinter;
16
use Leonidas\Console\Library\ModelTemplateTagsProviderPrinter;
17
use Leonidas\Contracts\System\Model\Post\PostCollectionInterface;
18
use Leonidas\Contracts\System\Model\Post\PostInterface;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
class ModelCommand extends HopliteCommand
24
{
25
    protected static $defaultName = 'make:model';
26
27
    protected function configure()
28
    {
29
        // $this
30
        //     ->addArgument('model', InputArgument::REQUIRED);
31
    }
32
33
    protected function execute(InputInterface $input, OutputInterface $output): int
34
    {
35
        $this->runTests();
36
37
        return 0;
38
    }
39
40
    protected function runTests(): void
41
    {
42
        $playground = getcwd() . '/.playground/model';
43
44
        if (!is_dir($playground)) {
45
            mkdir($playground, 0777, true);
46
        }
47
48
        $this->testCollection($playground);
49
        $this->testRepository($playground);
50
        $this->testModel($playground);
51
        $this->testFactories($playground);
52
        $this->testAccessProviders($playground);
53
    }
54
55
    protected function testAccessProviders(string $playground): void
56
    {
57
        $namespace = 'Leonidas\Library\System\Model\Post';
58
        $model = 'Leonidas\Contracts\System\Model\Post\PostInterface';
59
60
        $get = new ModelGetAccessProviderPrinter(
61
            $namespace,
62
            $getter = 'PostGetAccessProvider',
63
            $model,
64
            'post',
65
            false
66
        );
67
68
        $set = new ModelSetAccessProviderPrinter(
69
            $namespace,
70
            'PostGetAccessProvider',
71
            $model,
72
            'post',
73
            true
74
        );
75
76
        $tag = new ModelTemplateTagsProviderPrinter(
77
            $namespace,
78
            'PostTemplateTags',
79
            $model,
80
            'post',
81
            $namespace . '\\' . $getter,
82
            'post'
83
        );
84
85
        $this->printPhp($get = $get->printFile());
86
        file_put_contents($playground . '/access-get.php', $get);
87
88
        $this->printPhp($set = $set->printFile());
89
        file_put_contents($playground . '/access-set.php', $set);
90
91
        $this->printPhp($tag = $tag->printFile());
92
        file_put_contents($playground . '/access-tag.php', $tag);
93
    }
94
95
    protected function testFactories(string $playground): void
96
    {
97
        $namespace = 'Leonidas\Library\System\Model\Post';
98
99
        $collection = new ModelCollectionFactoryPrinter(
100
            $namespace,
101
            'PostCollectionFactory',
102
            $namespace . '\\' . 'PostCollection'
103
        );
104
105
        $query = new ModelQueryFactoryPrinter(
106
            $namespace,
107
            'PostQueryFactory',
108
            $namespace . '\\' . 'PostQuery',
109
            'post'
110
        );
111
112
        $model = new ModelConverterPrinter(
113
            $namespace,
114
            'PostConverter',
115
            'Leonidas\Library\System\Model\Post\Post',
116
            'Leonidas\Contracts\System\Model\Post\PostInterface',
117
            'user'
118
        );
119
120
        $this->printPhp($collection = $collection->printFile());
121
        file_put_contents($playground . '/factory-collection.php', $collection);
122
123
        $this->printPhp($query = $query->printFile());
124
        file_put_contents($playground . '/factory-query.php', $query);
125
126
        $this->printPhp($model = $model->printFile());
127
        file_put_contents($playground . '/factory-model.php', $model);
128
    }
129
130
    protected function testModel(string $playground): void
131
    {
132
        $interface = new ModelInterfacePrinter(
133
            $contracts = 'Leonidas\Contracts\System\Model\Post',
134
            $type = 'PostInterface',
135
            $template = 'attachment'
136
        );
137
138
        $class = new ModelPrinter(
139
            'Leonidas\Library\System\Model\Post',
140
            'Post',
141
            $contracts . '\\' . $type,
142
            'test',
143
            $template
144
        );
145
146
        $this->printPhp($interface = $interface->printFile());
147
        file_put_contents($playground . '/model-interface.php', $interface);
148
149
        $this->printPhp($class = $class->printFromType());
150
        file_put_contents($playground . '/model-class.php', $class);
151
    }
152
153
    protected function testRepository(string $playground): void
154
    {
155
        $interface = new ModelRepositoryInterfacePrinter(
156
            $model = PostInterface::class,
157
            $collection = PostCollectionInterface::class,
158
            $single = 'post',
159
            $plural = 'posts',
160
            $contracts = 'Leonidas\Contracts\System\Model\Post',
161
            $type = 'PostRepositoryInterface',
162
            $template = 'post'
163
        );
164
165
        $class = new ModelRepositoryPrinter(
166
            $model,
167
            $collection,
168
            $single,
169
            $plural,
170
            'Leonidas\Library\System\Model\Post',
171
            'PostRepository',
172
            $contracts . '\\' . $type,
173
            $template
174
        );
175
176
        $this->printPhp($interface = $interface->printFile());
177
        file_put_contents($playground . '/repository-interface.php', $interface);
178
179
        $this->printPhp($class = $class->printFile());
180
        file_put_contents($playground . '/repository-class.php', $class);
181
    }
182
183
    protected function testCollection(string $playground): void
184
    {
185
        $factory = ModelCollectionsFactory::build([
186
            'model' => PostInterface::class,
187
            'single' => 'post',
188
            'plural' => 'posts',
189
            'namespace' => 'Leonidas\Library\System\Model\Post',
190
            'contracts' => 'Leonidas\Contracts\System\Model\Post',
191
            'abstracts' => 'Leonidas\Library\System\Model\Post\Abstracts',
192
            'type' => 'PostCollectionInterface',
193
            'abstract' => 'AbstractPostCollection',
194
            'collection' => 'PostCollection',
195
            'query' => 'PostQuery',
196
            'entity' => 'post',
197
            'template' => 'post',
198
        ]);
199
200
        $output = [
201
            'interface' => $factory->getModelInterfacePrinter()->printFile(),
202
            'abstract' => $factory->getAbstractCollectionPrinter()->printFile(),
203
            'collection' => $factory->getChildCollectionPrinter()->printFile(),
204
            'query' => $factory->getChildQueryPrinter()->printFile(),
205
        ];
206
207
        foreach ($output as $class => $code) {
208
            $file = $playground . '/collection-' . $class . '.php';
209
210
            $this->printPhp($code);
211
            file_put_contents($file, $code);
212
        }
213
    }
214
}
215