Test Failed
Push — master ( fdb79d...2e4512 )
by Chris
19:35
created

PostTypeRegistrar   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 32
dl 0
loc 60
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerMany() 0 4 2
A __construct() 0 3 1
A registerOptions() 0 8 3
A registerOne() 0 11 2
A getArgs() 0 20 1
1
<?php
2
3
namespace Leonidas\Library\System\PostType;
4
5
use Leonidas\Contracts\System\PostType\PostTypeInterface;
6
use Leonidas\Contracts\System\PostType\PostTypeOptionHandlerCollectionInterface;
7
use Leonidas\Contracts\System\PostType\PostTypeRegistrarInterface;
8
use Leonidas\Library\System\AbstractSystemModelTypeRegistrar;
9
10
class PostTypeRegistrar extends AbstractSystemModelTypeRegistrar implements PostTypeRegistrarInterface
11
{
12
    protected ?PostTypeOptionHandlerCollectionInterface $optionHandlers = null;
13
14
    public function __construct(?PostTypeOptionHandlerCollectionInterface $optionHandlers = null)
15
    {
16
        $this->optionHandlers = $optionHandlers;
17
    }
18
19
    public function registerOne(PostTypeInterface $postType)
20
    {
21
        $registered = register_post_type(
0 ignored issues
show
Bug introduced by
The function register_post_type was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        $registered = /** @scrutinizer ignore-call */ register_post_type(
Loading history...
22
            $postType->getName(),
23
            $this->getArgs($postType)
24
        );
25
26
        $registered->options = $postType->getOptions();
27
28
        if (isset($this->optionHandlers)) {
29
            $this->registerOptions($postType);
30
        }
31
    }
32
33
    public function registerMany(PostTypeInterface ...$postTypes)
34
    {
35
        foreach ($postTypes as $postType) {
36
            $this->registerOne($postType);
37
        }
38
    }
39
40
    protected function getArgs(PostTypeInterface $postType)
41
    {
42
        $args = [
43
            'exclude_from_search' => $postType->isExcludedFromSearch(),
44
            'show_in_admin_bar' => $postType->isShownInAdminBar(),
45
            'menu_position' => $postType->getMenuPosition(),
46
            'menu_icon' => $postType->getMenuIcon(),
47
            'capability_type' => $postType->getCapabilityType(),
48
            'map_meta_cap' => $postType->usesMapMetaCap(),
49
            'supports' => $postType->getSupports(),
50
            'register_meta_box_cb' => $postType->getRegisterMetaBoxCb(),
51
            'taxonomies' => $postType->getTaxonomies(),
52
            'has_archive' => $postType->getArchive(),
53
            'can_export' => $postType->canBeExported(),
54
            'delete_with_user' => $postType->isDeletedWithUser(),
55
            'template' => $postType->getTemplate(),
56
            'template_lock' => $postType->getTemplateLock(),
57
        ] + $this->getBaseArgs($postType);
58
59
        return array_filter($args, fn ($arg) => $arg !== null);
60
    }
61
62
    protected function registerOptions(PostTypeInterface $postType)
63
    {
64
        foreach ($postType->getOptions() as $option => $value) {
65
            if ($this->optionHandlers->has($option)) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            if ($this->optionHandlers->/** @scrutinizer ignore-call */ has($option)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
                $this->optionHandlers->get($option)->handle($postType, $value);
67
            }
68
69
            throw $this->unregisteredOptionException($option);
70
        }
71
    }
72
}
73