Completed
Push — master ( e1eb62...af5e02 )
by Patrick
02:15
created

InstrumentBase::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace ExtendedStrings\Strings\Instrument;
6
7
use ExtendedStrings\Strings\Note;
8
9
abstract class InstrumentBase implements InstrumentInterface
10
{
11
    protected $defaultStopLength;
12
13
    private $stringFrequencies = [];
14
    private $stringLength;
15
16
    /**
17
     * Violin constructor.
18
     *
19
     * @param array|null $stringFrequencies
20
     * @param float      $stringLength
21
     */
22
    public function __construct(array $stringFrequencies = null, float $stringLength = null)
23
    {
24
        if (null === $stringFrequencies) {
25
            $stringFrequencies = array_map(function ($name) {
26
                return Note::fromName($name)->getFrequency();
27
            }, $this->getDefaultNames());
28
        }
29
        if (null === $stringLength) {
30
            $stringLength = $this->getDefaultStringLength();
31
        }
32
33
        $this->stringFrequencies = $stringFrequencies;
34
        $this->stringLength = $stringLength;
35
    }
36
37
    /**
38
     * @return string[] The default strings of the instrument, as note names
39
     *                  (e.g. 'A4', 'D3').
40
     */
41
    abstract protected function getDefaultNames(): array;
42
43
    /**
44
     * @return float The default vibrating string length of the instrument (mm).
45
     */
46
    abstract protected function getDefaultStringLength(): float;
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getStringFrequencies(): array
52
    {
53
        return $this->stringFrequencies;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getStringLength(): float
60
    {
61
        return $this->stringLength;
62
    }
63
}
64