Passed
Push — master ( 3491f1...e1ceb1 )
by Chris
40:32
created

ModelInterfacePrinter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 43
c 0
b 0
f 0
dl 0
loc 73
ccs 0
cts 18
cp 0
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResolvedPartials() 0 15 3
A __construct() 0 5 1
A setupClass() 0 10 2
1
<?php
2
3
namespace Leonidas\Console\Library\Printer\Model;
4
5
use Leonidas\Console\Library\Printer\Model\Abstracts\AbstractClassPrinter;
6
use Leonidas\Contracts\System\Model\CommentableInterface;
7
use Leonidas\Contracts\System\Model\FilterableInterface;
8
use Leonidas\Contracts\System\Model\HierarchicalInterface;
9
use Leonidas\Contracts\System\Model\MimeInterface;
10
use Leonidas\Contracts\System\Model\MutableAuthoredInterface;
11
use Leonidas\Contracts\System\Model\MutableContentInterface;
12
use Leonidas\Contracts\System\Model\MutableDatableInterface;
13
use Leonidas\Contracts\System\Model\MutablePostModelInterface;
14
use Leonidas\Contracts\System\Model\MutableTermModelInterface;
15
use Leonidas\Contracts\System\Model\MutableUserModelInterface;
16
use Leonidas\Contracts\System\Model\PingableInterface;
17
use Leonidas\Contracts\System\Model\RestrictableInterface;
18
use Nette\PhpGenerator\PhpNamespace;
19
use Stringable;
20
21
class ModelInterfacePrinter extends AbstractClassPrinter
22
{
23
    protected const PARTIALS = [
24
        'post' => [
25
            FilterableInterface::class,
26
            MutableAuthoredInterface::class,
27
            MutableContentInterface::class,
28
            MutablePostModelInterface::class,
29
            PingableInterface::class,
30
            CommentableInterface::class,
31
            RestrictableInterface::class,
32
            MimeInterface::class,
33
            MutableDatableInterface::class,
34
        ],
35
        'post:h' => [
36
            '@post',
37
            HierarchicalInterface::class,
38
        ],
39
        'attachment' => [
40
            '@post',
41
        ],
42
        'term' => [
43
            MutableTermModelInterface::class,
44
        ],
45
        'term:h' => [
46
            '@term',
47
            HierarchicalInterface::class,
48
        ],
49
        'user' => [
50
            MutableUserModelInterface::class,
51
        ],
52
        'comment' => [
53
            HierarchicalInterface::class,
54
            Stringable::class,
55
        ],
56
    ];
57
58
    protected string $template;
59
60
    public function __construct(string $namespace, string $class, string $template = 'post')
61
    {
62
        parent::__construct($namespace, $class);
63
64
        $this->template = $template;
65
    }
66
67
    protected function setupClass(PhpNamespace $namespace): object
68
    {
69
        $interface = $namespace->addInterface($this->class);
70
71
        foreach ($this->getResolvedPartials() as $partial) {
72
            $namespace->addUse($partial);
73
            $interface->addExtend($partial);
74
        }
75
76
        return $interface;
77
    }
78
79
    protected function getResolvedPartials(): array
80
    {
81
        $partials = [];
82
83
        foreach (static::PARTIALS[$this->template] as $partial) {
84
            if (str_starts_with($partial, '@')) {
85
                $inherit = static::PARTIALS[substr($partial, 1)];
86
87
                $partials = [...array_values($inherit), ...$partials];
88
            } else {
89
                $partials[] = $partial;
90
            }
91
        }
92
93
        return $partials;
94
    }
95
}
96