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

ModelConverterPrinter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 85
dl 0
loc 113
ccs 0
cts 48
cp 0
rs 10
c 1
b 0
f 1
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setupClass() 0 56 1
1
<?php
2
3
namespace Leonidas\Console\Library;
4
5
use Leonidas\Console\Library\Abstracts\AbstractClassPrinter;
6
use Leonidas\Contracts\System\Schema\Comment\CommentConverterInterface;
7
use Leonidas\Contracts\System\Schema\Post\PostConverterInterface;
8
use Leonidas\Contracts\System\Schema\Term\TermConverterInterface;
9
use Leonidas\Contracts\System\Schema\User\UserConverterInterface;
10
use Leonidas\Library\System\Model\Abstracts\AbstractModelConverter;
11
use Leonidas\Library\System\Schema\Exceptions\UnexpectedEntityException;
12
use Nette\PhpGenerator\PhpNamespace;
13
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...
14
use WP_Post;
15
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...
16
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...
17
18
class ModelConverterPrinter extends AbstractClassPrinter
19
{
20
    public const TEMPLATES = [
21
        'post' => WP_Post::class,
22
        'post:h' => WP_Post::class,
23
        'attachment' => WP_Post::class,
24
        'term' => WP_Term::class,
25
        'term:h' => WP_Term::class,
26
        'user' => WP_User::class,
27
        'comment' => WP_Comment::class,
28
    ];
29
30
    public const CORES = [
31
        'post' => 'post',
32
        'post:h' => 'post',
33
        'attachment' => 'post',
34
        'term' => 'term',
35
        'term:h' => 'term',
36
        'user' => 'user',
37
        'comment' => 'comment',
38
    ];
39
40
    public const CONVERTERS = [
41
        'post' => PostConverterInterface::class,
42
        'post:h' => PostConverterInterface::class,
43
        'attachment' => PostConverterInterface::class,
44
        'term' => TermConverterInterface::class,
45
        'term:h' => TermConverterInterface::class,
46
        'user' => UserConverterInterface::class,
47
        'comment' => CommentConverterInterface::class,
48
    ];
49
50
    public const FUNCTIONS = [
51
        'post' => 'get_post(%s)',
52
        'post:h' => 'get_post(%s)',
53
        'attachment' => 'get_post(%s)',
54
        'term' => 'get_term(%s)',
55
        'term:h' => 'get_term(%s)',
56
        'user' => 'get_user_by(\'id\', %s)',
57
        'comment' => 'get_comment(%s)',
58
    ];
59
60
    protected string $model;
61
62
    protected string $contract;
63
64
    protected string $template;
65
66
    public function __construct(string $namespace, string $class, string $model, string $contract, string $template)
67
    {
68
        parent::__construct($namespace, $class);
69
70
        $this->model = $model;
71
        $this->contract = $contract;
72
        $this->template = $template;
73
    }
74
75
    protected function setupClass(PhpNamespace $namespace): object
76
    {
77
        $base = AbstractModelConverter::class;
78
        $error = UnexpectedEntityException::class;
79
        $converter = static::CONVERTERS[$this->template];
80
        $template = static::TEMPLATES[$this->template];
81
        $core = static::CORES[$this->template];
82
        $function = static::FUNCTIONS[$this->template];
83
84
        $class = $namespace
85
            ->addUse($base)
86
            ->addUse($error)
87
            ->addUse($converter)
88
            ->addUse($template)
89
            ->addUse($this->contract)
90
            ->addClass($this->class)
91
            ->setExtends($base)
92
            ->addImplement($converter);
93
94
        $model = explode('\\', $this->model);
95
        $model = end($model);
96
97
        $class->addMethod('convert')
98
            ->setPublic()
99
            ->setReturnType($this->contract)
100
            ->addBody(sprintf(
101
                'return new %s($%s, $this->autoInvoker);',
102
                $model,
103
                $core
104
            ))
105
            ->addParameter($core)
106
            ->setType($template);
107
108
        $contract = explode('\\', $this->contract);
109
        $contract = end($contract);
110
111
        $throw = explode('\\', $error);
112
        $throw = end($throw);
113
114
        $call = sprintf($function, '$' . $core . '->getId()');
115
116
        $class->addMethod('revert')
117
            ->setPublic()
118
            ->setReturnType($template)
119
            ->addBody(sprintf('if ($%s instanceof %s) {', $core, $contract))
120
            ->addBody(sprintf('    return %s;', $call))
121
            ->addBody("} \n")
122
            ->addBody(sprintf('throw new %s(', $throw))
123
            ->addBody(sprintf('    %s::class,', $contract))
124
            ->addBody(sprintf('    $%s,', $core))
125
            ->addBody('    __METHOD__')
126
            ->addBody(');')
127
            ->addParameter($core)
128
            ->setType('object');
129
130
        return $class;
131
    }
132
}
133