ParkingRepository   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 69
c 0
b 0
f 0
wmc 14
lcom 1
cbo 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 5 2
A getSlotsForCarsWithColour() 0 9 2
A getPlateForCarsWithColour() 0 10 2
A getSlotKeysForCarsWithColour() 0 12 2
A getSlotKeyForCarWithPlate() 0 11 4
A store() 0 4 1
1
<?php
2
namespace Parking\Infrastructure\Repository;
3
4
class ParkingRepository implements \Parking\Domain\Repository\ParkingRepository
5
{
6
7
    private $path;
8
9
    public function __construct($path)
10
    {
11
        $this->path = $path;
12
    }
13
14
    public function get()
15
    {
16
        $data = file_get_contents($this->path);
17
        return $data ? unserialize($data) : new \Parking\Domain\Parking();
18
    }
19
20
    public function getSlotsForCarsWithColour($colour)
21
    {
22
        $parking = $this->get();
23
        $slots = array_filter($parking->getSlots(), function($slot) use ($colour) {
24
            return $slot->getCar() && $slot->getCar()->colour == $colour;
25
        });
26
27
        return $slots;
28
    }
29
30
    public function getPlateForCarsWithColour($colour)
31
    {
32
        $slots = $this->getSlotsForCarsWithColour($colour);
33
        $results = [];
34
        foreach ($slots as $slot) {
35
            $results[] = $slot->getCar()->plate;
36
        }
37
38
        return $results;
39
    }
40
41
    public function getSlotKeysForCarsWithColour($colour)
42
    {
43
        $parking = $this->get();
44
        $slots = $this->getSlotsForCarsWithColour($colour);
45
46
        $results = [];
47
        foreach ($slots as $slot) {
48
            $results[] = array_search($slot, $parking->getSlots()) + 1;
49
        }
50
51
        return $results;
52
    }
53
54
    public function getSlotKeyForCarWithPlate($plate)
55
    {
56
        $parking = $this->get();
57
        foreach ($parking->getSlots() as $index => $slot) {
58
            if ($slot->getCar() && $slot->getCar()->plate == $plate) {
59
                return $index + 1;
60
            }
61
        }
62
63
        return false;
64
    }
65
66
    public function store(\Parking\Domain\Parking $parking)
67
    {
68
        return file_put_contents($this->path, serialize($parking));
69
    }
70
71
72
}