DriveNow   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 90
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A refreshCity() 0 5 1
A getData() 0 4 1
A getCars() 0 8 2
A countCars() 0 4 1
A getCities() 0 11 2
1
<?php
2
namespace DriveNow;
3
4
/**
5
 * Class DriveNow
6
 * @package DriveNow
7
 */
8
class DriveNow
9
{
10
    const CITY_MUNICH = 4604;
11
    const CITY_BERLIN = 6099;
12
    const CITY_DUSSELDORF = 1293;
13
    const CITY_COLOGNE = 1774;
14
    const CITY_HAMBURG = 40065;
15
    const CITY_VIENNA = 40468;
16
    const CITY_LONDON = 40758;
17
    const CITY_COPENHAGEN = 41369;
18
    const CITY_STOCKHOLM = 42128;
19
    const CITY_BRUSSELS = 42619;
20
    const CITY_MILANO = 42756;
21
22
    private $city_id;
23
    private $api_key;
24
    private $cache;
25
    private $city_cache;
26
27
    /**
28
     * DriveNow constructor.
29
     * DriveNow constructor.
30
     * @param string $api_key
31
     * @param int|null $city
32
     */
33
    public function __construct(string $api_key, integer $city = null)
34
    {
35
        $this->api_key = $api_key;
36
        //Default city is Copenhagen
37
        $this->city_id = $city ?: self::CITY_COPENHAGEN;
38
        $this->refreshCity();
39
    }
40
41
    /**
42
     * Refresh city cache
43
     */
44
    public function refreshCity()
45
    {
46
        $output = Client::request('https://api2.drive-now.com/cities/' . $this->city_id . '?expand=full', $this->api_key);
47
        $this->cache = json_decode($output, true);
48
    }
49
50
    /**
51
     * Handle to get the raw data
52
     * @return mixed
53
     */
54
    public function getData()
55
    {
56
        return $this->cache;
57
    }
58
59
    /**
60
     * Get array with cars
61
     * @return array
62
     */
63
    public function getCars()
64
    {
65
        $cars = [];
66
        foreach ($this->cache['cars']['items'] as $item) {
67
            $cars[$item['id']] = new Car($item);
68
        }
69
        return $cars;
70
    }
71
72
    /**
73
     * Get the amount of cars in the given city
74
     * @return integer
75
     */
76
    public function countCars()
77
    {
78
        return $this->cache['cars']['count'];
79
    }
80
81
    /**
82
     * Recieve cities supported by Drive Now
83
     * @return array
84
     */
85
    public function getCities()
86
    {
87
        $output = Client::request('https://api2.drive-now.com/cities?expand=cities', $this->api_key);
88
        $this->city_cache = json_decode($output, true);
89
90
        $cities = [];
91
        foreach ($this->city_cache['items'] as $item) {
92
            $cities[] = new City($item);
93
        }
94
        return $cities;
95
    }
96
97
}