OpenLibraryMapper::readOnline()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
cc 4
nc 2
nop 1
crap 20
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 : Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\Publisher;
11
12
/**
13
 * https://openlibrary.org/dev/docs/api/books
14
 * Class OpenLibraryMapper.
15
 */
16
class OpenLibraryMapper extends AbstractBookMapper implements MapperInterface
17
{
18
    /**
19
     * @param $data array
20
     *
21
     * @return array
22
     */
23
    public function process($data): array
24
    {
25
        $details = $data['details'];
26
27
        // authors : see also 'contributions'
28
        return [
29
            'auteur1' => $details['authors'][0]['name'] ?? null,
30
            'auteur2' => $details['authors'][1]['name'] ?? null,
31
            'auteur3' => $details['authors'][2]['name'] ?? null,
32
            'titre' => $details['title'] ?? null,
33
            'sous-titre' => $this->filterSubtitle($details),
34
            'éditeur' => $details['publishers'][0]['name'] ?? null,
35
            'année' => $this->convertDate2Year($details),
36
            'lieu' => ($details['publish_places'][0]['name']) ?? null,
37
            'pages totales' => $this->nbPages($details),
38
            'lire en ligne' => $this->readOnline($data),
39
//            'présentation en ligne' => $this->previewOnline($data), // pas de consensus
40
        ];
41
    }
42
43
    private function filterSubtitle(array $details): ?string
44
    {
45
        if (!isset($details['subtitle']) || 'roman' === mb_strtolower((string) $details['subtitle'])) {
46
            return null;
47
        }
48
49
        if (strlen((string) $details['subtitle']) > 80) {
50
            return null;
51
        }
52
53
        return $details['subtitle'];
54
    }
55
56
    /**
57
     * TODO : lire en ligne !
58
     * preview : Preview state - either "noview" or "full".
59
     * preview_url : A URL to the preview of the book.
60
     * This links to the archive.org page when a readable version of the book is available, otherwise it links to the
61
     * book page on openlibrary.org.
62
     * Please note that the preview_url is always provided even if there is no readable version available. The preview
63
     * property should be used to test if a book is readable.
64
     *
65
     * @param $data
66
     */
67
    private function readOnline($data): ?string
68
    {
69
        if (!empty($data['preview_url']) && isset($data['preview']) && 'full' === $data['preview']) {
70
            return $data['preview_url'];
71
        }
72
73
        return null;
74
    }
75
76
    private function convertDate2Year($details): ?string
77
    {
78
        if (!isset($details['publish_date'])) {
79
            return null;
80
        }
81
        if (preg_match('/[^0-9]?([12]\d{3})[^0-9]?/', (string) $details['publish_date'], $matches) > 0) {
82
            return (string) $matches[1];
83
        }
84
85
        return null;
86
    }
87
88
    private function nbPages($details)
89
    {
90
        return (isset($details['number_of_pages'])) ? ((int) $details['number_of_pages']) : null;
91
    }
92
}
93