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

ModelPrinter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 107
c 0
b 0
f 0
dl 0
loc 165
ccs 0
cts 57
cp 0
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isPostTemplate() 0 3 1
A __construct() 0 11 1
A getResolvedPartials() 0 20 4
A setupClass() 0 50 4
1
<?php
2
3
namespace Leonidas\Console\Library\Printer\Model;
4
5
use Leonidas\Console\Library\Printer\Model\Abstracts\AbstractTypedClassPrinter;
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 Leonidas\Contracts\Util\AutoInvokerInterface;
19
use Leonidas\Library\System\Model\Abstracts\AllAccessGrantedTrait;
20
use Leonidas\Library\System\Model\Abstracts\LazyLoadableRelationshipsTrait;
21
use Leonidas\Library\System\Model\Abstracts\Post\FilterablePostModelTrait;
22
use Leonidas\Library\System\Model\Abstracts\Post\HierarchicalPostModelTrait;
23
use Leonidas\Library\System\Model\Abstracts\Post\MimePostModelTrait;
24
use Leonidas\Library\System\Model\Abstracts\Post\MutableAuthoredPostModelTrait;
25
use Leonidas\Library\System\Model\Abstracts\Post\MutableCommentablePostModelTrait;
26
use Leonidas\Library\System\Model\Abstracts\Post\MutableContentPostModelTrait;
27
use Leonidas\Library\System\Model\Abstracts\Post\MutableDatablePostModelTrait;
28
use Leonidas\Library\System\Model\Abstracts\Post\MutablePingablePostModelTrait;
29
use Leonidas\Library\System\Model\Abstracts\Post\MutablePostModelTrait;
30
use Leonidas\Library\System\Model\Abstracts\Post\RestrictablePostModelTrait;
31
use Leonidas\Library\System\Model\Abstracts\Post\ValidatesPostTypeTrait;
32
use Leonidas\Library\System\Model\Abstracts\Term\HierarchicalTermTrait;
33
use Leonidas\Library\System\Model\Abstracts\Term\MutableTermModelTrait;
34
use Leonidas\Library\System\Model\Abstracts\Term\ValidatesTaxonomyTrait;
35
use Leonidas\Library\System\Model\Abstracts\User\MutableUserModelTrait;
36
use Leonidas\Library\System\Model\Abstracts\User\ValidatesRoleTrait;
37
use Nette\PhpGenerator\PhpNamespace;
38
use WP_Comment;
0 ignored issues
show
Bug introduced by
The type WP_Comment was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
use WP_Post;
40
use WP_Term;
0 ignored issues
show
Bug introduced by
The type WP_Term was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
use WP_User;
0 ignored issues
show
Bug introduced by
The type WP_User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
43
class ModelPrinter extends AbstractTypedClassPrinter
44
{
45
    public const TEMPLATES = [
46
        'post' => WP_Post::class,
47
        'post:h' => WP_Post::class,
48
        'attachment' => WP_Post::class,
49
        'term' => WP_Term::class,
50
        'term:h' => WP_Term::class,
51
        'user' => WP_User::class,
52
        // 'comment' => WP_Comment::class,
53
    ];
54
55
    public const CORES = [
56
        'post' => 'post',
57
        'post:h' => 'post',
58
        'attachment' => 'post',
59
        'term' => 'term',
60
        'term:h' => 'term',
61
        'user' => 'user',
62
        // 'comment' => 'comment',
63
    ];
64
65
    public const VALIDATORS = [
66
        'post' => ValidatesPostTypeTrait::class,
67
        'post:h' => ValidatesPostTypeTrait::class,
68
        'attachment' => ValidatesPostTypeTrait::class,
69
        'term' => ValidatesTaxonomyTrait::class,
70
        'user' => ValidatesRoleTrait::class,
71
    ];
72
73
    public const ASSERTIONS = [
74
        'post' => "\$this->assertPostType($%s, '%s');",
75
        'post:h' => "\$this->assertPostType($%s, '%s');",
76
        'attachment' => "\$this->assertPostType($%s, '%s');",
77
        'term' => "\$this->assertTaxonomy($%s, '%s');",
78
        'term:h' => "\$this->assertTaxonomy($%s, '%s');",
79
        'user' => "\$this->assertRole($%s, '%s');",
80
    ];
81
82
    public const PARTIALS = [
83
        'post' => [
84
            FilterableInterface::class => FilterablePostModelTrait::class,
85
            MutableAuthoredInterface::class => MutableAuthoredPostModelTrait::class,
86
            MutableContentInterface::class => MutableContentPostModelTrait::class,
87
            MutablePostModelInterface::class => MutablePostModelTrait::class,
88
            PingableInterface::class => MutablePingablePostModelTrait::class,
89
            CommentableInterface::class => MutableCommentablePostModelTrait::class,
90
            RestrictableInterface::class => RestrictablePostModelTrait::class,
91
            MimeInterface::class => MimePostModelTrait::class,
92
            MutableDatableInterface::class => MutableDatablePostModelTrait::class,
93
        ],
94
        'post:h' => [
95
            '@post',
96
            HierarchicalInterface::class => HierarchicalPostModelTrait::class,
97
        ],
98
        'attachment' => [
99
            '@post',
100
        ],
101
        'term' => [
102
            MutableTermModelInterface::class => MutableTermModelTrait::class,
103
        ],
104
        'term:h' => [
105
            '@term',
106
            HierarchicalInterface::class => HierarchicalTermTrait::class,
107
        ],
108
        'user' => [
109
            MutableUserModelInterface::class => MutableUserModelTrait::class,
110
        ],
111
        // 'comment' => [],
112
    ];
113
114
    protected string $entity;
115
116
    protected string $template;
117
118
    public function __construct(
119
        string $namespace,
120
        string $class,
121
        string $type,
122
        string $entity,
123
        string $template = 'post'
124
    ) {
125
        parent::__construct($namespace, $class, $type);
126
127
        $this->entity = $entity;
128
        $this->template = $template;
129
    }
130
131
    protected function setupClass(PhpNamespace $namespace): object
132
    {
133
        $accessTrait = AllAccessGrantedTrait::class;
134
        $lazyLoadTrait = LazyLoadableRelationshipsTrait::class;
135
        $invoker = AutoInvokerInterface::class;
136
        $template = static::TEMPLATES[$this->template];
137
        $core = static::CORES[$this->template];
138
        $validator = static::VALIDATORS[$this->template];
139
        $assertion = static::ASSERTIONS[$this->template];
140
141
        $class = $namespace
142
            ->addUse($this->type)
143
            ->addUse($accessTrait)
144
            ->addUse($lazyLoadTrait)
145
            ->addUse($validator)
146
            ->addUse($invoker)
147
            ->addUse($template)
148
            ->addClass($this->class);
149
150
        $class->addImplement($this->type);
151
        $class->addTrait($accessTrait);
152
        $class->addTrait($lazyLoadTrait);
153
        $class->addTrait($validator);
154
155
        foreach ($this->getResolvedPartials() as $partial) {
156
            $namespace->addUse($partial);
157
            $class->addTrait($partial);
158
        }
159
160
        $constructor = $class->addMethod('__construct')->setPublic();
161
        $constraint = $this->template === 'attachment' ? 'attachment' : $this->entity;
162
163
        $constructor->addParameter($core)->setType($template);
164
        $constructor->addParameter('autoInvoker')->setType($invoker);
165
166
        $constructor->addBody(sprintf($assertion, $core, $constraint) . "\n");
167
        $constructor->addBody(sprintf('$this->%s = $%s;', $core, $core));
168
        $constructor->addBody('$this->autoInvoker = $autoInvoker;' . "\n");
169
170
        $getAccessTemplate = $this->isPostTemplate()
171
            ? '$this->getAccessProvider = new %sTagAccessProvider($this, $%s);'
172
            : '$this->getAccessProvider = new %sGetAccessProvider($this);';
173
174
        $constructor->addBody(sprintf($getAccessTemplate, $this->class, $core));
175
        $constructor->addBody(sprintf(
176
            '$this->setAccessProvider = new %sSetAccessProvider($this);',
177
            $this->class
178
        ));
179
180
        return $class;
181
    }
182
183
    protected function isPostTemplate(): bool
184
    {
185
        return in_array($this->template, ['post', 'post:h', 'attachment']);
186
    }
187
188
    protected function getResolvedPartials(): array
189
    {
190
        $partials = [];
191
        $map = [];
192
193
        foreach (static::PARTIALS[$this->template] as $contract => $partial) {
194
            if (str_starts_with($partial, '@')) {
195
                $inherit = static::PARTIALS[substr($partial, 1)];
196
197
                $partials = [...array_values($inherit), ...$partials];
198
                $map = $map + array_flip($inherit);
199
            } else {
200
                $partials[] = $partial;
201
                $map[$partial] = $contract;
202
            }
203
        }
204
205
        return $this->isDoingTypeMatch()
206
            ? $this->matchTraitsToType($partials, $map)
207
            : $partials;
208
    }
209
}
210