Passed
Push — master ( 2f45dd...0d6a5d )
by Chris
17:20
created

ModelCollectionCommand::testFactories()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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