Passed
Push — dev ( 4ef148...eba0dc )
by Dispositif
07:17
created

CirrusSearch::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
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
    /**
24
     * @var string
25
     */
26
    private $url;
27
28
    /**
29
     * CirrusSearch constructor.
30
     *
31
     * @param string|null $url
32
     */
33
    public function __construct(?string $url = null)
34
    {
35
        $this->url = $url;
36
    }
37
38
    public function setUrl(string $url)
39
    {
40
        $this->url = $url;
41
    }
42
43
    /**
44
     * TODO: use Wiki API library or Guzzle
45
     *
46
     * @return array
47
     * @throws ConfigException
48
     */
49
    public function getPageTitles(): array
50
    {
51
        if (!$this->url) {
52
            throw new ConfigException('CirrusSearch null URL');
53
        }
54
55
        $json = file_get_contents($this->url);
56
        if (false === $json) {
57
            return [];
58
        }
59
60
        $myArray = json_decode($json, true);
61
        $result = $myArray['query']['search'];
62
        if (empty($result)) {
63
            return [];
64
        }
65
66
        foreach ($result as $res) {
67
            $titles[] = trim($res['title']);
68
        }
69
70
        return $titles;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $titles seems to be defined by a foreach iteration on line 66. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
71
    }
72
}
73