Parking::leave()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
namespace Parking\Domain;
3
/**
4
 * Class Parking
5
 * @package Parking\Domain
6
 */
7
class Parking
8
{
9
10
    /**
11
     * @var array
12
     */
13
    private $slots = [];
14
15
    /**
16
     * Parking constructor.
17
     * @param array $slots
18
     */
19
    public function __construct($slots = array())
20
    {
21
        $this->slots = $slots;
22
    }
23
24
    /**
25
     * Park method
26
     *
27
     * @param Car $car
28
     * @return bool|int
29
     */
30
    public function park(Car $car)
31
    {
32
        foreach ($this->slots as $index => &$slot) {
33
            if ($slot->available()) {
34
                $slot->addCar($car);
35
                return $index + 1;
36
            }
37
        }
38
39
        return false;
40
    }
41
42
    /**
43
     * Leave method
44
     *
45
     * @param $key
46
     * @return bool
47
     */
48
    public function leave($key)
49
    {
50
        $slot = $this->getSlot($key);
51
        if ($slot) {
52
            $slot->leave();
53
            return true;
54
        }
55
56
        return false;
57
    }
58
59
    /**
60
     * Getter of slot
61
     *
62
     * @param $key
63
     * @return bool|mixed
64
     */
65
    public function getSlot($key)
66
    {
67
        if (!isset ($this->slots[$key])) {
68
            return false;
69
        }
70
71
        return $this->slots[$key];
72
    }
73
74
    /**
75
     * Getter of slots
76
     *
77
     * @return array
78
     */
79
    public function getSlots()
80
    {
81
        return $this->slots;
82
    }
83
84
85
}