ParkingRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 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
}