HostedVideo   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 4 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 3
loc 75
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B sideload() 3 56 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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