Test Failed
Push — master ( aa40d3...766a39 )
by Dispositif
07:56
created

GoogleBookMapper::convertIsbn()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.0368

Importance

Changes 0
Metric Value
cc 7
eloc 10
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
crap 7.0368
rs 8.8333
1
<?php /**
2
 * This file is part of dispositif/wikibot application (@github)
3
 * 2019/2020 © Philippe M. <[email protected]>
4
 * For the full copyright and MIT license information, please view the license file.
5
 */ /** @noinspection PhpUndefinedFieldInspection */
6
7
/**
8
 * This file is part of dispositif/wikibot application
9
 * 2019 : Philippe M. <[email protected]>
10
 * For the full copyright and MIT license information, please view the LICENSE file.
11
 */
12
13
declare(strict_types=1);
14
15
namespace App\Domain\Publisher;
16
17
use App\Domain\Enums\Language;
18
use Scriptotek\GoogleBooks\Volume;
19
20
/**
21
 * Google Books API data mapping.
22
 * Doc : https://developers.google.com/books/docs/v1/reference/volumes
23
 * Class GoogleBookMapper.
24
 */
25
class GoogleBookMapper extends AbstractBookMapper implements MapperInterface
26
{
27
    // raw URL or wiki-template ?
28
    public const MODE_RAW_URL = true;
29
30
    public const GOOGLE_URL_REPLACE = 'https://books.google.com/books?id=%s&printsec=frontcover';
31
32
    // sous-titre non ajoutés :
33
    public const SUBTITLE_FILTER = ['roman', 'récit', 'poèmes', 'biographie'];
34
    /**
35
     * @var bool|null
36
     */
37
    private $mapLanguageData = false;
38
39
    /**
40
     * @param           $volume Volume
41
     *
42
     * @return array
43
     */
44 2
    public function process($volume): array
45
    {
46
        return [
47
            // language : data not accurate !
48 2
            'langue' => $this->langFilterByIsbn($volume),
49 2
            'auteur1' => $volume->authors[0],
50 2
            'auteur2' => $volume->authors[1] ?? null,
51 2
            'auteur3' => $volume->authors[2] ?? null,
52 2
            'titre' => $volume->title,
53 2
            'sous-titre' => $this->filterSubtitle($volume),
54 2
            'année' => $this->convertDate2Year($volume->publishedDate),
55 2
            'pages totales' => (string)$volume->pageCount ?? null,
56 2
            'isbn' => $this->convertIsbn($volume),
57 2
            'présentation en ligne' => $this->presentationEnLigne($volume),
58 2
            'lire en ligne' => $this->lireEnLigne($volume),
59
        ];
60
    }
61
62
    /**
63
     * Use the inconstant 'language' ?
64
     *
65
     * @param bool $useLanguage
66
     */
67 1
    public function mapLanguageData(bool $useLanguage)
68
    {
69 1
        $this->mapLanguageData = $useLanguage;
70 1
    }
71
72
    /**
73
     * @param $volume
74
     *
75
     * @return string|null
76
     */
77 2
    private function filterSubtitle($volume): ?string
78
    {
79
        // "biographie" ?
80 2
        if (empty($volume->subtitle) || in_array(mb_strtolower($volume->subtitle), self::SUBTITLE_FILTER)) {
81
            return null;
82
        }
83
84 2
        if (strlen($volume->subtitle) > 50) {
85
            return null;
86
        }
87
88 2
        return $volume->subtitle;
89
    }
90
91
    /**
92
     * @param $data
93
     *
94
     * @return string|null
95
     */
96 2
    private function convertDate2Year($data)
97
    {
98 2
        if (!isset($data)) {
99
            return null;
100
        }
101 2
        if (preg_match('/[^0-9]?([12]\d{3})[^0-9]?/', $data, $matches) > 0) {
102 2
            return (string)$matches[1];
103
        }
104
105
        return null;
106
    }
107
108
    /**
109
     * @param Volume $volume
110
     *
111
     * @return string|null
112
     */
113 2
    private function convertIsbn(Volume $volume): ?string
114
    {
115 2
        if (!(property_exists($volume, 'industryIdentifiers') && $volume->industryIdentifiers !== null)) {
116
            return null;
117
        }
118
        // so isbn-13 replace isbn-10
119
        // todo refac algo (if 2x isbn13?)
120 2
        $isbn = null;
121 2
        $ids = (array)$volume->industryIdentifiers;
122 2
        foreach ($ids as $id) {
123 2
            if (!isset($isbn) && in_array($id->type, ['ISBN_10', 'ISBN_13'])) {
124 2
                $isbn = $id->identifier;
125
            }
126 2
            if ('ISBN_13' === $id->type) {
127 2
                $isbn = $id->identifier;
128
            }
129
        }
130
131 2
        return $isbn;
132
    }
133
134
    /**
135
     * @param Volume $volume
136
     *
137
     * @return string|null
138
     */
139 2
    private function presentationEnLigne(Volume $volume): ?string
140
    {
141 2
        if (empty($volume->id) || $volume->accessInfo->viewability != 'PARTIAL') {
142 2
            return null;
143
        }
144
145
        return $this->returnGoogleRef($volume);
146
    }
147
148
    /**
149
     * @param Volume $volume
150
     *
151
     * @return string|null
152
     */
153 2
    private function lireEnLigne(Volume $volume): ?string
154
    {
155 2
        if (empty($volume->id) || $volume->accessInfo->viewability != 'ALL_PAGES') {
156 2
            return null;
157
        }
158
159
        return $this->returnGoogleRef($volume);
160
    }
161
162
    /**
163
     * @param Volume $volume
164
     *
165
     * @return string
166
     */
167
    private function returnGoogleRef(Volume $volume): string
168
    {
169
        if (self::MODE_RAW_URL) {
170
            return sprintf(self::GOOGLE_URL_REPLACE, $volume->id);
171
        }
172
173
        return sprintf('{{Google Livres|%s}}', $volume->id);
174
    }
175
176
    /**
177
     * /!\ Google Books language data not consistant !
178
     * set $mapLanguageData to true to use that data in mapping.
179
     *
180
     * @param Volume $volume
181
     *
182
     * @return string|null
183
     */
184 2
    private function langFilterByIsbn(Volume $volume): ?string
185
    {
186 2
        if ($this->mapLanguageData !== true) {
187 1
            return null;
188
        }
189
//        $isbn = $this->convertIsbn($volume);
190
//        if ($isbn) {
191
//            try {
192
//                $isbnMachine = new IsbnFacade($isbn);
193
//                $isbnMachine->validate();
194
//                $isbnLang = $isbnMachine->getCountryShortName();
195
//            } catch (\Throwable $e) {
196
//                unset($e);
197
//            }
198
//        }
199
//        if (isset($isbnLang)) {
200
//            echo "(ISBN lang: ".$isbnLang.")\n";
201
//        } else {
202
//            echo "(no ISBN lang)\n";
203
//        }
204
205 1
        $gooLang = $volume->language;
206
        // filtering lang because seems inconsistant
207
//        if (isset($isbnLang) && !empty($gooLang) && $gooLang !== $isbnLang) {
208
//            echo sprintf(
209
//                "*** Filtering Google langue=%s because ISBN langue=%s ",
210
//                $gooLang,
211
//                $isbnLang
212
//            );
213
//
214
//            return null;
215
//        }
216
217 1
        return Language::all2wiki($gooLang);
218
    }
219
}
220