Passed
Pull Request — master (#1300)
by Curtis
05:45
created

Avatar::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace App\Models\enso\Avatars;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Http\UploadedFile;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\DB;
9
use App\Models\User;
10
use App\Contracts\enso\files\Attachable;
11
use App\Traits\enso\files\HasFile;
12
use LaravelEnso\Helpers\App\Traits\CascadesMorphMap;
13
14
/**
15
 * @property int $id
16
 * @property int $user_id
17
 * @property string $created_at
18
 * @property string $updated_at
19
 * @property User $user
20
 */
21
class Avatar extends Model implements Attachable
22
{
23
    use CascadesMorphMap, HasFile;
0 ignored issues
show
introduced by
The trait App\Traits\enso\files\HasFile requires some properties which are not provided by App\Models\enso\Avatars\Avatar: $saved_name, $extensions, $file
Loading history...
24
25
    /**
26
     * The table associated with the model.
27
     * 
28
     * @var string
29
     */
30
    protected $table = 'avatars';
31
32
    /**
33
     * @var array
34
     */
35
36
    public const Width = 250;
37
    public const Height = 250;
38
39
    protected $fillable = ['user_id', 'original_name', 'saved_name'];
40
41
    protected $hidden = ['user_id', 'created_at', 'updated_at'];
42
43
    protected $casts = ['user_id' => 'int'];
44
45
    protected $optimizeImages = true;
46
47
    protected $resizeImages = [
48
        'width' => self::Width,
49
        'height' => self::Height,
50
    ];
51
52
    protected $mimeTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif'];
53
54
    protected $folder = 'avatars';
55
56
    public function user()
57
    {
58
        return $this->belongsTo(User::class);
59
    }
60
61
    public function store(UploadedFile $file)
62
    {
63
        return DB::transaction(function () use ($file) {
64
            $this->delete();
65
            $avatar = self::create(['user_id' => Auth::user()->id]);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
66
67
            return tap($avatar)->upload($file);
68
        });
69
    }
70
}
71