Passed
Push — master ( a228a3...2f45dd )
by Chris
14:02
created

ModelCollectionCommand::runTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 2
rs 10
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\ModelCollectionsFactory;
7
use Leonidas\Console\Library\ModelInterfacePrinter;
8
use Leonidas\Console\Library\ModelPrinter;
9
use Leonidas\Console\Library\ModelRepositoryInterfacePrinter;
10
use Leonidas\Console\Library\ModelRepositoryPrinter;
11
use Leonidas\Contracts\System\Model\Post\PostCollectionInterface;
12
use Leonidas\Contracts\System\Model\Post\PostInterface;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class ModelCollectionCommand extends HopliteCommand
18
{
19
    protected static $defaultName = 'make:model-collection';
20
21
    protected function configure()
22
    {
23
        // $this
24
        //     ->addArgument('model', InputArgument::REQUIRED);
25
    }
26
27
    protected function execute(InputInterface $input, OutputInterface $output): int
28
    {
29
        $this->runTests();
30
31
        return 0;
32
    }
33
34
    protected function runTests(): void
35
    {
36
        $playground = getcwd() . '/.playground/factory';
37
38
        $this->testCollection($playground);
39
        $this->testRepository($playground);
40
        $this->testModel($playground);
41
    }
42
43
    protected function testModel(string $playground): void
44
    {
45
        $interface = new ModelInterfacePrinter(
46
            $contracts = 'Leonidas\Contracts\System\Model\Post',
47
            $type = 'PostInterface',
48
            $template = 'attachment'
49
        );
50
51
        $class = new ModelPrinter(
52
            'Leonidas\Library\System\Model\Post',
53
            'Post',
54
            $contracts . '\\' . $type,
55
            'test',
56
            $template
57
        );
58
59
        $this->printPhp($interface = $interface->printFile());
60
        file_put_contents($playground . '/model-interface.php', $interface);
61
62
        $this->printPhp($class = $class->printFromType());
63
        file_put_contents($playground . '/model-class.php', $class);
64
    }
65
66
    protected function testRepository(string $playground): void
67
    {
68
        $interface = new ModelRepositoryInterfacePrinter(
69
            $model = PostInterface::class,
70
            $collection = PostCollectionInterface::class,
71
            $single = 'post',
72
            $plural = 'posts',
73
            $contracts = 'Leonidas\Contracts\System\Model\Post',
74
            $type = 'PostRepositoryInterface',
75
            $template = 'post'
76
        );
77
78
        $class = new ModelRepositoryPrinter(
79
            $model,
80
            $collection,
81
            $single,
82
            $plural,
83
            'Leonidas\Library\System\Model\Post',
84
            'PostRepository',
85
            $contracts . '\\' . $type,
86
            $template
87
        );
88
89
        $this->printPhp($interface = $interface->printFile());
90
        file_put_contents($playground . '/repository-interface.php', $interface);
91
92
        $this->printPhp($class = $class->printFile());
93
        file_put_contents($playground . '/repository-class.php', $class);
94
    }
95
96
    protected function testCollection(string $playground): void
97
    {
98
        $factory = ModelCollectionsFactory::build([
99
            'model' => PostInterface::class,
100
            'single' => 'post',
101
            'plural' => 'posts',
102
            'namespace' => 'Leonidas\Library\System\Model\Post',
103
            'contracts' => 'Leonidas\Contracts\System\Model\Post',
104
            'abstracts' => 'Leonidas\Library\System\Model\Post\Abstracts',
105
            'type' => 'PostCollectionInterface',
106
            'abstract' => 'AbstractPostCollection',
107
            'collection' => 'PostCollection',
108
            'query' => 'PostQuery',
109
            'entity' => 'post',
110
            'template' => 'post',
111
        ]);
112
113
        $output = [
114
            'interface' => $factory->getModelInterfacePrinter()->printFile(),
115
            'abstract' => $factory->getAbstractCollectionPrinter()->printFile(),
116
            'collection' => $factory->getChildCollectionPrinter()->printFile(),
117
            'query' => $factory->getChildQueryPrinter()->printFile(),
118
        ];
119
120
        foreach ($output as $class => $code) {
121
            $file = $playground . '/collection-' . $class . '.php';
122
123
            $this->printPhp($code);
124
125
            file_put_contents($file, $code);
126
        }
127
    }
128
}
129