Temperature   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 62
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A celsius() 0 3 1
A getScale() 0 3 1
A __toString() 0 12 2
A getDegrees() 0 7 2
A fahrenheit() 0 3 1
A roundCelsius() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\NestApi\Client\Device\Thermostat;
6
7
use LauLamanApps\NestApi\Client\Device\Thermostat\Temperature\Scale;
8
9
final class Temperature
10
{
11
    /**
12
     * @var Scale
13
     */
14
    private $scale;
15
16
    /**
17
     * @var float
18
     */
19
    private $degrees;
20
21 64
    public function __construct(Scale $scale, float $degrees)
22
    {
23 64
        $this->scale = $scale;
24 64
        $this->degrees = $degrees;
25 64
    }
26
27 50
    public static function celsius(float $degrees): self
28
    {
29 50
        return new self(Scale::celsius(), self::roundCelsius($degrees));
30
    }
31
32 12
    public static function fahrenheit(int $degrees): self
33
    {
34 12
        return new self(Scale::fahrenheit(), (float)$degrees);
35
    }
36
37 51
    public function getScale(): Scale
38
    {
39 51
        return $this->scale;
40
    }
41
42
    /**
43
     * @return float|int
44
     */
45 69
    public function getDegrees()
46
    {
47 69
        if ($this->scale->isCelsius()) {
48 39
            return self::roundCelsius($this->degrees);
49
        }
50
51 30
        return (int)$this->degrees;
52
    }
53
54 62
    private static function roundCelsius(float $degrees): float
55
    {
56 62
        return round($degrees * 2) / 2;
57
    }
58
59 18
    public function __toString(): string
60
    {
61 18
        if ($this->scale->isCelsius()) {
62 11
            return sprintf(
63 11
                '%.1f°C',
64 11
                $this->getDegrees()
65
            );
66
        }
67
68 7
        return sprintf(
69 7
            '%d°F',
70 7
            $this->getDegrees()
71
        );
72
    }
73
}
74