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

CirrusSearch   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUrl() 0 3 1
A __construct() 0 3 1
A getPageTitles() 0 22 5
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