Completed
Push — master ( f8ff86...ec22b1 )
by Henry
03:57
created

Audio   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createPreview() 0 28 6
A __classLoaded() 0 7 1
A getBlankPath() 0 4 2
A getValue() 0 24 6
A getImage() 0 7 2
1
<?php
2
namespace Divergence\Models\Media;
3
4
use Exception;
5
6
class Audio extends Media
7
{
8
    // configurables
9
    public static $previewExtractCommand = 'ffmpeg -i %1$s -ss %3$u -t %4$u -f mp3 -y %2$s'; // 1=input file, 2=output file, 3=start time, 4=duration
10
    public static $previewDuration = 30;
11
    public static $iconPath = '/site-root/img/icons/filetypes/mp3.png';
12
13
14
    // magic methods
15
    public static function __classLoaded()
16
    {
17
        $className = get_called_class();
18
19
        Media::$mimeHandlers['audio/mpeg'] = $className;
20
21
        parent::__classLoaded();
0 ignored issues
show
introduced by
The method __classLoaded() does not exist on Divergence\Models\Media\Media. Maybe you want to declare this class abstract? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        parent::/** @scrutinizer ignore-call */ 
22
                __classLoaded();
Loading history...
22
    }
23
24
25
    public function getValue($name)
26
    {
27
        switch ($name) {
28
            case 'ThumbnailMIMEType':
29
                return 'image/png';
30
31
            case 'Width':
32
                return 128;
33
34
            case 'Height':
35
                return 128;
36
37
            case 'Extension':
38
39
                switch ($this->MIMEType) {
40
                    case 'audio/mpeg':
41
                        return 'mp3';
42
                    default:
43
                        throw new Exception('Unable to find audio extension for mime-type: '.$this->MIMEType);
44
                }
45
46
                // no break
47
            default:
48
                return parent::getValue($name);
49
        }
50
    }
51
52
53
    // public methods
54
    public static function getBlankPath($contextClass)
55
    {
56
        $node = Site::resolvePath(static::$iconPath);
0 ignored issues
show
Bug introduced by
The type Divergence\Models\Media\Site was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
        return $node ? $node->RealPath : null;
58
    }
59
60
    public function getImage($sourceFile = null)
61
    {
62
        if (!isset($sourceFile)) {
63
            $sourceFile = $this->BlankPath;
0 ignored issues
show
Bug Best Practice introduced by
The property BlankPath does not exist on Divergence\Models\Media\Audio. Since you implemented __get, consider adding a @property annotation.
Loading history...
64
        }
65
66
        return imagecreatefromstring(file_get_contents($sourceFile));
67
    }
68
69
    public function createPreview()
70
    {
71
        // check if a preview already exists
72
73
        if (!empty($_REQUEST['startTime']) && is_numeric($_REQUEST['startTime']) && ($_REQUEST['startTime'] >= 0) && ($_REQUEST['startTime'] < $this->Duration)) {
74
            $startTime = $_REQUEST['startTime'];
75
        } else {
76
            $startTime = 0;
77
        }
78
79
        $previewPath = tempnam('/tmp', 'mediaPreview');
80
81
        // generate preview
82
        $cmd = sprintf(static::$previewExtractCommand, $this->FilesystemPath, $previewPath, $startTime, static::$previewDuration);
0 ignored issues
show
Bug introduced by
It seems like $this->FilesystemPath can also be of type array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        $cmd = sprintf(static::$previewExtractCommand, /** @scrutinizer ignore-type */ $this->FilesystemPath, $previewPath, $startTime, static::$previewDuration);
Loading history...
Bug Best Practice introduced by
The property FilesystemPath does not exist on Divergence\Models\Media\Audio. Since you implemented __get, consider adding a @property annotation.
Loading history...
83
        shell_exec($cmd);
84
85
        if (!filesize($previewPath)) {
86
            throw new Exception('Preview output is empty');
87
        }
88
89
        // create media instance
90
        $PreviewMedia = Media::createFromFile($previewPath, [
91
            'ContextClass' => 'Media'
92
            ,'ContextID' => $this->ID
93
            ,'Caption' => sprintf('%u sec preview (%us-%us)', static::$previewDuration, $startTime, $startTime+static::$previewDuration),
94
        ]);
95
96
        return $PreviewMedia;
97
    }
98
99
    // static methods
100
#    public static function analyzeFile($filename, $mediaInfo = array())
101
#    {
102
#        // Initialize getID3 engine
103
#        $getID3 = new getID3();
104
#
105
#        $mediaInfo['id3Info'] = $getID3->analyze($filename);
106
#
107
#        $mediaInfo['width'] = 0;
108
#        $mediaInfo['height'] = 0;
109
#        $mediaInfo['duration'] = $mediaInfo['id3Info']['playtime_seconds'];
110
#
111
#        return $mediaInfo;
112
#    }
113
}
114