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

ContentBlockTransformer::getMediaAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models\Transformers;
4
5
use App\Models\ContentBlock;
6
use League\Fractal\TransformerAbstract;
7
8
class ContentBlockTransformer extends TransformerAbstract
9
{
10
    public function transform(ContentBlock $contentBlock)
11
    {
12
        $attributes = [
13
            'id' => $contentBlock->id,
14
            'type' => $contentBlock->type,
15
            'orderColumn' => $contentBlock->order_column,
16
        ];
17
18
        return array_merge(
19
            $attributes,
20
            $this->getMediaAttributes($contentBlock),
21
            $this->getTranslatedAttributes($contentBlock)
22
        );
23
    }
24
25
    protected function getMediaAttributes(ContentBlock $contentBlock): array
26
    {
27
        return array_reduce($contentBlock->mediaLibraryCollectionNames(), function ($mediaAttributes, $collectionName) use ($contentBlock) {
28
            $mediaAttributes[$collectionName] = fractal($contentBlock->getMedia($collectionName), new MediaTransformer())->toArray();
29
30
            return $mediaAttributes;
31
        }, []);
32
    }
33
34
    protected function getTranslatedAttributes(ContentBlock $contentBlock): array
35
    {
36
        return array_reduce($contentBlock->translatable, function ($translatables, $attribute) use ($contentBlock) {
37
            foreach (config('app.locales') as $locale) {
38
                $translatables[$attribute][$locale] = $contentBlock->getTranslation($attribute, $locale);
39
            }
40
41
            return $translatables;
42
        }, []);
43
    }
44
}
45