HostedVideo::sideload()   B
last analyzed

Complexity

Conditions 7
Paths 15

Size

Total Lines 56

Duplication

Lines 3
Ratio 5.36 %

Importance

Changes 0
Metric Value
dl 3
loc 56
rs 8.0266
c 0
b 0
f 0
cc 7
nc 15
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
namespace app\models\types;
4
5
use Yii;
6
use yii\web\UploadedFile;
7
use yii\helpers\FileHelper;
8
use YoutubeDl\YoutubeDl;
9
10
/**
11
 * This is the model class for HostedVideo content type.
12
 */
13
class HostedVideo extends Video
14
{
15
    public $input = 'file';
16
    public $usable = true;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function __construct($config = [])
22
    {
23
        parent::__construct($config);
24
        $this->name = Yii::t('app', 'Hosted video');
25
        $this->description = Yii::t('app', 'Upload a video to servers.');
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function sideload($url)
32
    {
33
        if (!self::validateUrl($url)) {
34
            $this->addError('load', Yii::t('app', 'Empty or incorrect URL'));
35
36
            return false;
37
        }
38
39
        $youtubeParams = [
40
            'restrict-filenames' => true,
41
            'format' => 'best[ext=mp4]/best[ext=flv]',
42
        ];
43
        if (Yii::$app->params['proxy']) {
44
            $youtubeParams['proxy'] = Yii::$app->params['proxy'];
45
        }
46
        $dl = new YoutubeDl($youtubeParams);
47
        $dl->setDownloadPath(sys_get_temp_dir());
48
49
        try {
50
            $video = $dl->download($url);
51
            if (!$video) {
52
                $this->addError('load', Yii::t('app', 'Downloading failed'));
53
54
                return false;
55
            }
56
        } catch (\Symfony\Component\Process\Exception\ProcessFailedException $e) {
57
            $this->addError('load', Yii::t('app', 'Downloading error'));
58
            Yii::error($e->getMessage());
59
60
            return false;
61
        } catch (\YoutubeDl\Exception\NotFoundException $e) {
62
            $this->addError('load', Yii::t('app', 'Media not found!'));
63
            Yii::error($e->getMessage());
64
65
            return false;
66
        }
67
68
        $filename = $video->getFilename();
69
        $tmpFilepath = sys_get_temp_dir().'/'.$filename;
70
71
        $fileInstance = new UploadedFile();
72
        $fileInstance->name = $filename;
73
        $fileInstance->tempName = $tmpFilepath;
74
        $fileInstance->type = FileHelper::getMimeType($fileInstance->tempName);
75
        $fileInstance->size = $video->getFile()->getSize();
76
        $this->upload = $fileInstance;
77
78 View Code Duplication
        if (static::validateFile($fileInstance->tempName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
            return ['filename' => $filename, 'tmppath' => $tmpFilepath, 'duration' => static::getDuration($tmpFilepath)];
80
        }
81
82
        $this->addError('load', Yii::t('app', 'Invalid file'));
83
        unlink($fileInstance->tempName);
84
85
        return false;
86
    }
87
}
88