Passed
Branch dev3 (6039a0)
by Dispositif
02:46
created

GoogleBooksUrlHandler::sethandlerParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
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
use App\Domain\Models\Wiki\GoogleLivresTemplate;
14
use App\Domain\Publisher\GoogleBooksUtil;
15
use App\Domain\WikiOptimizer\OuvrageOptimize;
16
use DomainException;
17
18
/**
19
 * Normalize a Google Book links.
20
 * Clean the useless URL parameters or transform into wiki-template.
21
 */
22
class GoogleBooksUrlHandler extends AbstractOuvrageHandler
23
{
24
    protected $handlerParam = 'lire en ligne';
25
26
    public function sethandlerParam(string $handlerParam = 'lire en ligne'): self
27
    {
28
        $this->handlerParam = $handlerParam;
29
        return $this;
30
    }
31
32
    public function handle()
33
    {
34
        $url = $this->getParam($this->handlerParam);
35
        if (empty($url)
36
            || !GoogleBooksUtil::isGoogleBookURL($url)
37
        ) {
38
            return;
39
        }
40
41
        if (OuvrageOptimize::CONVERT_GOOGLEBOOK_TEMPLATE) {
42
            $template = GoogleLivresTemplate::createFromURL($url);
43
            if ($template !== null) {
44
                $this->setParam($this->handlerParam, $template->serialize());
45
                $this->addSummaryLog('{Google}');
46
                $this->optiStatus->setNotCosmetic(true);
47
48
                return;
49
            }
50
        }
51
52
        try {
53
            $goo = GoogleBooksUtil::simplifyGoogleUrl($url);
54
        } catch (DomainException $e) {
55
            // ID manquant ou malformé
56
            $errorValue = sprintf(
57
                '%s <!-- ERREUR %s -->',
58
                $url,
59
                $e->getMessage()
60
            );
61
            $this->setParam($this->handlerParam, $errorValue);
62
            $this->addSummaryLog('erreur URL');
63
            $this->optiStatus->setNotCosmetic(true);
64
            $this->optiStatus->setMajor(true);
65
        }
66
67
        if (!empty($goo) && $goo !== $url) {
68
            $this->setParam($this->handlerParam, $goo);
69
            // cleaned tracking parameters in Google URL ?
70
            if (GoogleBooksUtil::isTrackingUrl($url)) {
71
                $this->addSummaryLog('tracking');
72
                $this->optiStatus->setNotCosmetic(true);
73
            }
74
        }
75
    }
76
}