Passed
Push — master ( 9e3ea7...ee24dc )
by Dispositif
02:33
created

OuvrageIsbnHandler::stripIsbn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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
11
namespace App\Domain\WikiOptimizer\Handlers;
12
13
14
use App\Domain\IsbnFacade;
15
use Exception;
16
use Throwable;
17
18
class OuvrageIsbnHandler extends AbstractOuvrageHandler
19
{
20
    /**
21
     * Refac complexity (lines, 20 conditions)
22
     * Validate or correct ISBN.
23
     * @throws Exception
24
     */
25
    public function handle()
26
    {
27
        $isbn = $this->getParam('isbn') ?? '';
28
        if (empty($isbn)) {
29
            return;
30
        }
31
32
        // ISBN-13 à partir de 2007
33
        $year = $this->findBookYear();
34
        if ($year !== null && $year < 2007 && 10 === strlen($this->stripIsbn($isbn))) {
35
            // juste mise en forme ISBN-10 pour 'isbn'
36
            try {
37
                $isbnMachine = new IsbnFacade($isbn);
38
                // skip trigger_error() for deprecated method
39
                @$isbnMachine->validate();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for validate(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

39
                /** @scrutinizer ignore-unhandled */ @$isbnMachine->validate();

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Deprecated Code introduced by
The function Biblys\Isbn\Isbn::validate() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

39
                @/** @scrutinizer ignore-deprecated */ $isbnMachine->validate();
Loading history...
40
                $isbn10pretty = $isbnMachine->format('ISBN-10');
0 ignored issues
show
Deprecated Code introduced by
The function Biblys\Isbn\Isbn::format() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

40
                $isbn10pretty = /** @scrutinizer ignore-deprecated */ $isbnMachine->format('ISBN-10');
Loading history...
41
                if ($isbn10pretty !== $isbn) {
42
                    $this->setParam('isbn', $isbn10pretty);
43
                    $this->addSummaryLog('ISBN10');
44
                    //                    $this->notCosmetic = true;
45
                }
46
            } catch (Throwable $e) {
47
                // ISBN not validated
48
                $this->setParam(
49
                    'isbn invalide',
50
                    sprintf('%s %s', $isbn, $e->getMessage() ?? '')
51
                );
52
                $this->addSummaryLog(sprintf('ISBN invalide: %s', $e->getMessage()));
53
                $this->optiStatus->setNotCosmetic(true);
54
            }
55
56
            return;
57
        }
58
59
        try {
60
            $isbnMachine = new IsbnFacade($isbn);
61
            // skip trigger_error() for deprecated method
62
            @$isbnMachine->validate();
0 ignored issues
show
Deprecated Code introduced by
The function Biblys\Isbn\Isbn::validate() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

62
            @/** @scrutinizer ignore-deprecated */ $isbnMachine->validate();
Loading history...
63
            $isbn13 = @$isbnMachine->format('ISBN-13');
0 ignored issues
show
Deprecated Code introduced by
The function Biblys\Isbn\Isbn::format() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

63
            $isbn13 = @/** @scrutinizer ignore-deprecated */ $isbnMachine->format('ISBN-13');
Loading history...
64
        } catch (Throwable $e) {
65
            // ISBN not validated
66
            // TODO : bot ISBN invalide (queue, message PD...)
67
            $this->setParam(
68
                'isbn invalide',
69
                sprintf('%s %s', $isbn, $e->getMessage() ?? '')
70
            );
71
            $this->addSummaryLog(sprintf('ISBN invalide: %s', $e->getMessage()));
72
            $this->optiStatus->setNotCosmetic(true);
73
74
            // TODO log file ISBNinvalide
75
            return;
76
        }
77
78
        // Si $isbn13 et 'isbn2' correspond à ISBN-13 => suppression
79
        if ($this->hasParamValue('isbn2')
80
            && $this->stripIsbn($this->getParam('isbn2')) === $this->stripIsbn($isbn13)
0 ignored issues
show
Bug introduced by
It seems like $this->getParam('isbn2') can also be of type null; however, parameter $isbn of App\Domain\WikiOptimizer...sbnHandler::stripIsbn() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            && $this->stripIsbn(/** @scrutinizer ignore-type */ $this->getParam('isbn2')) === $this->stripIsbn($isbn13)
Loading history...
81
        ) {
82
            $this->unsetParam('isbn2');
83
        }
84
85
        // ISBN-10 ?
86
        $stripIsbn = $this->stripIsbn($isbn);
87
        if (10 === mb_strlen($stripIsbn)) {
88
            // ajout des tirets
89
            $isbn10pretty = $isbn;
90
91
            try {
92
                $isbn10pretty = $isbnMachine->format('ISBN-10');
0 ignored issues
show
Deprecated Code introduced by
The function Biblys\Isbn\Isbn::format() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

92
                $isbn10pretty = /** @scrutinizer ignore-deprecated */ $isbnMachine->format('ISBN-10');
Loading history...
93
            } catch (Throwable $e) {
94
                unset($e);
95
            }
96
97
            // archivage ISBN-10 dans 'isbn2'
98
            if (!$this->getParam('isbn2')) {
99
                $this->setParam('isbn2', $isbn10pretty);
100
            }
101
            // sinon dans 'isbn3'
102
            if ($this->hasParamValue('isbn2')
103
                && $this->stripIsbn($this->getParam('isbn2')) !== $stripIsbn
104
                && empty($this->getParam('isbn3'))
105
            ) {
106
                $this->setParam('isbn3', $isbn10pretty);
107
            }
108
            // delete 'isbn10' (en attendant modification modèle)
109
            if ($this->hasParamValue('isbn10') && $this->stripIsbn($this->getParam('isbn10')) === $stripIsbn) {
110
                $this->unsetParam('isbn10');
111
            }
112
        }
113
114
        // ISBN correction
115
        if ($isbn13 !== $isbn) {
116
            $this->setParam('isbn', $isbn13);
117
            $this->addSummaryLog('ISBN');
118
            //            $this->notCosmetic = true;
119
        }
120
    }
121
122
    /**
123
     * Find year of book publication.
124
     */
125
    protected function findBookYear(): ?int
126
    {
127
        $annee = $this->getParam('année');
128
        if (!empty($annee) && is_numeric($annee)) {
129
            return (int)$annee;
130
        }
131
        $date = $this->getParam('date');
132
        if ($date && preg_match('#[^0-9]?([12]\d\d\d)[^0-9]?#', $date, $matches) > 0) {
133
            return (int)$matches[1];
134
        }
135
136
        return null;
137
    }
138
139
    protected function stripIsbn(string $isbn): string
140
    {
141
        return trim(preg_replace('#[^0-9Xx]#', '', $isbn));
142
    }
143
}