Car::isOn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tleckie\DesignPatterns\State;
4
5
/**
6
 * Class Car
7
 *
8
 * @package Tleckie\DesignPatterns\State
9
 * @author  Teodoro Leckie Westberg <[email protected]>
10
 */
11
class Car
12
{
13
    /** @var CarState */
14
    private CarState $state;
15
16
    /**
17
     * Car constructor.
18
     *
19
     * @param CarState $state
20
     */
21
    public function __construct(CarState $state)
22
    {
23
        $this->changeState($state);
24
    }
25
26
    /**
27
     * @param CarState $state
28
     */
29
    private function changeState(CarState $state): void
30
    {
31
        $this->state = $state;
32
    }
33
34
    /**
35
     * @return $this
36
     */
37
    public function brake(): Car
38
    {
39
        $this->changeState($this->state->brake());
40
41
        return $this;
42
    }
43
44
    /**
45
     * @return $this
46
     */
47
    public function speedUp(): Car
48
    {
49
        $this->changeState($this->state->speedUp());
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return $this
56
     */
57
    public function turnOff(): Car
58
    {
59
        $this->changeState($this->state->turnOff());
60
61
        return $this;
62
    }
63
64
    /**
65
     * @return $this
66
     */
67
    public function turnOn(): Car
68
    {
69
        $this->changeState($this->state->turnOn());
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return bool
76
     */
77
    public function isOn(): bool
78
    {
79
        return !$this->state instanceof OffState;
80
    }
81
}
82