|
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\Domain; |
|
11
|
|
|
|
|
12
|
|
|
use App\Domain\InfrastructurePorts\BookApiInterface; |
|
13
|
|
|
use App\Domain\Models\Wiki\OuvrageTemplate; |
|
14
|
|
|
use Exception; |
|
15
|
|
|
use LogicException; |
|
16
|
|
|
use Scriptotek\GoogleBooks\Volume; |
|
17
|
|
|
use SimpleXMLElement; |
|
18
|
|
|
use Throwable; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* TODO HEXA : create interface for Scriptotek GB Volume !! |
|
22
|
|
|
*/ |
|
23
|
|
|
class ImportOuvrageFromApi |
|
24
|
|
|
{ |
|
25
|
|
|
private readonly OuvrageTemplate $ouvrage; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* OuvrageFromApi constructor. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(OuvrageTemplate $ouvrage, private readonly BookApiInterface $adapter) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->ouvrage = clone $ouvrage; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Inutile si pas de clonage $ouvrage dans construct(). |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getOuvrage(): ?OuvrageTemplate |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->ouvrage; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* |
|
45
|
|
|
* |
|
46
|
|
|
* @throws Exception |
|
47
|
|
|
*/ |
|
48
|
|
|
public function hydrateFromIsbn(string $isbn): OuvrageTemplate |
|
49
|
|
|
{ |
|
50
|
|
|
$e = null; |
|
|
|
|
|
|
51
|
|
|
$volume = $this->getDataByIsbn($isbn); |
|
52
|
|
|
/** |
|
53
|
|
|
* @var Volume |
|
54
|
|
|
*/ |
|
55
|
|
|
$data = $this->mapping($volume); |
|
56
|
|
|
|
|
57
|
|
|
if(isset($data['infos'])) { |
|
58
|
|
|
$infos = $data['infos']; |
|
59
|
|
|
unset($data['infos']); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
$this->ouvrage->hydrate($data); |
|
64
|
|
|
if(isset($infos)) { |
|
65
|
|
|
$this->ouvrage->setInfos($infos); |
|
66
|
|
|
} |
|
67
|
|
|
} catch (Throwable $e) { |
|
68
|
|
|
throw new LogicException('Hydratation error '.$e->getMessage(), $e->getCode(), $e); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->ouvrage; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $volume |
|
76
|
|
|
*/ |
|
77
|
|
|
private function mapping($volume): array |
|
78
|
|
|
{ |
|
79
|
|
|
$mapper = $this->adapter->getMapper(); |
|
80
|
|
|
|
|
81
|
|
|
// FIXED : empty(SimpleXmlElement) => false !! |
|
82
|
|
|
if (empty($volume) && !$volume instanceof SimpleXMLElement) { |
|
83
|
|
|
return []; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/* |
|
87
|
|
|
* @var $mapper MapperInterface |
|
88
|
|
|
*/ |
|
89
|
|
|
return $mapper->process($volume); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function getDataByIsbn(string $isbn) |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->adapter->getDataByIsbn($isbn); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|