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

BnfFromIsbnHandler::handle()   B

Complexity

Conditions 8
Paths 22

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 29
rs 8.4444
cc 8
nc 22
nop 0
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\Application\OuvrageComplete\Handlers;
11
12
use App\Domain\Models\Wiki\OuvrageTemplate;
13
use App\Domain\OuvrageFactory;
14
use Psr\Log\LoggerInterface;
15
use Throwable;
16
17
class BnfFromIsbnHandler implements CompleteHandlerInterface
18
{
19
20
    /**
21
     * @var string
22
     */
23
    protected $isbn;
24
    /**
25
     * @var string|null
26
     */
27
    protected $isbn10;
28
    /**
29
     * @var LoggerInterface
30
     */
31
    protected $logger;
32
33
    public function __construct(string $isbn, ?string $isbn10, LoggerInterface $logger)
34
    {
35
        $this->isbn = $isbn;
36
        $this->isbn10 = $isbn10;
37
        $this->logger = $logger;
38
    }
39
40
    public function handle(): ?OuvrageTemplate
41
    {
42
        try {
43
            $this->logger->debug('BIBLIO NAT FRANCE...');
44
            // BnF sait pas trouver un vieux livre (10) d'après ISBN-13... FACEPALM !
45
            $bnfOuvrage = null;
46
            if ($this->isbn10) {
47
                $bnfOuvrage = OuvrageFactory::BnfFromIsbn($this->isbn10);
48
                sleep(2);
49
            }
50
            if (!$this->isbn10 || null === $bnfOuvrage || empty($bnfOuvrage->getParam('titre'))) {
51
                $bnfOuvrage = OuvrageFactory::BnfFromIsbn($this->isbn);
52
            }
53
54
        } catch (Throwable $e) {
55
            if (strpos($e->getMessage(), 'Could not resolve host') !== false) {
56
                throw $e;
57
            }
58
            $this->logger->error(sprintf(
59
                "*** ERREUR BnF Isbn Search %s %s %s \n",
60
                $e->getMessage(),
61
                $e->getFile(),
62
                $e->getLine()
63
            ));
64
65
            return null;
66
        }
67
68
        return $bnfOuvrage instanceof OuvrageTemplate ? $bnfOuvrage : null;
0 ignored issues
show
introduced by
$bnfOuvrage is always a sub-type of App\Domain\Models\Wiki\OuvrageTemplate.
Loading history...
69
    }
70
}