|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of dispositif/wikibot application (@github) |
|
4
|
|
|
* 2019/2020 © 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
|
|
|
|
|
11
|
|
|
namespace App\Domain; |
|
12
|
|
|
|
|
13
|
|
|
use Exception; |
|
14
|
|
|
use Throwable; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* TODO refac : too many conditions |
|
18
|
|
|
* Trait OptimizeISBNTrait |
|
19
|
|
|
*/ |
|
20
|
|
|
trait OptimizeISBNTrait |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Refac complexity (lines, 20 conditions) |
|
24
|
|
|
* Validate or correct ISBN. |
|
25
|
|
|
* |
|
26
|
|
|
* @throws Exception |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function processIsbn() |
|
29
|
|
|
{ |
|
30
|
|
|
$isbn = $this->getParam('isbn') ?? ''; |
|
|
|
|
|
|
31
|
|
|
if (empty($isbn)) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// ISBN-13 à partir de 2007 |
|
36
|
|
|
$year = $this->findBookYear(); |
|
|
|
|
|
|
37
|
|
|
if ($year !== null && $year < 2007 && 10 === strlen($this->stripIsbn($isbn))) { |
|
|
|
|
|
|
38
|
|
|
// juste mise en forme ISBN-10 pour 'isbn' |
|
39
|
|
|
try { |
|
40
|
|
|
$isbnMachine = new IsbnFacade($isbn); |
|
41
|
|
|
$isbnMachine->validate(); |
|
42
|
|
|
$isbn10pretty = $isbnMachine->format('ISBN-10'); |
|
43
|
|
|
if ($isbn10pretty !== $isbn) { |
|
44
|
|
|
$this->setParam('isbn', $isbn10pretty); |
|
|
|
|
|
|
45
|
|
|
$this->addSummaryLog('ISBN10'); |
|
|
|
|
|
|
46
|
|
|
// $this->notCosmetic = true; |
|
47
|
|
|
} |
|
48
|
|
|
} catch (Throwable $e) { |
|
49
|
|
|
// ISBN not validated |
|
50
|
|
|
$this->setParam( |
|
51
|
|
|
'isbn invalide', |
|
52
|
|
|
sprintf('%s %s', $isbn, $e->getMessage() ?? '') |
|
53
|
|
|
); |
|
54
|
|
|
$this->addSummaryLog(sprintf('ISBN invalide: %s', $e->getMessage())); |
|
55
|
|
|
$this->notCosmetic = true; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
|
|
$isbnMachine = new IsbnFacade($isbn); |
|
63
|
|
|
$isbnMachine->validate(); |
|
64
|
|
|
$isbn13 = $isbnMachine->format('ISBN-13'); |
|
65
|
|
|
} catch (Throwable $e) { |
|
66
|
|
|
// ISBN not validated |
|
67
|
|
|
// TODO : bot ISBN invalide (queue, message PD...) |
|
68
|
|
|
$this->setParam( |
|
69
|
|
|
'isbn invalide', |
|
70
|
|
|
sprintf('%s %s', $isbn, $e->getMessage() ?? '') |
|
71
|
|
|
); |
|
72
|
|
|
$this->addSummaryLog(sprintf('ISBN invalide: %s', $e->getMessage())); |
|
73
|
|
|
$this->notCosmetic = true; |
|
74
|
|
|
|
|
75
|
|
|
// TODO log file ISBNinvalide |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Si $isbn13 et 'isbn2' correspond à ISBN-13 => suppression |
|
80
|
|
|
if (isset($isbn13) |
|
81
|
|
|
&& $this->hasParamValue('isbn2') |
|
|
|
|
|
|
82
|
|
|
&& $this->stripIsbn($this->getParam('isbn2')) === $this->stripIsbn($isbn13) |
|
83
|
|
|
) { |
|
84
|
|
|
$this->unsetParam('isbn2'); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// ISBN-10 ? |
|
88
|
|
|
$stripIsbn = $this->stripIsbn($isbn); |
|
89
|
|
|
if (10 === mb_strlen($stripIsbn)) { |
|
90
|
|
|
// ajout des tirets |
|
91
|
|
|
$isbn10pretty = $isbn; |
|
92
|
|
|
|
|
93
|
|
|
try { |
|
94
|
|
|
$isbn10pretty = $isbnMachine->format('ISBN-10'); |
|
95
|
|
|
} catch (Throwable $e) { |
|
96
|
|
|
unset($e); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// archivage ISBN-10 dans 'isbn2' |
|
100
|
|
|
if (!$this->getParam('isbn2')) { |
|
101
|
|
|
$this->setParam('isbn2', $isbn10pretty); |
|
102
|
|
|
} |
|
103
|
|
|
// sinon dans 'isbn3' |
|
104
|
|
|
if ($this->hasParamValue('isbn2') |
|
105
|
|
|
&& $this->stripIsbn($this->getParam('isbn2')) !== $stripIsbn |
|
106
|
|
|
&& empty($this->getParam('isbn3')) |
|
107
|
|
|
) { |
|
108
|
|
|
$this->setParam('isbn3', $isbn10pretty); |
|
109
|
|
|
} |
|
110
|
|
|
// delete 'isbn10' (en attendant modification modèle) |
|
111
|
|
|
if ($this->hasParamValue('isbn10') && $this->stripIsbn($this->getParam('isbn10')) === $stripIsbn) { |
|
112
|
|
|
$this->unsetParam('isbn10'); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// ISBN correction |
|
117
|
|
|
if ($isbn13 !== $isbn) { |
|
118
|
|
|
$this->setParam('isbn', $isbn13); |
|
119
|
|
|
$this->addSummaryLog('ISBN'); |
|
120
|
|
|
// $this->notCosmetic = true; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|