Passed
Branch master (350ff0)
by Chris
18:33
created

Factory::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 2
dl 0
loc 21
ccs 0
cts 10
cp 0
crap 6
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Leonidas\Library\Core\Models\Taxonomy;
4
5
use Leonidas\Contracts\Options\TaxonomyOptionHandlerInterface;
6
use Leonidas\Library\Core\Models\AbstractWpConfigModelFactory;
7
use Leonidas\Library\Core\Models\Taxonomy\Taxonomy;
8
use WP_Taxonomy;
9
10
class Factory extends AbstractWpConfigModelFactory
11
{
12
    /**
13
     *
14
     */
15
    public function create(array $taxonomies): array
16
    {
17
        return parent::create($taxonomies);
18
    }
19
20
    /**
21
     *
22
     */
23
    public function build(string $name, array $args): object
24
    {
25
        $labels = $args['labels'] ?? [];
26
        $objectTypes = $args['object_types'];
27
        $options = $args['options'] ?? [];
28
        $rewrite = $args['rewrite'] ?? [];
29
30
        unset($args['labels'], $args['rewrite'], $args['object_types'], $args['options']);
31
32
        $taxonomy = (new Taxonomy($name, $objectTypes))
33
            ->setArgs($args)
34
            ->setLabels($labels)
35
            ->setRewrite($rewrite)
36
            ->register()
37
            ->getRegisteredTaxonomy();
38
39
        if (isset($this->optionHandlers)) {
40
            $this->processOptions($options, $taxonomy);
0 ignored issues
show
Bug introduced by
It seems like $taxonomy can also be of type false; however, parameter $taxonomy of Leonidas\Library\Core\Mo...ctory::processOptions() does only seem to accept WP_Taxonomy, maybe add an additional type check? ( Ignorable by Annotation )

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

40
            $this->processOptions($options, /** @scrutinizer ignore-type */ $taxonomy);
Loading history...
41
        }
42
43
        return $taxonomy;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $taxonomy could return the type false which is incompatible with the type-hinted return object. Consider adding an additional type-check to rule them out.
Loading history...
44
    }
45
46
    /**
47
     *
48
     */
49
    protected function processOptions($options, WP_Taxonomy $taxonomy)
50
    {
51
        foreach ($options as $option => $args) {
52
            $handler = $this->optionHandlers[$option] ?? null;
53
54
            if (!$handler) {
55
                throw new \Exception("There is no registered handler for the {$option} option provided");
56
            }
57
58
            if ($handler && in_array(TaxonomyOptionHandlerInterface::class, class_implements($handler))) {
59
                $handler::handle($taxonomy, $args);
60
            } else {
61
                throw new \Exception("{$handler} is not a valid option handler");
62
            }
63
        }
64
    }
65
}
66