Passed
Push — 1.2 ( 2544d0...f6cea4 )
by Quentin
07:12
created

BlockRepository::hydrate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 24
rs 9.8333
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 Illuminate\Config\Repository as Config;
11
use Illuminate\Support\Collection;
12
use Log;
13
use ReflectionException;
14
use Schema;
15
16
class BlockRepository extends ModuleRepository
17
{
18
    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...
19
20
    /**
21
     * @var Config
22
     */
23
    protected $config;
24
25
    /**
26
     * @param Block $model
27
     */
28
    public function __construct(Block $model, Config $config)
29
    {
30
        $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...
31
        $this->config = $config;
32
    }
33
34
    /**
35
     * @param string $role
36
     * @return array
37
     */
38
    public function getCrops($role)
39
    {
40
        return $this->config->get('twill.block_editor.crops')[$role];
41
    }
42
43
    public function hydrate($object, $fields)
44
    {
45
        if (Schema::hasTable(config('twill.related_table', 'related'))) {
46
            $relatedItems = Collection::make();
47
48
            Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($object, &$relatedItems) {
0 ignored issues
show
Unused Code introduced by
The import $object is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
49
                Collection::make($items)->each(function ($item) use ($browserName, &$relatedItems) {
50
                    try {
51
                        $repository = $this->getModelRepository($item['endpointType'] ?? $browserName);
52
                        $relatedItems->push((object) [
53
                            'related' => $repository->getById($item['id']),
54
                            'browser_name' => $browserName,
55
                        ]);
56
57
                    } catch (ReflectionException $e) {
58
                        Log::error($e);
59
                    }
60
                });
61
            });
62
63
            $object->setRelation('relatedItems', $relatedItems);
64
        }
65
66
        return parent::hydrate($object, $fields);
67
    }
68
69
    /**
70
     * @param HasMedias|HasFiles $object
71
     * @return void
72
     */
73
    public function afterSave($object, $fields)
74
    {
75
        if (Schema::hasTable(config('twill.related_table', 'related'))) {
76
            Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($object) {
77
                $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

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

81
        parent::afterSave(/** @scrutinizer ignore-type */ $object, $fields);
Loading history...
82
    }
83
84
    public function afterDelete($object)
85
    {
86
        $object->medias()->sync([]);
87
        $object->files()->sync([]);
88
89
        if (Schema::hasTable(config('twill.related_table', 'related'))) {
90
            $object->relatedItems()->delete();
91
        }
92
    }
93
94
    /**
95
     * @param array $block
96
     * @param bool $repeater
97
     * @return array
98
     */
99
    public function buildFromCmsArray($block, $repeater = false)
100
    {
101
        $blocksFromConfig = $this->config->get('twill.block_editor.' . ($repeater ? 'repeaters' : 'blocks'));
102
103
        $block['type'] = Collection::make($blocksFromConfig)->search(function ($blockConfig) use ($block) {
104
            return $blockConfig['component'] === $block['type'];
105
        });
106
107
        $block['content'] = empty($block['content']) ? new \stdClass : (object) $block['content'];
108
109
        if ($block['browsers']) {
110
            $browsers = Collection::make($block['browsers'])->map(function ($items) {
111
                return Collection::make($items)->pluck('id');
112
            })->toArray();
113
114
            $block['content']->browsers = $browsers;
115
        }
116
117
        return $block;
118
    }
119
}
120