Passed
Push — master ( af0694...e67862 )
by Daan
01:28
created

Present::getHeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DaanMooij\AdventOfCode\Year2015\Day2;
4
5
use Exception;
6
7
class Present
8
{
9
    private int $height;
10
    private int $width;
11
    private int $length;
12
13
    public function __construct(int $height, int $width, int $length)
14
    {
15
        $this->height = $height;
16
        $this->width = $width;
17
        $this->length = $length;
18
    }
19
20
    public static function fromString(string $dimensions): Present
21
    {
22
        list($height, $width, $length) = explode('x', $dimensions);
23
24
        return new Present(intval($height), intval($width), intval($length));
25
    }
26
27
    /** @return array<int> */
28
    public function getSortedDimensions(): array
29
    {
30
        $dimensions = [$this->height, $this->width, $this->length];
31
        sort($dimensions);
32
33
        return $dimensions;
34
    }
35
36
    public function getVolume(): int
37
    {
38
        return $this->height * $this->width * $this->length;
39
    }
40
41
    public function getSmallestSide(): int
42
    {
43
        $sortedDimensions = $this->getSortedDimensions();
44
        $smallestSide = reset($sortedDimensions);
45
        if (!is_int($smallestSide)) {
46
            throw new Exception('Could not get smallest side');
47
        }
48
49
        return $smallestSide;
50
    }
51
52
    public function getSecondSmallestSide(): int
53
    {
54
        $dimensions = $this->getSortedDimensions();
55
        array_shift($dimensions);
56
        $secondSmallestSide = reset($dimensions);
57
        if (!is_int($secondSmallestSide)) {
58
            throw new Exception('Could not get second smallest side');
59
        }
60
61
        return $secondSmallestSide;
62
    }
63
64
    public function getSurfaceArea(): int
65
    {
66
        $surfaceArea = (2 * $this->length * $this->width)
67
            + (2 * $this->width * $this->height)
68
            + (2 * $this->height * $this->length);
69
70
        return $surfaceArea;
71
    }
72
73
    public function getSmallestSideArea(): int
74
    {
75
        $dimensions = $this->getSortedDimensions();
76
        $smallestSide = array_shift($dimensions);
77
        $secondSmallestSide = array_shift($dimensions);
78
79
        return $smallestSide * $secondSmallestSide;
80
    }
81
82
    public function getHeight(): int
83
    {
84
        return $this->height;
85
    }
86
87
    public function getWidth(): int
88
    {
89
        return $this->width;
90
    }
91
92
    public function getLength(): int
93
    {
94
        return $this->length;
95
    }
96
}
97