Passed
Push — master ( e1e679...09e224 )
by Quentin
10:29
created

HandleRevisions::hydrateHandleRevisions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 13
ccs 4
cts 6
cp 0.6667
crap 3.3332
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Repositories\Behaviors;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Auth;
8
9
trait HandleRevisions
10
{
11 2
    public function hydrateHandleRevisions($object, $fields)
12
    {
13
        // HandleRepeaters trait => getRepeaters
14 2
        foreach($this->getRepeaters() as $repeater) {
0 ignored issues
show
Bug introduced by
It seems like getRepeaters() 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

14
        foreach($this->/** @scrutinizer ignore-call */ getRepeaters() as $repeater) {
Loading history...
15
            $this->hydrateRepeater($object, $fields, $repeater['relation'], $repeater['model']);
16
        }
17
18
        // HandleBrowers trait => getBrowsers
19 2
        foreach($this->getBrowsers() as $browser) {
0 ignored issues
show
Bug introduced by
It seems like getBrowsers() 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

19
        foreach($this->/** @scrutinizer ignore-call */ getBrowsers() as $browser) {
Loading history...
20
            $this->hydrateBrowser($object, $fields, $browser['relation'], $browser['positionAttribute'], $browser['model']);
21
        }
22
23 2
        return $object;
24
    }
25
26 17
    public function beforeSaveHandleRevisions($object, $fields)
27
    {
28 17
        $lastRevisionPayload = json_decode($object->revisions->first()->payload ?? "{}", true);
29
30 17
        if ($fields != $lastRevisionPayload) {
31 17
            $object->revisions()->create([
32 17
                'payload' => json_encode($fields),
33 17
                'user_id' => Auth::guard('twill_users')->user()->id ?? null,
34
            ]);
35
        }
36
37 17
        return $fields;
38
    }
39
40 1
    public function preview($id, $fields)
41
    {
42 1
        $object = $this->model->findOrFail($id);
43
44 1
        return $this->hydrateObject($object, $fields);
45
    }
46
47 2
    protected function hydrateObject($object, $fields)
48
    {
49 2
        $fields = $this->prepareFieldsBeforeSave($object, $fields);
0 ignored issues
show
Bug introduced by
It seems like prepareFieldsBeforeSave() 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

49
        /** @scrutinizer ignore-call */ 
50
        $fields = $this->prepareFieldsBeforeSave($object, $fields);
Loading history...
50
51 2
        $object->fill(Arr::except($fields, $this->getReservedFields()));
0 ignored issues
show
Bug introduced by
It seems like getReservedFields() 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

51
        $object->fill(Arr::except($fields, $this->/** @scrutinizer ignore-call */ getReservedFields()));
Loading history...
52
53 2
        $object = $this->hydrate($object, $fields);
0 ignored issues
show
Bug introduced by
The method hydrate() does not exist on A17\Twill\Repositories\Behaviors\HandleRevisions. Did you maybe mean hydrateOrderedBelongsTomany()? ( Ignorable by Annotation )

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

53
        /** @scrutinizer ignore-call */ 
54
        $object = $this->hydrate($object, $fields);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
55 2
        return $object;
56
    }
57
58 1
    public function previewForRevision($id, $revisionId)
59
    {
60 1
        $object = $this->model->findOrFail($id);
61
62 1
        $fields = json_decode($object->revisions->where('id', $revisionId)->first()->payload, true);
63
64 1
        $hydratedObject = $this->hydrateObject($this->model->newInstance(), $fields);
65 1
        $hydratedObject->id = $id;
66
67 1
        return $hydratedObject;
68
    }
69
70
    public function hydrateMultiSelect($object, $fields, $relationship, $model = null, $customHydratedRelationship = null)
71
    {
72
        $fieldsHasElements = isset($fields[$relationship]) && !empty($fields[$relationship]);
73
        $relatedElements = $fieldsHasElements ? $fields[$relationship] : [];
74
75
        $relationRepository = $this->getModelRepository($relationship, $model);
0 ignored issues
show
Bug introduced by
It seems like getModelRepository() 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

75
        /** @scrutinizer ignore-call */ 
76
        $relationRepository = $this->getModelRepository($relationship, $model);
Loading history...
76
        $relatedElementsCollection = Collection::make();
77
78
        foreach ($relatedElements as $relatedElement) {
79
            $newRelatedElement = $relationRepository->getById($relatedElement);
80
            $relatedElementsCollection->push($newRelatedElement);
81
        }
82
83
        $object->setRelation($customHydratedRelationship ?? $relationship, $relatedElementsCollection);
84
    }
85
86
    public function hydrateBrowser($object, $fields, $relationship, $positionAttribute = 'position', $model = null)
87
    {
88
        return $this->hydrateOrderedBelongsTomany($object, $fields, $relationship, $positionAttribute, $model);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->hydrateOrderedBel...itionAttribute, $model) targeting A17\Twill\Repositories\B...eOrderedBelongsTomany() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
89
    }
90
91
    public function hydrateOrderedBelongsTomany($object, $fields, $relationship, $positionAttribute = 'position', $model = null)
92
    {
93
        $fieldsHasElements = isset($fields['browsers'][$relationship]) && !empty($fields['browsers'][$relationship]);
94
        $relatedElements = $fieldsHasElements ? $fields['browsers'][$relationship] : [];
95
96
        $relationRepository = $this->getModelRepository($relationship, $model);
97
        $relatedElementsCollection = Collection::make();
98
        $position = 1;
99
100
        foreach ($relatedElements as $relatedElement) {
101
            $newRelatedElement = $relationRepository->getById($relatedElement['id']);
102
            $pivot = $newRelatedElement->newPivot($object, [$positionAttribute => $position++], $object->$relationship()->getTable(), true);
103
            $newRelatedElement->setRelation('pivot', $pivot);
104
            $relatedElementsCollection->push($newRelatedElement);
105
        }
106
107
        $object->setRelation($relationship, $relatedElementsCollection);
108
    }
109
110
    public function hydrateRepeater($object, $fields, $relationship, $model)
111
    {
112
        $relationFields = $fields['repeaters'][$relationship] ?? [];
113
114
        $relationRepository = $this->getModelRepository($relationship, $model);
115
116
        $repeaterCollection = Collection::make();
117
118
        foreach ($relationFields as $index => $relationField) {
119
            $relationField['position'] = $index + 1;
120
            $relationField[$this->model->getForeignKey()] = $object->id;
121
            unset($relationField['id']);
122
123
            $newRepeater = $relationRepository->createForPreview($relationField);
124
125
            $repeaterCollection->push($newRepeater);
126
        }
127
128
        $object->setRelation($relationship, $repeaterCollection);
129
    }
130
131 5
    public function getCountForMine()
132
    {
133 5
        $query = $this->model->newQuery();
134 5
        return $this->filter($query, $this->countScope)->mine()->count();
0 ignored issues
show
Bug introduced by
It seems like filter() 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

134
        return $this->/** @scrutinizer ignore-call */ filter($query, $this->countScope)->mine()->count();
Loading history...
135
    }
136
137 5
    public function getCountByStatusSlugHandleRevisions($slug)
138
    {
139 5
        if ($slug === 'mine') {
140 5
            return $this->getCountForMine();
141
        }
142
143
        return false;
144
    }
145
}
146