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

ModelQueryFactoryPrinter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 1
b 0
f 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\Post\QueryFactoryInterface;
9
use Leonidas\Contracts\System\Schema\Term\TermConverterInterface;
10
use Leonidas\Contracts\System\Schema\User\UserConverterInterface;
11
use Nette\PhpGenerator\PhpNamespace;
12
use WP_Comment_Query;
0 ignored issues
show
Bug introduced by
The type WP_Comment_Query 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...
13
use WP_Query;
0 ignored issues
show
Bug introduced by
The type WP_Query 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_Term_Query;
0 ignored issues
show
Bug introduced by
The type WP_Term_Query 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...
15
use WP_User_Query;
0 ignored issues
show
Bug introduced by
The type WP_User_Query 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
17
class ModelQueryFactoryPrinter extends AbstractClassPrinter
18
{
19
    public const QUERIES = [
20
        'post' => WP_Query::class,
21
        'post:h' => WP_Query::class,
22
        'attachment' => WP_Query::class,
23
        // 'term' => WP_Term_Query::class,
24
        // 'term:h' => WP_Term_Query::class,
25
        // 'user' => WP_User_Query::class,
26
        // 'comment' => WP_Comment_Query::class,
27
    ];
28
29
    public const CONVERTERS = [
30
        'post' => PostConverterInterface::class,
31
        'post:h' => PostConverterInterface::class,
32
        'attachment' => PostConverterInterface::class,
33
        // 'term' => TermConverterInterface::class,
34
        // 'term:h' => TermConverterInterface::class,
35
        // 'user' => UserConverterInterface::class,
36
        // 'comment' => CommentConverterInterface::class,
37
    ];
38
39
    protected string $query;
40
41
    protected string $template;
42
43
    public function __construct(string $namespace, string $class, string $query, string $template)
44
    {
45
        parent::__construct($namespace, $class);
46
        $this->query = $query;
47
        $this->template = $template;
48
    }
49
50
    protected function setupClass(PhpNamespace $namespace): object
51
    {
52
        $base = QueryFactoryInterface::class;
53
        $query = static::QUERIES[$this->template];
54
        $converter = static::CONVERTERS[$this->template];
55
        $return = explode('\\', $this->query);
56
        $return = end($return);
57
58
        $class = $namespace
59
            ->addUse($base)
60
            ->addUse($converter)
61
            ->addUse($query)
62
            ->addClass($this->class);
63
64
        $class->addImplement($base);
65
66
        $class->addMethod('__construct')
67
            ->setPublic()
68
            ->addBody('$this->converter = $converter;')
69
            ->addParameter('converter')
70
            ->setType($converter);
71
72
        $class->addMethod('createQuery')
73
            ->setPublic()
74
            ->addBody(sprintf('return new %s($query, $this->converter);', $return))
75
            ->setReturnType($this->query)
76
            ->addParameter('query')
77
            ->setType($query);
78
79
        return $class;
80
    }
81
}
82