Completed
Push — master ( 726390...169d80 )
by Andy
01:51
created

EveryPolitician::getCountriesJsonData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 0
crap 3
1
<?php
2
3
namespace EveryPolitician\EveryPolitician;
4
5
use \GuzzleHttp;
6
7
class EveryPolitician
8
{
9
    private $countriesJsonFilename;
10
    private $countriesJsonUrl;
11
    private $countriesJsonData;
12
13
    const DEFAULT_COUNTRIES_JSON_URL = 'https://raw.githubusercontent.com/'
14
        .'everypolitician/everypolitician-data/master/countries.json';
15
16 60
    public function __construct($options = [])
17
    {
18 60
        if (!array_key_exists('filename', $options)) {
19 54
            if (array_key_exists('url', $options)) {
20 3
                $this->countriesJsonUrl = $options['url'];
21 2
            } else {
22 52
                $this->countriesJsonUrl = self::DEFAULT_COUNTRIES_JSON_URL;
23
            }
24 36
        } else {
25 6
            $this->countriesJsonFilename = $options['filename'];
26
        }
27 60
    }
28
29 9
    public function __toString()
30
    {
31 9
        $body = !is_null($this->countriesJsonUrl) ? $this->countriesJsonUrl : $this->countriesJsonFilename;
32 9
        return "<EveryPolitician: $body>";
33
    }
34
35
    /**
36
     * Construct from filename
37
     *
38
     * @param string $filename name of Popolo json file
39
     *
40
     * @return $this
41
     */
42 6
    public static function fromFilename($filename)
43
    {
44 6
        return new self(['filename' => $filename]);
45
    }
46
47
    /**
48
     * Construct from URL
49
     *
50
     * @param string $url location of Popolo json file
51
     *
52
     * @return $this
53
     */
54 3
    public static function fromUrl($url)
55
    {
56 3
        return new self(['url' => $url]);
57
    }
58
59 51
    private function countriesJsonData()
60
    {
61 51
        if (!is_null($this->countriesJsonData)) {
62 3
            return $this->countriesJsonData;
63
        }
64 51
        if (!is_null($this->countriesJsonFilename)) {
65 3
            $f = file_get_contents($this->countriesJsonFilename);
66 3
            $this->countriesJsonData = json_decode($f, true);
67 2
        } else {
68 48
            $client = new GuzzleHttp\Client();
69 48
            $response = $client->get($this->countriesJsonUrl);
70 48
            $this->countriesJsonData = json_decode($response->getBody(), true);
71
        }
72 51
        return $this->countriesJsonData;
73
    }
74
75
    /**
76
     * Return a list of all known countries
77
     */
78 51
    public function countries()
79
    {
80 51
        $countries = [];
81 51
        $countriesData = $this->countriesJsonData();
82 51
        foreach ($countriesData as $countryData) {
83 51
            $countries[] = new Country($countryData);
84 34
        }
85 51
        return $countries;
86
    }
87
88
    /**
89
     * Return a Country object from a country slug
90
     */
91 42
    public function country($countrySlug)
92
    {
93 42
        foreach ($this->countries() as $c) {
94 42
            if ($c->slug == $countrySlug) {
95 36
                return $c;
96
            }
97 24
        }
98 9
        throw new Exceptions\NotFoundException("Couldn't find the country with slug '$countrySlug'");
99
    }
100
101
    /**
102
     * Return an array of Country and Legislature objects from their slugs
103
     */
104 12
    public function countryLegislature($countrySlug, $legislatureSlug)
105
    {
106 12
        $country = $this->country($countrySlug);
107 6
        $legislature = $country->legislature($legislatureSlug);
108 3
        return [$country, $legislature];
109
    }
110
}
111