Completed
Push — master ( 3dee16...2bf36e )
by Andy
02:57
created

Popolo   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 91.3%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 65
c 2
b 0
f 0
ccs 21
cts 23
cp 0.913
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __get() 0 11 2
A fromFilename() 0 7 1
A fromUrl() 0 8 2
1
<?php
2
3
namespace EveryPolitician\EveryPoliticianPopolo;
4
5
class Popolo
6
{
7
    use Traits\ArrayGetterTrait;
8
9
    private $jsonData;
10
    private $collections = [
11
        'persons'       => ['Person', 'PersonCollection'],
12
        'organizations' => ['Organization', 'OrganizationCollection'],
13
        'memberships'   => ['Membership', 'MembershipCollection'],
14
        'areas'         => ['Area', 'AreaCollection'],
15
        'posts'         => ['Post', 'PostCollection'],
16
        'events'        => ['Event', 'EventCollection'],
17
    ];
18
19
    /**
20
     * Create a new Popolo Instance from
21
     */
22 282
    public function __construct($jsonData)
23
    {
24 282
        $this->jsonData = $jsonData;
25 282
    }
26
27 279
    public function __get($prop)
28
    {
29 279
        if (array_key_exists($prop, $this->collections)) {
30 279
            $c = $this->collections[$prop];
31 279
            $objectClass = 'EveryPolitician\\EveryPoliticianPopolo\\Objects\\'.$c[0];
32 279
            $collectionClass = 'EveryPolitician\\EveryPoliticianPopolo\\Collections\\'.$c[1];
33 279
            $dataArr = $this->arrGet($this->jsonData, $prop, []);
34 279
            return new $collectionClass($dataArr, $objectClass, $this);
35
        }
36
        trigger_error('Undefined property: '.__CLASS__.'::$'.$prop, E_USER_ERROR);
37
    }
38
39
    /**
40
     * Construct from filename
41
     *
42
     * @param string $filename name of Popolo json file
43
     *
44
     * @return $this
45
     */
46 240
    public static function fromFilename($filename)
47
    {
48 240
        $contents = file_get_contents($filename);
49 237
        $jsonData = json_decode($contents, true);
50 237
        $instance = new self($jsonData);
51 237
        return $instance;
52
    }
53
54
    /**
55
     * Construct from URL
56
     *
57
     * @param string $url location of Popolo json file
58
     *
59
     * @return $this
60
     */
61 3
    public static function fromUrl($url, $client = null)
62
    {
63 3
        $client = $client ?: new \GuzzleHttp\Client();
64 3
        $response = $client->get($url);
65 3
        $jsonData = json_decode($response->getBody()->getContents(), true);
66 3
        $instance = new self($jsonData);
67 3
        return $instance;
68
    }
69
}
70