Completed
Push — master ( 7b220e...d0f73c )
by Patrick
08:53
created

Instrument::fromNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace ExtendedStrings\Strings;
6
7
class Instrument implements InstrumentInterface
8
{
9
    private $strings;
10
11
    /**
12
     * Instrument constructor.
13
     *
14
     * @param InstrumentStringInterface[] $strings
15
     */
16
    public function __construct(array $strings)
17
    {
18
        $this->strings = $strings;
19
    }
20
21
    /**
22
     * @param string[] $stringNames
23
     * @param float    $length
24
     *
25
     * @return self
26
     */
27
    public static function fromNames(array $stringNames, float $length = 500.0): self
28
    {
29
        $strings = [];
30
        $number = 1;
31
        foreach ($stringNames as $name) {
32
            $frequency = Note::fromName($name)->getFrequency();
33
            $strings[] = new InstrumentString($frequency, $length, $number++);
34
        }
35
36
        return new self($strings);
37
    }
38
39
    /**
40
     * @param string $preset
41
     *
42
     * @return self
43
     */
44
    public static function fromPreset(string $preset): self
45
    {
46
        switch ($preset) {
47
            case 'violin':
48
                $names = ['E5', 'A4', 'D4', 'G3'];
49
                $length = 325;
50
                break;
51
52
            case 'viola':
53
                $names = ['A4', 'D3', 'G3', 'C3'];
54
                $length = 410;
55
                break;
56
57
            case 'cello':
58
                $names = ['A3', 'D2', 'G2', 'C2'];
59
                $length = 690;
60
                break;
61
62
            case 'guitar':
63
                $names = ['E4', 'B3', 'G3', 'D3', 'A2', 'E2'];
64
                $length = 650;
65
                break;
66
67
            case 'double bass':
68
                $names = ['E1', 'A1', 'D2', 'G2'];
69
                $length = 1140;
70
                break;
71
72
            default:
73
                throw new \InvalidArgumentException(sprintf('Preset not found: %s', $preset));
74
        }
75
76
        return self::fromNames($names, $length);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getStrings(): array
83
    {
84
        return $this->strings;
85
    }
86
}
87