Completed
Pull Request — master (#139)
by Sebastian
07:31
created

HasContentBlocks::getContentBlockCollectionNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Reset assured: this will be part of blender-model eventually.
5
 */
6
7
namespace App\Models\Traits;
8
9
use App\Models\ContentBlock;
10
use App\Http\Requests\Request;
11
use Illuminate\Support\Collection;
12
use Illuminate\Database\Eloquent\Relations\MorphMany;
13
14
trait HasContentBlocks
15
{
16
    public function contentBlocks(): MorphMany
17
    {
18
        return $this
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
19
            ->morphMany(ContentBlock::class, 'model')
20
            ->whereDraft(false)
21
            ->orderBy('order_column');
22
    }
23
24
    public function getContentBlockCollectionNames(): array
25
    {
26
        return $this->contentBlockCollections ?? ['default'];
0 ignored issues
show
Bug introduced by
The property contentBlockCollections does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
    }
28
29
    public function getContentBlockMediaLibraryCollections(): array
30
    {
31
        return $this->contentBlockMediaLibraryCollections ?? [];
0 ignored issues
show
Bug introduced by
The property contentBlockMediaLibraryCollections does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
    }
33
34
    public function getContentBlocks($collection = 'default'): Collection
35
    {
36
        return $this->contentBlocks->where('collection_name', $collection);
0 ignored issues
show
Bug introduced by
The property contentBlocks does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
    }
38
39
    public function syncContentBlocks(Request $request)
40
    {
41
        foreach ($this->getContentBlockCollectionNames() as $collection) {
42
            if (! $request->has("content_blocks_{$collection}")) {
43
                continue;
44
            }
45
46
            $this->syncContentBlockCollection(
47
                json_decode($request->get("content_blocks_{$collection}"), true),
48
                $collection
49
            );
50
        }
51
    }
52
53
    protected function syncContentBlockCollection(array $data, string $collection)
54
    {
55
        $contentBlocks = collect($data)->map(function (array $attributes, int $i): ContentBlock {
56
            return ContentBlock::findOrFail($attributes['id'])
57
                ->updateWithAttributes($attributes)
58
                ->setOrder($i);
59
        });
60
61
        $this->contentBlocks()
62
            ->where('collection_name', $collection)
63
            ->whereNotIn('id', $contentBlocks->pluck('id'))
64
            ->delete();
65
    }
66
}
67