Completed
Push — master ( 658806...ce8777 )
by Andy
07:01
created

LegislativePeriod::getCsvUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace EveryPolitician\EveryPolitician;
4
5
use \GuzzleHttp;
6
use \League\Csv;
7
8
class LegislativePeriod
9
{
10
    public $id;
11
    public $name;
12
    public $slug;
13
    protected $legislature;
14
    protected $country;
15
    protected $legislativePeriodData;
16
17 8
    public function __construct($legislativePeriodData, $legislature, $country)
18
    {
19 8
        $properties = ['id', 'name', 'slug'];
20 8
        foreach ($properties as $k) {
21 8
            $this->$k = $legislativePeriodData[$k];
22 5
        }
23 8
        $this->legislature = $legislature;
24 8
        $this->country = $country;
25 8
        $this->legislativePeriodData = $legislativePeriodData;
26 8
    }
27
28 8
    public function __get($prop)
29
    {
30 8
        if (in_array($prop, ['startDate', 'endDate', 'csvUrl'])) {
31 8
            $getter = 'get' . ucfirst($prop);
32 8
            return $this->$getter();
33
        }
34
        trigger_error('Undefined property: '.__CLASS__.'::$'.$prop, E_USER_ERROR);
35
    }
36
37
    /**
38
     * Return the start date of the legislative period
39
     *
40
     * If this is unknown, it returns null.
41
     */
42 3
    private function getStartDate()
43
    {
44 3
        if (array_key_exists('start_date', $this->legislativePeriodData)) {
45 3
            return $this->legislativePeriodData['start_date'];
46
        }
47
        return null;
48
    }
49
50
    /**
51
     * Return the end date of the legislative period
52
     *
53
     * If this is unknown, it returns null.
54
     */
55 3
    private function getEndDate()
56
    {
57 3
        if (array_key_exists('end_date', $this->legislativePeriodData)) {
58
            return $this->legislativePeriodData['end_date'];
59
        }
60 3
        return null;
61
    }
62
63
    /**
64
     * Return the URL to CSV of members during this legislative period
65
     */
66 2
    private function getCsvUrl()
67
    {
68
        return 'https://raw.githubusercontent.com/everypolitician'
69 2
            .'/everypolitician-data/'.$this->legislature->sha
70 2
            .'/'.$this->legislativePeriodData['csv'];
71
    }
72
73
    /**
74
     * Return parsed data from the CSV of members during the period
75
     *
76
     * This returns a list of one dict per row of the CSV file, where
77
     * the keys are the column headers.
78
     */
79 2
    public function csv()
80
    {
81 2
        $client = new GuzzleHttp\Client();
82 2
        $response = $client->get($this->csvUrl);
0 ignored issues
show
Documentation introduced by
The property csvUrl does not exist on object<EveryPolitician\E...cian\LegislativePeriod>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
83 2
        $reader = Csv\Reader::createFromString($response->getBody());
84 2
        return iterator_to_array($reader->fetchAssoc(), false);
85
    }
86
}
87