Configuration::assertPositiveIntegerNumber()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 3
eloc 2
c 2
b 1
f 1
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Jackal\Giffhanger\Configuration;
4
5
use Jackal\Giffhanger\Exception\GiffhangerConfigurationException;
6
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
class Configuration
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $configuration = [];
15
16
    /**
17
     * Configuration constructor.
18
     * @param array $configuration
19
     * @throws GiffhangerConfigurationException
20
     */
21
    public function __construct($configuration = [])
22
    {
23
        $resolver = new OptionsResolver();
24
        $resolver->setDefaults([
25
            'temp_dir' => sys_get_temp_dir(),
26
            'resize_width' => 640,
27
            'crop_ratio' => null,
28
            'frames' => 3,
29
            'duration' => 6,
30
            'bitrate' => 600,
31
            'frame_rate' => 10,
32
            'ffmpeg.binaries' => '/usr/bin/ffmpeg',
33
            'ffprobe.binaries' => '/usr/bin/ffprobe',
34
        ]);
35
36
        try {
37
            $this->configuration = $resolver->resolve($configuration);
38
39
            $this->assertPositiveIntegerNumber($this->getDimensionWidth());
40
            $this->assertPositiveIntegerNumber($this->getFrameRate());
41
            $this->assertPositiveIntegerNumber($this->getNumberOfFrames());
42
            $this->assertPositiveIntegerNumber($this->getOutputDuration());
43
            $this->assertPositiveIntegerNumber($this->getVideoBitrate());
44
        } catch (UndefinedOptionsException $e) {
45
            throw GiffhangerConfigurationException::invalidOption($e->getMessage());
46
        }
47
    }
48
49
    protected function assertPositiveIntegerNumber($value) : void
50
    {
51
        if (!is_numeric($value) or $value < 0) {
52
            throw GiffhangerConfigurationException::invalidPositiveIntegerValue($value);
53
        }
54
    }
55
56
    public function getTempFolder() : string
57
    {
58
        return $this->configuration['temp_dir'];
59
    }
60
61
    public function getDimensionWidth()
62
    {
63
        return $this->configuration['resize_width'];
64
    }
65
66
    public function getNumberOfFrames() : int
67
    {
68
        return (int) $this->configuration['frames'];
69
    }
70
71
    public function getOutputDuration() : string
72
    {
73
        return $this->configuration['duration'];
74
    }
75
76
    public function getVideoBitrate() : string
77
    {
78
        return $this->configuration['bitrate'];
79
    }
80
81
    public function getCropRatio()
82
    {
83
        return $this->configuration['crop_ratio'];
84
    }
85
86
    public function getFrameRate() : int
87
    {
88
        return (int) $this->configuration['frame_rate'];
89
    }
90
91
    public function getFFMpegBinaries() : string
92
    {
93
        return $this->configuration['ffmpeg.binaries'];
94
    }
95
96
    public function getFFProbeBinaries() : string
97
    {
98
        return $this->configuration['ffprobe.binaries'];
99
    }
100
}
101