Passed
Push — master ( d0e7a6...44374d )
by Dispositif
02:28
created

Wikidata2Ouvrage   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 102
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A completeTitleLink() 0 16 5
A __construct() 0 13 1
A getOuvrage() 0 3 1
A complete() 0 11 2
A completeAuthorLink() 0 16 4
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 © 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
namespace App\Domain\Publisher;
11
12
use App\Domain\Models\Wiki\OuvrageTemplate;
13
use App\Domain\Utils\WikiTextUtil;
14
use App\Infrastructure\WikidataAdapter;
15
use GuzzleHttp\Client;
16
17
/**
18
 * Complete un OuvrageTemplate à partir des données Infos[] en faisant
19
 * des requêtes vers Wikidata (ISBN => article du titre livre et ISNI => article
20
 * de l'auteur).
21
 *
22
 * Class Wikidata2Ouvrage
23
 *
24
 * @package App\Domain\Publisher
25
 */
26
class Wikidata2Ouvrage
27
{
28
29
    private $ouvrage;
30
    /**
31
     * @var array
32
     */
33
    private $infos;
34
    private $adapter;
35
    public $log = [];
36
    /**
37
     * @var array|null
38
     */
39
    private $data;
40
41
    /**
42
     * Wikidata2Ouvrage constructor.
43
     *
44
     * @param OuvrageTemplate $ouvrage
45
     *
46
     * @throws \Exception
47
     */
48
    public function __construct(OuvrageTemplate $ouvrage)
49
    {
50
        // todo dependency injection
51
        $this->adapter = new WikidataAdapter(
52
            new Client(['timeout' => 5, 'headers' => ['User-Agent' => getenv('USER_AGENT')]])
53
        );
54
55
        $clone = clone $ouvrage;
56
        $this->infos = $clone->getInfos();
57
        $clone->setInfos([]); // suppression Infos
58
        $clone->setSource('WikiData');
59
        $this->ouvrage = $clone;
60
        $this->complete();
61
    }
62
63
    public function getOuvrage(): OuvrageTemplate
64
    {
65
        return $this->ouvrage;
66
    }
67
68
    /**
69
     * quick and dirty.
70
     *
71
     * @throws \Exception
72
     */
73
    private function complete(): void
74
    {
75
        $this->data = $this->adapter->getDataByInfos($this->infos);
76
77
        if (empty($this->data)) {
78
            return;
79
        }
80
81
82
        $this->completeAuthorLink();
83
        $this->completeTitleLink();
84
    }
85
86
    /**
87
     * @return void
88
     * @throws \Exception
89
     */
90
    private function completeAuthorLink(): void
91
    {
92
        // Note : auteur1 non wikifié puisque venant de BnF
93
        if (!empty($this->data['articleAuthor']) && !empty($this->data['articleAuthor']['value'])
94
            && empty($this->ouvrage->getParam('lien auteur1'))
95
        ) {
96
            // ajout wikilien auteur1
97
            // "https://fr.wikipedia.org/wiki/Michel_Houellebecq"
98
            $lienTitre = str_replace(
99
                ['https://fr.wikipedia.org/wiki/', '_'],
100
                ['', ' '],
101
                $this->data['articleAuthor']['value']
102
            );
103
            $this->ouvrage->setParam('lien auteur1', $lienTitre);
104
            dump('Wikidata2Ouvrage: +lien auteur1='.$lienTitre);
105
            $this->log[] = '+lien auteur1';
106
        }
107
    }
108
109
    /**
110
     * @throws \Exception
111
     */
112
    private function completeTitleLink(): void
113
    {
114
        if (!empty($this->data['articleBook']) && !empty($this->data['articleBook']['value'])
115
            && empty($this->ouvrage->getParam('lien titre'))
116
            && false === WikiTextUtil::isWikify($this->ouvrage->getParam('titre'))
0 ignored issues
show
Bug introduced by
It seems like $this->ouvrage->getParam('titre') can also be of type null; however, parameter $text of App\Domain\Utils\WikiTextUtil::isWikify() 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

116
            && false === WikiTextUtil::isWikify(/** @scrutinizer ignore-type */ $this->ouvrage->getParam('titre'))
Loading history...
117
        ) {
118
            // ajout wikilien titre
119
            // "https://fr.wikipedia.org/wiki/La_Carte_et_le_Territoire"
120
            $lienTitre = str_replace(
121
                ['https://fr.wikipedia.org/wiki/', '_'],
122
                ['', ' '],
123
                $this->data['articleBook']['value']
124
            );
125
            $this->ouvrage->setParam('lien titre', $lienTitre);
126
            dump('Wikidata2Ouvrage: +lien titre='.$lienTitre);
127
            $this->log[] = '+lien titre';
128
        }
129
    }
130
131
}
132