Completed
Push — master ( b991b0...0f93a7 )
by Tyler
10:35 queued 09:22
created

Viddler::updateStatusTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
namespace LeadThread\Viddler\Upload\Models;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Model;
7
use LeadThread\Viddler\Upload\Traits\CanLog;
8
use LeadThread\Viddler\Upload\Components\ViddlerClient;
9
use LeadThread\Viddler\Upload\Components\VideoFile;
10
use LeadThread\Viddler\Upload\Events\ViddlerError;
11
use LeadThread\Viddler\Upload\Events\ViddlerFinished;
12
13
/**
14
  * @property boolean $uploaded
15
  * @property string  $disk
16
  * @property string  $extension
17
  * @property string  $filename
18
  * @property string  $mime
19
  * @property string  $path
20
  * @property string  $status
21
  * @property string  $title
22
  * @property string  $viddler_id
23
  */
24
class Viddler extends Model
25
{
26
    use CanLog;
27
    
28
    protected $guarded = ['id'];
29
    protected $table;
30
    protected $client;
31
    public $file;
32
33 18
    public function __construct(array $attributes = [])
34
    {
35 18
        if (empty($this->table)) {
36 18
            $this->table = config('viddler.table');
37 18
        }
38 18
        parent::__construct($attributes);
39
40 18
        $this->file = new VideoFile($this);
41
42 18
        $this->client = $this->getClient();
43 18
    }
44
45 18
    public function __toString()
46 18
    {
47 18
        return "Viddler #{$this->id}";
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<LeadThread\Viddler\Upload\Models\Viddler>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
48
    }
49
50 18
    public function convert()
51
    {
52
        try {
53 18
            $this->file->convert();
54 18
        } catch (Exception $e) {
55
            $this->handleError($e);
56
            throw $e;
57
        }
58 18
        return $this;
59
    }
60
61 18
    public function upload()
62
    {
63
        try {
64 18
            $this->client->upload($this);
65 18
        } catch (Exception $e) {
66 3
            $this->handleError($e);
67 3
            throw $e;
68
        }
69 15
        return $this;
70
    }
71
72 6
    public function check()
73
    {
74
        try {
75 6
            $this->client->check($this);
76 6
        } catch (Exception $e) {
77 3
            $this->handleError($e);
78 3
            throw $e;
79
        }
80 3
        return $this;
81
    }
82
83 18
    public function getFile()
84
    {
85 18
        return $this->file;
86 6
    }
87
88 18
    public function updateStatusTo($status)
89
    {
90 18
        $this->file->moveTo($status);
91 18
        $this->status = $status;
92
        
93 18
        $this->save();
94
95 18
        if ($this->status === "finished") {
96 3
            event(new ViddlerFinished($this));
97 9
        }
98
        
99 18
        return $this;
100
    }
101
102 18
    public function isResolved()
103
    {
104 18
        return $this->status === "finished" || $this->status === "error";
105
    }
106
107 18
    public function isNotResolved()
108
    {
109 18
        return !$this->isResolved();
110 6
    }
111
112 18
    public function setClient(ViddlerClient $client)
113 6
    {
114 18
        $this->client = $client;
115 18
    }
116
117 18
    public function getClient()
118
    {
119 18
        if (empty($this->client)) {
120 18
            $this->client = new ViddlerClient();
121 18
        }
122 18
        return $this->client;
123
    }
124
125 18
    protected function handleError(Exception $e)
126
    {
127 6
        $this->file->removeFile();
128
129 6
        $this->status = "error";
130 6
        $this->path = null;
131 6
        $this->filename = null;
132 6
        $this->disk = null;
133 6
        $this->extension = null;
134 6
        $this->mime = null;
135 18
        $this->save();
136
137 6
        $this->error("{$this} Error Occurred! ".$e->getMessage());
138
139 6
        event(new ViddlerError($this, $e->getMessage()));
140 6
    }
141
}
142