HasFeatureImage::fieldFeatureImage()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 36
ccs 30
cts 30
cp 1
rs 9.552
cc 4
nc 2
nop 2
crap 4
1
<?php
2
3
namespace NovaThinKit\FeatureImage\Nova;
4
5
use Illuminate\Http\Request;
6
use Laravel\Nova\Fields\Image;
7
use NovaThinKit\FeatureImage\Models\WithFeatureImage;
8
9
trait HasFeatureImage
10
{
11 10
    public function fieldFeatureImage(string $title = 'Feature Image', ?string $tag = null): Image
12
    {
13 10
        $model = $this->model();
0 ignored issues
show
Bug introduced by
It seems like model() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

13
        /** @scrutinizer ignore-call */ 
14
        $model = $this->model();
Loading history...
14
15 10
        $attr = method_exists($model, 'featureImageKey') ? $model->featureImageKey($tag) : 'image';
16
17 10
        return Image::make($title, $attr)
18 10
            ->rules(['mimes:jpeg,png', 'max:' . 1024 * 10])
19 10
            ->deletable(true)
20 10
            ->store(function (Request $request, WithFeatureImage $model, string $attribute) use ($tag) {
21 2
                return function () use ($request, $model, $attribute, $tag) {
22 2
                    if ($request->hasFile($attribute)) {
23 2
                        $model->$attribute = $model
24 2
                            ->featureImageManager($tag)
25 2
                            ->storeUploaded($request->file($attribute));
0 ignored issues
show
Bug introduced by
It seems like $request->file($attribute) can also be of type Illuminate\Http\UploadedFile[] and array and null; however, parameter $image of NovaThinKit\FeatureImage...anager::storeUploaded() does only seem to accept Illuminate\Http\UploadedFile, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
                            ->storeUploaded(/** @scrutinizer ignore-type */ $request->file($attribute));
Loading history...
26 2
                        $model->save();
0 ignored issues
show
Bug introduced by
The method save() does not exist on NovaThinKit\FeatureImage\Models\WithFeatureImage. It seems like you code against a sub-type of said class. However, the method does not exist in NovaThinKit\Tests\Fixtures\AdvertisingDTO. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
                        $model->/** @scrutinizer ignore-call */ 
27
                                save();
Loading history...
27
                    }
28 2
                };
29 10
            })
30 10
            ->delete(function (Request $request, WithFeatureImage $model) use ($attr, $tag) {
31 1
                if ($model->featureImageManager($tag)->delete()) {
32 1
                    $model->$attr = null;
33
                }
34
35 1
                return true;
36 10
            })
37 10
            ->preview(function ($value, $disk, WithFeatureImage $model) use ($tag) {
38 4
                return $model->featureImageManager($tag)->url('thumb');
39 10
            })
40 10
            ->thumbnail(function ($value, $disk, WithFeatureImage $model) use ($tag) {
41 4
                return $model->featureImageManager($tag)->url('thumb');
42 10
            })
43 10
            ->download(function ($request, $model, $disk, $path) use ($tag) {
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

43
            ->download(function ($request, $model, $disk, /** @scrutinizer ignore-unused */ $path) use ($tag) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $disk is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

43
            ->download(function ($request, $model, /** @scrutinizer ignore-unused */ $disk, $path) use ($tag) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44 1
                return $model->featureImageManager($tag)->download();
45 10
            })
46 10
            ->help('Image: jpg or png, ~1600x1200px. The script will automatically crop images');
47
    }
48
}
49