Term::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Rarst\Hugo\wprss2hugo\Handler;
5
6
use Rarst\Hugo\wprss2hugo\Filesystem\Write;
7
8
/**
9
 * Handler for native and custom taxonomy terms.
10
 */
11
class Term extends Node
12
{
13
    /** @var Config */
14
    private $config;
15
16
    /**
17
     * Set up with instance of config to add custom taxonomies as necessary.
18
     */
19
    public function __construct(Write $store, Config $config)
20
    {
21
        parent::__construct($store);
22
        $this->config = $config;
23
    }
24
25
    /**
26
     * Handle a term XML element.
27
     */
28
    public function handle(\SimpleXMLElement $node): void
29
    {
30
        switch ($node->getName()) {
31
            case 'category':
32
                $this->category($node);
33
                break;
34
35
            case 'tag':
36
                $this->tag($node);
37
                break;
38
39
            case 'term':
40
                $this->term($node);
41
                break;
42
        }
43
    }
44
45
    /**
46
     * Handle a `category` XML node.
47
     */
48
    private function category(\SimpleXMLElement $node): void
49
    {
50
        $this->store->content(
51
            "content/categories/{$node->category_nicename}/_index",
52
            [
53
                'title' => (string)$node->cat_name,
54
                'slug'  => (string)$node->category_nicename,
55
            ],
56
            (string)$node->category_description,
57
            );
58
    }
59
60
    /**
61
     * Handle a `tag` XML node.
62
     */
63
    private function tag(\SimpleXMLElement $node): void
64
    {
65
        $this->store->content(
66
            "content/tags/{$node->tag_slug}/_index",
67
            [
68
                'title' => (string)$node->tag_name,
69
                'slug'  => (string)$node->tag_slug,
70
            ],
71
            (string)$node->tag_description
72
        );
73
    }
74
75
    /**
76
     * Handle a `term` XML node.
77
     */
78
    private function term(\SimpleXMLElement $node): void
79
    {
80
        $taxonomy = (string)$node->term_taxonomy;
81
82
        if ('nav_menu' === $taxonomy) {
83
            return;
84
        }
85
86
        $this->config->addTaxonomy($taxonomy, $taxonomy);
87
88
        $this->store->content(
89
            "content/{$taxonomy}/{$node->term_slug}/_index",
90
            [
91
                'title' => (string)$node->term_name,
92
                'slug'  => (string)$node->term_slug,
93
            ],
94
            (string)$node->term_description
95
        );
96
    }
97
}
98