Completed
Push — master ( 94ccae...54b8ce )
by Nicolas
02:33
created

ValidateAssetActivity::do_activity()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 59
Code Lines 35

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 59
rs 8.9847
cc 4
eloc 35
nc 4
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 SA\CpeSdk;
12
13
class ValidateAssetActivity extends BasicActivity
14
{
15
    /** @var \finfo */
16
    private $finfo;
17
18
    public function __construct($params, $debug, $cpeLogger = null)
19
    {
20
        parent::__construct($params, $debug, $cpeLogger);
21
        $this->finfo = new \finfo(FILEINFO_MIME_TYPE);
22
    }
23
24
    // Perform the activity
25
    public function do_activity($task)
26
    {
27
        $this->cpeLogger->log_out(
28
            "INFO",
29
            basename(__FILE__),
30
            "Preparing Asset validation ...",
31
            $this->activityLogKey
32
        );
33
34
        // Call parent do_activity:
35
        // It download the input file we will process.
36
        parent::do_activity($task);
37
38
        // Fetch first 1 KiB of file
39
        $this->send_heartbeat($task);
40
        $obj = $this->s3->getObject([
41
            'Bucket' => $this->input->{'input_asset'}->{'bucket'},
42
            'Key' => $this->input->{'input_asset'}->{'file'},
43
            'Range' => '0-10240'
44
        ]);
45
        $this->send_heartbeat($task);
46
47
        // Determine file type
48
        $tmpFile = tempnam(sys_get_temp_dir(), 'ct');
49
        file_put_contents($tmpFile, $obj['Body']);
50
        $mime = trim((new CommandExecuter($this->cpeLogger))->execute(
51
            'file -b --mime-type ' . escapeshellarg($tmpFile))['out']);
52
        $type = substr($mime, 0, strpos($mime, '/'));
53
54
        $this->cpeLogger->log_out(
55
            "DEBUG",
56
            basename(__FILE__),
57
            "File meta information gathered. Mime: $mime | Type: $type",
58
            $this->activityLogKey
59
        );
60
61
        // Load the right transcoder base on input_type
62
        // Get asset detailed info
63
        switch ($type)
64
        {
65
        case 'audio':
66
        case 'video':
67
        case 'image':
68
        default:
69
            require_once __DIR__.'/transcoders/VideoTranscoder.php';
70
71
            // Initiate transcoder obj
72
            $videoTranscoder = new VideoTranscoder($this, $task);
73
            // Get input video information
74
            $assetInfo = $videoTranscoder->get_asset_info($this->pathToInputFile);
75
            $assetInfo->mime = $mime;
76
            $assetInfo->type = $type;
77
78
            // Liberate memory
79
            unset($videoTranscoder);
80
81
            return $assetInfo;
82
        }
83
    }
84
}
85