Passed
Push — master ( b52452...60ff6d )
by Luca
04:00 queued 01:57
created

Giffhanger::generate()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 23
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 35
rs 8.0555
1
<?php
2
3
namespace Jackal\Giffhanger\Giffhanger;
4
5
use Jackal\Giffhanger\Configuration\Configuration;
6
use Jackal\Giffhanger\Exception\GiffhangerException;
7
use Jackal\Giffhanger\Generator\GifGenerator;
8
use Jackal\Giffhanger\Generator\VideoH264Generator;
9
use Jackal\Giffhanger\Generator\VideoOggGenerator;
10
use Jackal\Giffhanger\Generator\VideoWebMGenerator;
11
12
class Giffhanger
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $videoFile;
18
19
    /**
20
     * @var Configuration
21
     */
22
    protected $config;
23
24
    /**
25
     * Giffhanger constructor.
26
     * @param string $sourceVideoFile
27
     * @param array $config
28
     * @throws \Jackal\Giffhanger\Exception\GiffhangerConfigurationException
29
     */
30
    public function __construct($sourceVideoFile, $config = [])
31
    {
32
        $this->videoFile = $sourceVideoFile;
33
34
        $this->config = new Configuration($config);
35
    }
36
37
    /**
38
     * @return Configuration
39
     */
40
    public function getConfig() : Configuration
41
    {
42
        return $this->config;
43
    }
44
45
    /**
46
     * @param string $destinationFile
47
     * @throws GiffhangerException
48
     */
49
    public function generate($destinationFile) : void
50
    {
51
        if (!is_file($this->videoFile) or !is_readable($this->videoFile)) {
52
            throw GiffhangerException::inputFileNotFoundOrNotReadable($this->videoFile);
53
        }
54
55
        $mimeType = mime_content_type($this->videoFile);
56
        if (strpos($mimeType, 'video/') === false) {
57
            throw GiffhangerException::inputFileIsNotVideo($this->videoFile, $mimeType);
58
        }
59
60
        $ext = strtolower(pathinfo($destinationFile, PATHINFO_EXTENSION));
61
        switch ($ext) {
62
            case 'gif':
63
                $generator = new GifGenerator($this->videoFile, $destinationFile, $this->config);
64
65
                break;
66
            case 'avi':
67
            case 'mp4':
68
                $generator = new VideoH264Generator($this->videoFile, $destinationFile, $this->config);
69
70
                break;
71
            case 'webm':
72
                $generator = new VideoWebMGenerator($this->videoFile, $destinationFile, $this->config);
73
74
                break;
75
            case 'ogg':
76
                $generator = new VideoOggGenerator($this->videoFile, $destinationFile, $this->config);
77
78
                break;
79
            default:
80
                throw GiffhangerException::invalidExtension($ext);
81
        }
82
83
        $generator->generate();
84
    }
85
}
86