Passed
Pull Request — 2.x (#1308)
by Harings
07:04
created

BlockRepository   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 51.02%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
dl 0
loc 114
ccs 25
cts 49
cp 0.5102
rs 10
c 1
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A afterDelete() 0 7 2
A getCrops() 0 3 1
A hydrate() 0 24 3
A afterSave() 0 11 3
A buildFromCmsArray() 0 17 3
A __construct() 0 4 1
A getBlockInstanceForBlockData() 0 10 2
1
<?php
2
3
namespace A17\Twill\Repositories;
4
5
use A17\Twill\Models\Behaviors\HasFiles;
6
use A17\Twill\Models\Behaviors\HasMedias;
7
use A17\Twill\Models\Block;
8
use A17\Twill\Repositories\Behaviors\HandleFiles;
9
use A17\Twill\Repositories\Behaviors\HandleMedias;
10
use A17\Twill\Services\Blocks\Block as BlockConfig;
11
use A17\Twill\Services\Blocks\BlockCollection;
12
use Illuminate\Config\Repository as Config;
13
use Illuminate\Support\Collection;
14
use Log;
15
use ReflectionException;
16
use Schema;
17
18
class BlockRepository extends ModuleRepository
19
{
20
    use HandleMedias, HandleFiles;
0 ignored issues
show
introduced by
The trait A17\Twill\Repositories\Behaviors\HandleMedias requires some properties which are not provided by A17\Twill\Repositories\BlockRepository: $medias, $crop_y, $pivot, $metadatas, $mediasParams, $crop_h, $ratio, $crop_x, $crop_w
Loading history...
introduced by
The trait A17\Twill\Repositories\Behaviors\HandleFiles requires some properties which are not provided by A17\Twill\Repositories\BlockRepository: $filesParams, $files
Loading history...
21
22
    /**
23
     * @var Config
24
     */
25
    protected $config;
26
27
    /**
28
     * @param Block $model
29 22
     */
30
    public function __construct(Block $model, Config $config)
31 22
    {
32 22
        $this->model = $model;
0 ignored issues
show
Documentation Bug introduced by
It seems like $model of type A17\Twill\Models\Block is incompatible with the declared type A17\Twill\Models\Model of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33 22
        $this->config = $config;
34
    }
35
36
    /**
37
     * @param string $role
38
     * @return array
39
     */
40
    public function getCrops($role)
41
    {
42
        return $this->config->get('twill.block_editor.crops')[$role];
43
    }
44 2
45
    public function hydrate($object, $fields)
46 2
    {
47 2
        if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
48
            $relatedItems = Collection::make();
49 2
50
            Collection::make($fields['browsers'])->each(function ($items, $browserName) use (&$relatedItems) {
51
                Collection::make($items)->each(function ($item) use ($browserName, &$relatedItems) {
52
                    try {
53
                        $repository = $this->getModelRepository($item['endpointType'] ?? $browserName);
54
                        $relatedItems->push((object) [
55
                            'related' => $repository->getById($item['id']),
56
                            'browser_name' => $browserName,
57
                        ]);
58
59
                    } catch (ReflectionException $e) {
60
                        Log::error($e);
61
                    }
62 2
                });
63
            });
64 2
65
            $object->setRelation('relatedItems', $relatedItems);
66
        }
67 2
68
        return parent::hydrate($object, $fields);
69
    }
70
71
    /**
72
     * @param HasMedias|HasFiles $object
73
     * @return void
74 2
     */
75
    public function afterSave($object, $fields)
76 2
    {
77 2
        if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
78
            if (isset($fields['browsers'])) {
79
                Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($object) {
80
                    $object->saveRelated($items, $browserName);
0 ignored issues
show
Bug introduced by
It seems like saveRelated() 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

80
                    $object->/** @scrutinizer ignore-call */ 
81
                             saveRelated($items, $browserName);
Loading history...
81
                });
82
            }
83
        }
84 2
85 2
        parent::afterSave($object, $fields);
0 ignored issues
show
Bug introduced by
$object of type A17\Twill\Models\Behavio...els\Behaviors\HasMedias is incompatible with the type A17\Twill\Models\Model expected by parameter $object of A17\Twill\Repositories\M...Repository::afterSave(). ( Ignorable by Annotation )

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

85
        parent::afterSave(/** @scrutinizer ignore-type */ $object, $fields);
Loading history...
86
    }
87
88
    public function afterDelete($object)
89
    {
90
        $object->medias()->sync([]);
91
        $object->files()->sync([]);
92
93
        if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
94
            $object->relatedItems()->delete();
95
        }
96
    }
97
98
    /**
99
     * @param array $block
100
     * @param bool $repeater
101
     * @return array
102 3
     */
103
    public function buildFromCmsArray($block, $repeater = false)
104 3
    {
105
        $blockInstance = $this->getBlockInstanceForBlockData($block, $repeater);
106
107 3
        $block['type'] = $blockInstance['name'];
108
109
        $block['content'] = empty($block['content']) ? new \stdClass : (object) $block['content'];
110 3
111 3
        if ($block['browsers']) {
112 3
            $browsers = Collection::make($block['browsers'])->map(function ($items) {
113
                return Collection::make($items)->pluck('id');
114 3
            })->toArray();
115
116 3
            $block['content']->browsers = $browsers;
117
        }
118
119
        return $block;
120
    }
121
122
    public function getBlockInstanceForBlockData(array $block, bool $repeater = false): Collection
123
    {
124 3
        if ($repeater) {
125
            $blocksList = app(BlockCollection::class)->getRepeaterList();
126
        } else {
127
            $blocksList = app(BlockCollection::class)->getBlockList();
128
        }
129
130
        return $blocksList->first(function(Collection $blockConfig) use ($block) {
131
            return $blockConfig['component'] === $block['type'];
132
        });
133
    }
134
}
135