Passed
Branch master (609f2f)
by Dorian
08:59
created

YamlLibraryDumper::buildArray()   B

Complexity

Conditions 11
Paths 88

Size

Total Lines 68
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 39
c 1
b 0
f 0
nc 88
nop 1
dl 0
loc 68
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gnutix\Library\Dumper;
6
7
use Gnutix\Library\LibraryDumperInterface;
8
use Gnutix\Library\LibraryInterface;
9
use Gnutix\Library\Model\Book;
10
use Symfony\Component\Yaml\Dumper;
11
12
class YamlLibraryDumper implements LibraryDumperInterface
13
{
14
    public function dump(LibraryInterface $library): string
15
    {
16
        $dumper = new Dumper();
17
        $dump = $dumper->dump($this->buildArray($library), 99);
18
19
        // Apply fixes on the dump output
20
        return (string) preg_replace(
21
            ["#^( +?-)(?:\n)( +?id: )'(&[a-zA-Z_]+)/([a-zA-Z_]+)'$#m", "#''#", "#'#", '#§#', '#null#'],
22
            ['$1 $3'.PHP_EOL.'$2$4', '§', '', "'", '~'],
23
            $dump
24
        );
25
    }
26
27
    protected function buildArray(LibraryInterface $library): array
28
    {
29
        $array = [
30
            'languages' => [],
31
            'formats' => [
32
                [
33
                    'id' => '&f_unknown/unknown',
34
                    'name' => 'Unknown',
35
                ],
36
            ],
37
            'categories' => [],
38
            'editors' => [],
39
            'authors' => [],
40
            'books' => [],
41
        ];
42
43
        foreach ($library->getCategories() as $category) {
44
            $array['categories'][] = [
45
                'id' => '&c_'.$category->getId().'/'.$category->getId(),
46
                'name' => $category->getName(),
47
            ];
48
        }
49
        foreach ($library->getEditors() as $editor) {
50
            $array['editors'][] = [
51
                'id' => '&e_'.$editor->getId().'/'.$editor->getId(),
52
                'name' => $editor->getName(),
53
                'preferredLanguage' => $editor->getPreferredLanguage(),
54
            ];
55
        }
56
57
        foreach ($library->getBooks() as $book) {
58
            $array['books'][] = $this->buildBookArray($book);
59
60
            foreach ($book->getAuthors() as $author) {
61
                $skip = false;
62
63
                foreach ($array['authors'] as $authorArray) {
64
                    if (isset($authorArray['name']) && $authorArray['name'] === $author->getName()) {
65
                        $skip = true;
66
67
                        break;
68
                    }
69
                }
70
71
                if ($skip) {
72
                    continue;
73
                }
74
75
                $array['authors'][] = [
76
                    'id' => '&a_'.$author->getId().'/'.$author->getId(),
77
                    'name' => $author->getName(),
78
                ];
79
            }
80
81
            foreach ($book->getReleases() as $release) {
82
                if (isset($array['languages'][$release->getLanguage()->getId()])) {
83
                    continue;
84
                }
85
                $array['languages'][$release->getLanguage()->getId()] = [
86
                    'id' => '&l_'.$release->getLanguage()->getId().'/'.$release->getLanguage()->getId(),
87
                    'name' => $release->getLanguage()->getName(),
88
                ];
89
            }
90
        }
91
92
        $array['languages'] = array_values($array['languages']);
93
94
        return $array;
95
    }
96
97
    protected function buildBookArray(Book $book): array
98
    {
99
        $bookArray = [
100
            'authors' => [],
101
            'category' => '*c_'.$book->getCategory()->getId(),
102
            'releases' => [],
103
        ];
104
105
        foreach ($book->getAuthors() as $author) {
106
            $bookArray['authors'][] = '*a_'.$author->getId();
107
        }
108
109
        foreach ($book->getReleases() as $release) {
110
            $publicationDate = $release->getPublicationDate();
111
112
            if ($publicationDate instanceof \DateTime) {
113
                $publicationDate = [
114
                    'day' => $publicationDate->format('d'),
115
                    'month' => $publicationDate->format('m'),
116
                    'year' => $publicationDate->format('Y'),
117
                ];
118
            }
119
120
            $bookArray['releases'][] = [
121
                'title' => $release->getTitle(),
122
                'language' => '*l_'.$release->getLanguage()->getId(),
123
                'publicationDate' => $publicationDate,
124
                'editor' => '*e_'.$release->getEditor()->getId(),
125
                'format' => '*f_unknown',
126
                'owner' => [
127
                    'copies' => $release->getOwner()->getNbCopies(),
128
                    'readings' => $release->getOwner()->getNbReadings(),
129
                ],
130
                'series' => [
131
                    'id' => $release->getSeries()->getId(),
132
                    'title' => $release->getSeries()->getTitle(),
133
                    'number' => $release->getSeries()->getBookId(),
134
                ],
135
            ];
136
        }
137
138
        return $bookArray;
139
    }
140
}
141