Instrument   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 2
dl 0
loc 79
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromNames() 0 10 2
B fromPreset() 0 34 6
A getStrings() 0 4 1
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 VibratingStringInterface[] $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
        foreach ($stringNames as $name) {
31
            $frequency = Note::fromName($name)->getFrequency();
32
            $strings[] = new VibratingString($frequency, $length);
33
        }
34
35
        return new self($strings);
36
    }
37
38
    /**
39
     * @param string $preset
40
     *
41
     * @return self
42
     */
43
    public static function fromPreset(string $preset): self
44
    {
45
        switch ($preset) {
46
            case 'violin':
47
                $names = ['E5', 'A4', 'D4', 'G3'];
48
                $length = 325;
49
                break;
50
51
            case 'viola':
52
                $names = ['A4', 'D4', 'G3', 'C3'];
53
                $length = 410;
54
                break;
55
56
            case 'cello':
57
                $names = ['A3', 'D3', 'G2', 'C2'];
58
                $length = 690;
59
                break;
60
61
            case 'guitar':
62
                $names = ['E4', 'B3', 'G3', 'D3', 'A2', 'E2'];
63
                $length = 650;
64
                break;
65
66
            case 'double bass':
67
                $names = ['G2', 'D2', 'A1', 'E1'];
68
                $length = 1140;
69
                break;
70
71
            default:
72
                throw new \InvalidArgumentException(sprintf('Preset not found: %s', $preset));
73
        }
74
75
        return self::fromNames($names, $length);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getStrings(): array
82
    {
83
        return $this->strings;
84
    }
85
}
86