Passed
Push — master ( 2d0039...fd6b1a )
by Dispositif
02:43
created

IsbnFacade   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 21
c 0
b 0
f 0
dl 0
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCountryShortName() 0 12 3
A isbn2ean() 0 3 1
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Infrastructure;
11
12
use App\Domain\InfrastructurePorts\IsbnConverterInterface;
13
use Biblys\Isbn\Isbn;
14
15
class IsbnFacade extends Isbn implements IsbnConverterInterface
16
{
17
    public const ERROR_EMPTY = 'aucun code fourni';
18
19
    public const ERROR_INVALID_CHARACTERS = 'caractères invalides';
20
21
    public const ERROR_INVALID_LENGTH = 'trop court ou trop long';
22
23
    public const ERROR_INVALID_PRODUCT_CODE = 'code produit devrait être 978 ou 979';
24
25
    public const ERROR_INVALID_COUNTRY_CODE = 'code pays inconnu';
26
27
    // TODO: complete array.
28
    public const ISBN_LANGUAGE_CODES
29
        = [
30
            '0' => 'en',
31
            '1' => 'en',
32
            '2' => 'fr',
33
            '3' => 'de',
34
            '4' => 'ja',
35
            '5' => 'ru',
36
            '88' => 'it',
37
        ];
38
39
    public static function isbn2ean(string $isbn): string
40
    {
41
        return preg_replace('#[^0-9X]#i', '', $isbn);
42
    }
43
44
    public function getCountryShortName(): ?string
45
    {
46
        $langCode = $this->getCountry() ?? '';
47
        if (empty($langCode)) {
48
            return null;
49
        }
50
51
        if (array_key_exists($langCode, self::ISBN_LANGUAGE_CODES)) {
52
            return self::ISBN_LANGUAGE_CODES[$langCode];
53
        }
54
55
        return null;
56
    }
57
}
58