YamlLibraryDumper   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 87
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildBookArray() 0 16 1
A transformToSplitYamlDate() 0 19 4
A buildArray() 0 17 2
A buildTimeArray() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gnutix\StarWarsLibrary\Dumper;
6
7
use Gnutix\Library\Dumper\YamlLibraryDumper as BaseYamlLibraryDumper;
8
use Gnutix\Library\LibraryInterface;
9
use Gnutix\Library\Model\Book;
10
use Gnutix\StarWarsLibrary\Model\Book as StarWarsBook;
11
use Gnutix\StarWarsLibrary\Model\ChronologicalMarker;
12
use Gnutix\StarWarsLibrary\Model\Library as StarWarsLibrary;
13
use Webmozart\Assert\Assert;
14
15
final class YamlLibraryDumper extends BaseYamlLibraryDumper
16
{
17
    /**
18
     * @param string|float|null $dateInput
19
     */
20
    public function transformToSplitYamlDate($dateInput): array
21
    {
22
        $dates = [];
23
24
        foreach (explode(' - ', trim((string) $dateInput)) as $date) {
25
            $date = str_replace([' ', 'ABY'], '', $date);
26
27
            if (false !== strpos($date, 'BBY')) {
28
                $date = '-'.trim(str_replace('BBY', '', $date));
29
            }
30
31
            $dates[] = $date;
32
        }
33
34
        $results = $dates;
35
36
        return [
37
            'start' => trim($results[0]),
38
            'end' => isset($results[1]) ? trim($results[1]) : null,
39
        ];
40
    }
41
42
    /**
43
     * @param LibraryInterface&StarWarsLibrary $library
44
     */
45
    protected function buildArray(LibraryInterface $library): array
46
    {
47
        /** @var StarWarsLibrary $library */
48
        Assert::isInstanceOf($library, StarWarsLibrary::class);
49
50
        $eras = [];
51
52
        foreach ($library->getEras() as $era) {
53
            $eras[] = [
54
                'id' => '&era_'.$era->getId().'/'.$era->getId(),
55
                'name' => $era->getName(),
56
            ];
57
        }
58
59
        return array_merge([
60
            'eras' => $eras,
61
        ], parent::buildArray($library));
62
    }
63
64
    /**
65
     * @param Book&StarWarsBook $book
66
     */
67
    protected function buildBookArray(Book $book): array
68
    {
69
        /** @var StarWarsBook $book */
70
        Assert::isInstanceOf($book, StarWarsBook::class);
71
72
        return array_merge(
73
            [
74
                'starWars' => [
75
                    'chronology' => [
76
                        'era' => '*era_'.$book->getChronologicalMarker()->getEra()->getId(),
77
                        'time' => $this->buildTimeArray($book->getChronologicalMarker()),
78
                    ],
79
                    'swuBookId' => null,
80
                ],
81
            ],
82
            parent::buildBookArray($book)
83
        );
84
    }
85
86
    protected function buildTimeArray(ChronologicalMarker $chronologicalMarker): array
87
    {
88
        $start = $chronologicalMarker->getTimeStart();
89
90
        if (empty($start)) {
91
            $start = null;
92
            $end = null;
93
        } else {
94
            $date = $this->transformToSplitYamlDate($start);
95
            $start = $date['start'];
96
            $end = $date['end'];
97
        }
98
99
        return [
100
            'start' => $start,
101
            'end' => $end,
102
        ];
103
    }
104
}
105