ImageRepository::findExistingImageable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace HustleWorks\ChuteLaravel\Repositories;
4
5
use HustleWorks\Chute\Contracts\ImageRepositoryInterface;
6
use HustleWorks\Chute\DTO\ImageRecord;
7
use HustleWorks\ChuteLaravel\Models\Image;
8
9
class ImageRepository implements ImageRepositoryInterface
10
{
11
    public function create(array $attributes): ImageRecord
12
    {
13
        $image                 = new Image($attributes);
14
        $image->imageable_id   = $attributes['imageable_id'];
15
        $image->imageable_type = $attributes['imageable_type'];
16
        $image->save();
17
18
        return $this->_buildImageRecordFromImage($image);
19
    }
20
21
    /**
22
     * Find By Id
23
     *
24
     * @param $id
25
     * @return ImageRecord
26
     */
27
    public function findById($id): ImageRecord
28
    {
29
        return $this->_buildImageRecordFromImage(Image::find($id));
30
    }
31
32
    /**
33
     * Update
34
     *
35
     * @param int|ImageRecord $model
36
     * @param array           $attributes
37
     * @return mixed
38
     */
39
    public function update($model, array $attributes)
40
    {
41
        $image = $this->_findRecord($model);
42
        $image->update($attributes);
43
44
        return $this->_buildImageRecordFromImage($image, true);
45
    }
46
47
    /**
48
     * @param $identifier
49
     * @return Image
50
     */
51
    private function _findRecord($identifier)
52
    {
53
        /** @var Image $image */
54
        if ($identifier instanceof ImageRecord) {
55
            $image = Image::findOrFail($identifier->id);
56
        } else {
57
            $image = Image::findOrFail($identifier);
58
        }
59
60
        return $image;
61
    }
62
63
    /**
64
     * Build Image Record From Image
65
     *
66
     * @param Image $image
67
     * @param bool  $refresh
68
     * @return ImageRecord
69
     */
70
    private function _buildImageRecordFromImage(Image $image, bool $refresh = false)
71
    {
72
        $data = $refresh ? $image->fresh()->getAttributes() : $image->getAttributes();
73
        unset($data['imageable_id'], $data['imageable_type']);
74
75
        return new ImageRecord($data);
76
    }
77
78
    /**
79
     * Find Existing Relation
80
     *
81
     * Search for existing image, by the existing polymorphic identifiers
82
     *
83
     * @param array $identifiers for polymorphic relationship
84
     * @return ImageRecord
85
     */
86
    public function findExistingImageable(array $identifiers)
87
    {
88
        $image = Image::where('imageable_id', $identifiers['imageable_id'])
89
            ->where('imageable_type', $identifiers['imageable_type'])
90
            ->first();
91
92
        return $image_record = $image ? $this->_buildImageRecordFromImage($image) : null;
0 ignored issues
show
Unused Code introduced by
$image_record is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
93
    }
94
95
    /**
96
     * Delete
97
     *
98
     * @param int|ImageRecord $model
99
     * @return bool
100
     * @throws \Exception
101
     */
102
    public function delete($model)
103
    {
104
        $image = $this->_findRecord($model);
105
106
        return $image->delete();
107
    }
108
}