Passed
Branch dev (b7aeac)
by Dispositif
03:10
created

GoogleLivresTemplate::simplifyGoogleUrl()   C

Complexity

Conditions 12
Paths 75

Size

Total Lines 75
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 26
nc 75
nop 1
dl 0
loc 75
rs 6.9666
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Models\Wiki;
11
12
use App\Domain\Publisher\GoogleBooksTrait;
13
use App\Domain\Utils\ArrayProcessTrait;
14
use DomainException;
15
use Exception;
16
17
/**
18
 * https://fr.wikipedia.org/wiki/Mod%C3%A8le:Google_Livres
19
 * Le premier paramètre (ou id) est obligatoire.
20
 * Le deuxième (ou titre) est requis si on ne veut pas fabriquer le lien brut (inclusion {{ouvrage}} 'Lire en ligne')
21
 * Class GoogleLivresTemplate.
22
 */
23
class GoogleLivresTemplate extends AbstractWikiTemplate
24
{
25
    use ArrayProcessTrait, GoogleBooksTrait;
26
27
    /**
28
     * todo utile ici ?
29
     */
30
    const DEFAULT_GOOGLEBOOKS_URL = 'https://books.google.com/books';
31
32
    /**
33
     * todo URL avec fin
34
     */
35
    const GOOGLEBOOKS_START_URL_PATTERN = 'https?://(?:books|play)\.google\.[a-z\.]{2,6}/(?:books)?(?:books/[^\?]+\.html)?(?:/reader)?\?(?:[a-zA-Z=&]+&)?id=';
36
37
    const TRACKING_PARAMETERS
38
        = [
39
            'xtor',
40
            'ved',
41
            'ots',
42
            'sig',
43
            'source',
44
            'utm_source',
45
            'utm_medium',
46
            'utm_campaign',
47
            'utm_term',
48
            'utm_content',
49
        ];
50
51
    const WIKITEMPLATE_NAME = 'Google Livres';
52
53
    const REQUIRED_PARAMETERS = ['id'];
54
55
    const MINIMUM_PARAMETERS = ['id' => ''];
56
57
    const PARAM_ALIAS
58
        = [
59
            '1' => 'id',
60
            '2' => 'titre',
61
            'surligné' => 'surligne',
62
            'BuchID' => 'id',
63
        ];
64
65
    /**
66
     * @var array
67
     */
68
    protected $parametersByOrder
69
        = ['id', 'titre', 'couv', 'page', 'romain', 'page autre', 'surligne'];
70
71
    /**
72
     * Create {Google Book} from URL.
73
     * See also https://fr.wikipedia.org/wiki/Utilisateur:Jack_ma/GB
74
     * https://stackoverflow.com/questions/11584551/need-information-on-query-parameters-for-google-books-e-g-difference-between-d.
75
     *
76
     * @param string $url
77
     *
78
     * @return GoogleLivresTemplate|null
79
     * @throws Exception
80
     */
81
    public static function createFromURL(string $url): ?self
82
    {
83
        if (!self::isGoogleBookURL($url)) {
84
            throw new DomainException('not a Google Book URL');
85
        }
86
        $gooDat = self::parseGoogleBookQuery($url);
87
88
        if (empty($gooDat['id'])) {
89
            throw new DomainException("no GoogleBook 'id' in URL");
90
        }
91
        if (!preg_match('#[0-9A-Za-z_\-]{12}#', $gooDat['id'])) {
92
            throw new DomainException("GoogleBook 'id' malformed [0-9A-Za-z_\-]{12}");
93
        }
94
95
        $data = self::mapGooData($gooDat);
96
97
        $templ = new self();
98
        $templ->hydrate($data);
99
100
        return $templ;
101
    }
102
103
    /**
104
     * Mapping Google URL data to {Google Livres} data.
105
     *
106
     * @param array $gooData
107
     *
108
     * @return array
109
     */
110
    private static function mapGooData(array $gooData): array
111
    {
112
        $data = [];
113
        $data['id'] = $gooData['id'];
114
115
        // show cover ?
116
        if (isset($gooData['printsec']) && 'frontcover' === $gooData['printsec']) {
117
            $data['couv'] = '1';
118
        }
119
120
        // page number
121
        if (!empty($gooData['pg'])) {
122
            $data['page autre'] = $gooData['pg'];
123
124
            //  pg=PAx => "page=x"
125
            if (preg_match('/^PA([0-9]+)$/', $gooData['pg'], $matches) > 0) {
126
                $data['page'] = $matches[1];
127
                unset($data['page autre']);
128
            }
129
            //  pg=PRx => "page=x|romain=1"
130
            if (preg_match('/^PR([0-9]+)$/', $gooData['pg'], $matches) > 0) {
131
                $data['page'] = $matches[1];
132
                $data['romain'] = '1';
133
                unset($data['page autre']);
134
            }
135
        }
136
        // q : keywords search / dq : quoted phrase search
137
        // affichage Google : dq ignoré si q existe
138
        if (!empty($gooData['dq']) || !empty($gooData['q'])) {
139
            $data['surligne'] = $gooData['q'] ?? $gooData['dq']; // q prévaut
140
            $data['surligne'] = self::googleUrlEncode($data['surligne']);
141
        }
142
143
        return $data;
144
    }
145
146
    /**
147
     * Instead of url_encode(). No UTF-8 encoding.
148
     *
149
     * @param string $str
150
     *
151
     * @return string
152
     */
153
    public static function googleUrlEncode(string $str): string
154
    {
155
        return str_replace(' ', '+', trim(urldecode($str)));
156
    }
157
158
    /**
159
     * Check if Google URL or wiki {Google Books} template.
160
     *
161
     * @param string $text
162
     *
163
     * @return bool
164
     */
165
    public static function isGoogleBookValue(string $text): bool
166
    {
167
        if (true === self::isGoogleBookURL($text)) {
168
            return true;
169
        }
170
        if (preg_match('#^{{[ \n]*Google (Livres|Books)[^}]+}}$#i', $text) > 0) {
171
            return true;
172
        }
173
174
        return false;
175
    }
176
177
    /**
178
     * Serialize the wiki-template.
179
     * Improvement : force param order : id/titre/...
180
     *
181
     * @param bool|null $cleanOrder
182
     *
183
     * @return string
184
     */
185
    public function serialize(?bool $cleanOrder = true): string
186
    {
187
        $text = parent::serialize();
188
189
        // Documentation suggère non affichage de ces 2 paramètres
190
        return str_replace(['id=', 'titre='], '', $text);
191
    }
192
}
193