Completed
Push — master ( f4ebcb...ba1f71 )
by Henry
02:12
created

Audio::__classLoaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Divergence package.
4
 *
5
 * @author Henry Paradiz <[email protected]>
6
 * @copyright 2018 Henry Paradiz <[email protected]>
7
 * @license MIT For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
8
 *
9
 * @since 1.1
10
 */
11
namespace Divergence\Models\Media;
12
13
use Exception;
14
15
/**
16
 * Audio Media Model
17
 *
18
 * @author Henry Paradiz <[email protected]>
19
 * @author Chris Alfano <[email protected]>
20
 *
21
 * {@inheritDoc}
22
 */
23
class Audio extends Media
24
{
25
    // configurables
26
    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
27
    public static $previewDuration = 30;
28
    public static $iconPath = '/site-root/img/icons/filetypes/mp3.png';
29
30
    public function getValue($name)
31
    {
32
        switch ($name) {
33
            case 'ThumbnailMIMEType':
34
                return 'image/png';
35
36
            case 'Width':
37
                return 128;
38
39
            case 'Height':
40
                return 128;
41
42
            case 'Extension':
43
44
                switch ($this->MIMEType) {
45
                    case 'audio/mpeg':
46
                        return 'mp3';
47
                    default:
48
                        throw new Exception('Unable to find audio extension for mime-type: '.$this->MIMEType);
49
                }
50
51
                // no break
52
            default:
53
                return parent::getValue($name);
54
        }
55
    }
56
57
58
    // public methods
59
    public static function getBlankPath($contextClass)
60
    {
61
        $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...
62
        return $node ? $node->RealPath : null;
63
    }
64
65
    public function getImage($sourceFile = null)
66
    {
67
        if (!isset($sourceFile)) {
68
            $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...
69
        }
70
71
        return imagecreatefromstring(file_get_contents($sourceFile));
72
    }
73
74
    public function createPreview()
75
    {
76
        // check if a preview already exists
77
78
        if (!empty($_REQUEST['startTime']) && is_numeric($_REQUEST['startTime']) && ($_REQUEST['startTime'] >= 0) && ($_REQUEST['startTime'] < $this->Duration)) {
79
            $startTime = $_REQUEST['startTime'];
80
        } else {
81
            $startTime = 0;
82
        }
83
84
        $previewPath = tempnam('/tmp', 'mediaPreview');
85
86
        // generate preview
87
        $cmd = sprintf(static::$previewExtractCommand, $this->FilesystemPath, $previewPath, $startTime, static::$previewDuration);
0 ignored issues
show
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...
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

87
        $cmd = sprintf(static::$previewExtractCommand, /** @scrutinizer ignore-type */ $this->FilesystemPath, $previewPath, $startTime, static::$previewDuration);
Loading history...
88
        shell_exec($cmd);
89
90
        if (!filesize($previewPath)) {
91
            throw new Exception('Preview output is empty');
92
        }
93
94
        // create media instance
95
        $PreviewMedia = Media::createFromFile($previewPath, [
96
            'ContextClass' => 'Media'
97
            ,'ContextID' => $this->ID
98
            ,'Caption' => sprintf('%u sec preview (%us-%us)', static::$previewDuration, $startTime, $startTime+static::$previewDuration),
99
        ]);
100
101
        return $PreviewMedia;
102
    }
103
104
    // static methods
105
#    public static function analyzeFile($filename, $mediaInfo = array())
106
#    {
107
#        // Initialize getID3 engine
108
#        $getID3 = new getID3();
109
#
110
#        $mediaInfo['id3Info'] = $getID3->analyze($filename);
111
#
112
#        $mediaInfo['width'] = 0;
113
#        $mediaInfo['height'] = 0;
114
#        $mediaInfo['duration'] = $mediaInfo['id3Info']['playtime_seconds'];
115
#
116
#        return $mediaInfo;
117
#    }
118
}
119