Passed
Push — master ( 99b995...983f4d )
by Dispositif
02:27
created

Wikidata2Ouvrage   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 128
rs 10
c 2
b 0
f 0
wmc 16

6 Methods

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

106
            if (TextUtil::mb_ucfirst($lienTitre) === TextUtil::mb_ucfirst(/** @scrutinizer ignore-type */ $this->title)) {
Loading history...
107
                // skip wikilink if this is the article title
108
                return;
109
            }
110
111
            $this->ouvrage->setParam('lien auteur1', $lienTitre);
112
            dump('Wikidata2Ouvrage: +lien auteur1='.$lienTitre);
113
            $this->log[] = '+lien auteur1';
114
        }
115
    }
116
117
    /**
118
     * TODO : move to WikiTextUtil ?
119
     * "https://fr.wikipedia.org/wiki/Michel_Houellebecq" => "Michel Houellebecq".
120
     *
121
     * @param $wikiURL
122
     *
123
     * @return string|null
124
     */
125
    private function wikiURL2title($wikiURL): ?string
126
    {
127
        $lienTitre = str_replace(
128
            ['https://fr.wikipedia.org/wiki/', '_'],
129
            ['', ' '],
130
            $wikiURL
131
        );
132
133
        return trim(urldecode($lienTitre));
134
    }
135
136
    /**
137
     * @throws Exception
138
     */
139
    private function completeTitleLink(): void
140
    {
141
        if (!empty($this->data['articleBook']) && !empty($this->data['articleBook']['value'])
142
            && empty($this->ouvrage->getParam('lien titre'))
143
            && 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

143
            && false === WikiTextUtil::isWikify(/** @scrutinizer ignore-type */ $this->ouvrage->getParam('titre'))
Loading history...
144
        ) {
145
            // ajout wikilien titre
146
            // "https://fr.wikipedia.org/wiki/La_Carte_et_le_Territoire"
147
            $lienTitre = $this->wikiURL2title($this->data['articleBook']['value']);
148
            if (TextUtil::mb_ucfirst($lienTitre) === TextUtil::mb_ucfirst($this->title)) {
0 ignored issues
show
Bug introduced by
It seems like $this->title can also be of type null; however, parameter $str of App\Domain\Utils\TextUtil::mb_ucfirst() 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

148
            if (TextUtil::mb_ucfirst($lienTitre) === TextUtil::mb_ucfirst(/** @scrutinizer ignore-type */ $this->title)) {
Loading history...
149
                // skip wikilink if this is the article title
150
                return;
151
            }
152
            $this->ouvrage->setParam('lien titre', $lienTitre);
153
            dump('Wikidata2Ouvrage: +lien titre='.$lienTitre);
154
            $this->log[] = '+lien titre';
155
        }
156
    }
157
158
}
159