Passed
Push — 2.x ( ba8025...66fa7c )
by Quentin
07:23
created

BlockRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\BlockCollection;
11
use Illuminate\Config\Repository as Config;
12
use Illuminate\Support\Collection;
13
use Log;
14
use ReflectionException;
15
use Schema;
16
17
class BlockRepository extends ModuleRepository
18
{
19
    use HandleMedias, HandleFiles;
0 ignored issues
show
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...
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...
20
21
    /**
22
     * @var Config
23
     */
24
    protected $config;
25
26
    /**
27
     * @param Block $model
28
     */
29 22
    public function __construct(Block $model, Config $config)
30
    {
31 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...
32 22
        $this->config = $config;
33 22
    }
34
35
    /**
36
     * @param string $role
37
     * @return array
38
     */
39
    public function getCrops($role)
40
    {
41
        return $this->config->get('twill.block_editor.crops')[$role];
42
    }
43
44 2
    public function hydrate($object, $fields)
45
    {
46 2
        if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
47 2
            $relatedItems = Collection::make();
48
49
            Collection::make($fields['browsers'])->each(function ($items, $browserName) use (&$relatedItems) {
50
                Collection::make($items)->each(function ($item) use ($browserName, &$relatedItems) {
51
                    try {
52
                        $repository = $this->getModelRepository($item['endpointType'] ?? $browserName);
53
                        $relatedItems->push((object) [
54
                            'related' => $repository->getById($item['id']),
55
                            'browser_name' => $browserName,
56
                        ]);
57
58
                    } catch (ReflectionException $e) {
59
                        Log::error($e);
60
                    }
61
                });
62 2
            });
63
64 2
            $object->setRelation('relatedItems', $relatedItems);
65
        }
66
67 2
        return parent::hydrate($object, $fields);
68
    }
69
70
    /**
71
     * @param HasMedias|HasFiles $object
72
     * @return void
73
     */
74 2
    public function afterSave($object, $fields)
75
    {
76 2
        if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
77 2
            if (isset($fields['browsers'])) {
78
                Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($object) {
79
                    $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

79
                    $object->/** @scrutinizer ignore-call */ 
80
                             saveRelated($items, $browserName);
Loading history...
80
                });
81
            }
82
        }
83
84 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

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