Passed
Push — master ( 9ad278...1bcf8b )
by Dispositif
08:55
created

CirrusSearch::httpRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019/2020 © 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
11
namespace App\Infrastructure;
12
13
use App\Domain\Exceptions\ConfigException;
14
15
/**
16
 * Dirty.
17
 * Class CirrusSearch
18
 *
19
 * @package App\Infrastructure
20
 */
21
class CirrusSearch implements PageListInterface
22
{
23
    const BASE_URL = 'https://fr.wikipedia.org/w/api.php';
24
25
    private $params;
26
    private $options;
27
    private $defaultParams
28
        = [
29
            'action' => 'query',
30
            'list' => 'search',
31
            'formatversion' => '2',
32
            'format' => 'json',
33
            'srnamespace' => '0',
34
            'srlimit' => '100',
35
        ];
36
37
    public function __construct(array $params, ?array $options = [])
38
    {
39
        $this->params = $params;
40
        $this->options = $options;
41
    }
42
43
    /**
44
     * todo move to ApiSearch
45
     *
46
     * @return array
47
     * @throws ConfigException
48
     */
49
    public function getPageTitles(): array
50
    {
51
        $arrayResp = $this->httpRequest();
52
53
        if (!isset($arrayResp['query']) || empty($arrayResp['query']['search'])) {
54
            return [];
55
        }
56
        $results = $arrayResp['query']['search'];
57
58
        $titles = [];
59
        foreach ($results as $res) {
60
            if (!empty($res['title'])) {
61
                $titles[] = trim($res['title']); // trim utile ?
62
            }
63
        }
64
65
        if (isset($this->options['reverse']) && $this->options['reverse'] === true) {
66
            krsort($titles);
67
        }
68
69
        return $titles;
70
    }
71
72
    /**
73
     * @param array|null $options
74
     */
75
    public function setOptions(?array $options): void
76
    {
77
        $this->options = $options;
78
    }
79
80
    private function getURL(): string
81
    {
82
        if (empty($this->params['srsearch'])) {
83
            throw new \InvalidArgumentException('No "srsearch" argument in params.');
84
        }
85
86
        $allParams = array_merge($this->defaultParams, $this->params);
87
        // RFC3986 : space => %20
88
        $query = http_build_query($allParams, 'bla', '&', PHP_QUERY_RFC3986);
89
90
        return self::BASE_URL.'?'.$query;
91
    }
92
93
    /**
94
     * todo Guzzle or Wiki API
95
     *
96
     * @return array
97
     * @throws ConfigException
98
     */
99
    private function httpRequest(): array
100
    {
101
        if (!$this->getURL()) {
102
            throw new ConfigException('CirrusSearch null URL');
103
        }
104
105
        $json = file_get_contents($this->getURL());
106
        if (false === $json) {
107
            return [];
108
        }
109
110
        return json_decode($json, true);
111
    }
112
}
113