Completed
Pull Request — master (#6)
by Tyler
09:27
created

Service   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 90.74%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 101
ccs 49
cts 54
cp 0.9074
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setClient() 0 4 1
A check() 0 5 1
B create() 0 41 4
A getClient() 0 7 2
A isMimeSupported() 0 5 1
A getSupportedMimes() 0 15 1
1
<?php
2
3
namespace LeadThread\Viddler\Upload;
4
5
use Exception;
6
use Illuminate\Http\UploadedFile;
7
use Storage;
8
use LeadThread\Viddler\Upload\Components\ViddlerClient;
9
use LeadThread\Viddler\Upload\Exceptions\ViddlerIncorrectVideoTypeException;
10
use LeadThread\Viddler\Upload\Exceptions\ViddlerUploadFailedException;
11
use LeadThread\Viddler\Upload\Jobs\ProcessVideoJob;
12
use LeadThread\Viddler\Upload\Models\Viddler;
13
14
class Service
15
{
16
17
    /**
18
     * ViddlerClient
19
     */
20
    protected $client = null;
21
22
    /**
23
     * Checks the encoding status of a Viddler video
24
     */
25 6
    public function check(Viddler $model)
26
    {
27 6
        $model = $model->check();
28 3
        return $model;
29
    }
30
31
    /**
32
     * Convert, Upload and Store in Database from a video file
33
     */
34 24
    public function create(UploadedFile $file, $title)
35
    {
36 24
        if ($file->isValid()) {
37
            //Check file type
38 21
            $ok = $this->isMimeSupported($file->getMimeType());
39
40 21
            if ($ok === true) {
41
                //Store the file
42 18
                $filename = $file->hashName();
43 18
                $disk = Storage::disk(config('viddler.disk'));
44 18
                $contents = file_get_contents($file->getRealPath());
45 18
                $disk->put("new/".$filename, $contents);
46
47 18
                $class = config('viddler.model');
48
                
49 18
                $video = new $class([
50 18
                    'disk' => config('viddler.disk'),
51 18
                    'path' => 'new',
52 18
                    'filename' => $filename,
53 18
                    'title' => $title,
54 18
                    'status' => 'new',
55 18
                    'mime' => $file->getMimeType(),
56 18
                    'extension' => $file->extension(),
57 18
                ]);
58 18
                $video->save();
59
60
                //Done
61 18
                return $video;
62
            } else {
63 3
                $msg = [];
64 3
                $msg[] = "Incorrect file type!";
65 3
                if (is_object($file)) {
66 3
                    $msg[] = $file->getClientOriginalExtension();
67 3
                    $msg[] = "(".$file->getMimeType().")";
68 3
                }
69 3
                throw new ViddlerIncorrectVideoTypeException(implode(" ", $msg));
70
            }
71
        } else {
72 3
            throw new ViddlerUploadFailedException("Upload Failed");
73
        }
74
    }
75
76 6
    public function setClient(ViddlerClient $client)
77 3
    {
78 6
        $this->client = $client;
79 6
    }
80
81
    public function getClient()
82
    {
83
        if (empty($this->client)) {
84
            $this->client = new ViddlerClient();
85
        }
86
        return $this->client;
87
    }
88
89
    /**
90
     * Check if a mime is in the supported list
91
     * @param string|null $mime
92
     */
93 21
    protected function isMimeSupported($mime)
94
    {
95 21
        $supported = $this->getSupportedMimes();
96 21
        return in_array($mime, $supported);
97
    }
98
99 21
    protected function getSupportedMimes()
100
    {
101
        return [
102 21
            "video/x-msvideo",
103 21
            "video/mp4",
104 21
            "video/x-m4v",
105 21
            "video/x-flv",
106 21
            "video/quicktime",
107 21
            "video/x-ms-wmv",
108 21
            "video/mpeg",
109 21
            "video/3gpp",
110 21
            "video/x-ms-asf",
111
            "application/octet-stream"
112 21
        ];
113
    }
114
}
115