Config::addTaxonomy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
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
use Rarst\Hugo\wprss2hugo\Store;
8
9
/**
10
 * Site-side configuration, with defaults, adding things, and storing result.
11
 */
12
class Config implements Handler, Store
13
{
14
    /** @var array{params: array<string,string>} */
15
    private $config = [
16
        'title'      => '',
17
        'baseURL'    => '',
18
        'params'     => [
19
            'description' => '',
20
        ],
21
        'permalinks' => [ // WP term permalinks are typically singular.
22
            'categories' => '/category/:slug/',
23
            'tags'       => '/tag/:slug/',
24
            'formats'    => '/format/:slug/',
25
            'authors'    => '/author/:slug/',
26
        ],
27
        'taxonomies' => [
28
            'category' => 'categories',
29
            'tag'      => 'tags',
30
            'format'   => 'formats',
31
            'author'   => 'authors',
32
        ],
33
    ];
34
35
    /** @var Write */
36
    private $store;
37
38
    /**
39
     * Set up with writable store.
40
     */
41
    public function __construct(Write $store)
42
    {
43
        $this->store = $store;
44
    }
45
46
    /**
47
     * Handle configuration-related XML nodes.
48
     */
49
    public function handle(\SimpleXMLElement $node): void
50
    {
51
        switch ($node->getName()) {
52
            case 'title':
53
                $this->config['title'] = (string)$node;
54
                break;
55
56
            case 'description':
57
                $this->config['params']['description'] = (string)$node;
58
                break;
59
60
            case 'link':
61
                $this->config['baseURL'] = (string)$node;
62
                break;
63
        }
64
    }
65
66
    /**
67
     * Add a taxonomy to the configuration.
68
     */
69
    public function addTaxonomy(string $singular, string $plural): void
70
    {
71
        if (!isset($this->config['taxonomies'][$singular])) {
72
            $this->config['taxonomies'][$singular] = $plural;
73
        }
74
    }
75
76
    public function store(): void
77
    {
78
        $this->store->data('config', $this->config);
79
    }
80
}
81