Completed
Pull Request — master (#51)
by Tyler
02:38
created

ValidateAssetActivity::do_activity()   C

Complexity

Conditions 9
Paths 12

Size

Total Lines 75
Code Lines 42

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 75
rs 5.875
cc 9
eloc 42
nc 12
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This class validate input assets and get metadata about them
5
 * It makes sure the input files to be transcoded exists and is valid.
6
 * Based on the input file type we lunch the proper transcoder
7
 */
8
9
require_once __DIR__.'/BasicActivity.php';
10
11
use Guzzle\Http\EntityBody;
12
use SA\CpeSdk;
13
14
class ValidateAssetActivity extends BasicActivity
15
{
16
    /** @var \finfo */
17
    private $finfo;
18
19
    public function __construct($params, $debug, $cpeLogger = null)
20
    {
21
        parent::__construct($params, $debug, $cpeLogger);
22
        $this->finfo = new \finfo(FILEINFO_MIME_TYPE);
23
    }
24
25
    // Perform the activity
26
    public function do_activity($task)
27
    {
28
        $this->cpeLogger->log_out(
29
            "INFO",
30
            basename(__FILE__),
31
            "Preparing Asset validation ...",
32
            $this->activityLogKey
33
        );
34
35
        // Call parent do_activity:
36
        // It download the input file we will process.
37
        parent::do_activity($task);
38
39
        // Fetch first 1 KiB of file
40
        $this->send_heartbeat($task);
41
        $tmpFile = tempnam(sys_get_temp_dir(), 'ct');
42
        $obj = $this->s3->getObject([
43
            'Bucket' => $this->input->{'input_asset'}->{'bucket'},
44
            'Key' => $this->input->{'input_asset'}->{'file'},
45
            'Range' => 'bytes=0-1024'
46
        ]);
47
        $this->send_heartbeat($task);
48
49
        // Determine file type
50
        file_put_contents($tmpFile, (string) $obj['Body']);
51
        $mime = trim((new CommandExecuter($this->cpeLogger))->execute(
52
            'file -b --mime-type ' . escapeshellarg($tmpFile))['out']);
53
        $type = substr($mime, 0, strpos($mime, '/'));
54
55
        $this->cpeLogger->log_out(
56
            "DEBUG",
57
            basename(__FILE__),
58
            "File meta information gathered. Mime: $mime | Type: $type",
59
            $this->activityLogKey
60
        );
61
62
        // Load the right transcoder base on input_type
63
        // Get asset detailed info
64
        switch ($type)
65
        {
66
        case 'audio':
67
        case 'video':
68
        case 'image':
69
        default:
70
            require_once __DIR__.'/transcoders/VideoTranscoder.php';
71
72
            // Initiate transcoder obj
73
            $videoTranscoder = new VideoTranscoder($this, $task);
74
            // Get input video information
75
            $assetInfo = $videoTranscoder->get_asset_info($this->pathToInputFile);
76
77
            // Liberate memory
78
            unset($videoTranscoder);
79
        }
80
81
        if ($mime === 'application/octet-stream' && isset($asset['streams'])) {
0 ignored issues
show
Bug introduced by
The variable $asset seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
82
            // Check all stream types
83
            foreach ($asset['streams'] as $stream) {
84
                if ($stream['codec_type'] === 'video') {
85
                    // For a video type, set type to video and break
86
                    $type = 'video';
87
                    break;
88
                } elseif ($stream['codec_type'] === 'audio') {
89
                    // For an audio type, set to audio, but don't break
90
                    // in case there's a video stream later
91
                    $type = 'audio';
92
                }
93
            }
94
        }
95
96
        $assetInfo->mime = $mime;
97
        $assetInfo->type = $type;
98
99
        return $assetInfo;
100
    }
101
}
102