Passed
Pull Request — master (#324)
by Philippe
54:56 queued 21:29
created

ManagesPagebuilder::saveSectionsField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 2
dl 0
loc 15
ccs 12
cts 12
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
4
namespace Thinktomorrow\Chief\Management;
5
6
use Illuminate\Http\Request;
7
use Thinktomorrow\Chief\Pages\Page;
8
use Thinktomorrow\Chief\Sets\SetReference;
9
use Thinktomorrow\Chief\Modules\TextModule;
10
use Thinktomorrow\Chief\Modules\PagetitleModule;
11
use Thinktomorrow\Chief\Sets\StoredSetReference;
12
use Thinktomorrow\Chief\PageBuilder\UpdateSections;
13
use Thinktomorrow\Chief\Relations\AvailableChildren;
14
use Thinktomorrow\Chief\Fields\Types\PagebuilderField;
15
use Thinktomorrow\Chief\FlatReferences\FlatReferencePresenter;
16
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableContract;
17
18
trait ManagesPagebuilder
19
{
20
    /**
21
     * The naming convention is important here because to hook into the saving
22
     * flow it needs to have the save<key>Field method naming.
23
     *
24
     * @param PagebuilderField $field
25
     * @param Request $request
26
     */
27 40
    public function saveSectionsField(PagebuilderField $field, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

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

27
    public function saveSectionsField(/** @scrutinizer ignore-unused */ PagebuilderField $field, Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29 40
        $sections = $request->get('sections', []);
30
31 40
        $modules = $sections['modules'] ?? [];
32 40
        $text    = $sections['text'] ?? [];
33 40
        $sets    = $sections['pagesets'] ?? [];
34 40
        $order   = $sections['order'] ?? [];
35
36 40
        UpdateSections::forModel($this->model, $modules, $text, $sets, $order)
37 40
            ->updateModules()
38 40
            ->updateSets()
39 40
            ->addTextModules()
40 40
            ->updateTextModules()
41 40
            ->sort();
42 40
    }
43
44 47
    protected function createPagebuilderField(): PagebuilderField
45
    {
46 47
        $model = $this->model;
47
48 47
        $availableChildren = AvailableChildren::forParent($model);
49
50
        $modules = $availableChildren->onlyModules()->reject(function ($module) use ($model) {
51 1
            return $module->page_id != null && $module->page_id != $model->id;
52 47
        });
53
54 47
        $available_modules = FlatReferencePresenter::toGroupedSelectValues($modules)->toArray();
55 47
        $available_pages = FlatReferencePresenter::toGroupedSelectValues($availableChildren->onlyPages())->toArray();
56 47
        $available_sets = FlatReferencePresenter::toGroupedSelectValues($availableChildren->onlySets())->toArray();
57
58
        // Current sections
59
        $sections = $model->children()->map(function ($section, $index) {
60 11
            if ($section instanceof TranslatableContract) {
61 9
                $section->injectTranslationForForm();
0 ignored issues
show
Bug introduced by
The method injectTranslationForForm() does not exist on Thinktomorrow\Chief\Conc...le\TranslatableContract. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\Chief\Conc...le\TranslatableContract. ( Ignorable by Annotation )

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

61
                $section->/** @scrutinizer ignore-call */ 
62
                          injectTranslationForForm();
Loading history...
62
            }
63
64
            return [
65
                // Module reference is by id.
66 11
                'id'         => $section->flatReference()->get(),
67
68
                // Key is a separate value to assign each individual module.
69
                // This is separate from id to avoid vue key binding conflicts.
70 11
                'key'        => $section->flatReference()->get(),
71 11
                'type'       => $this->guessPagebuilderSectionType($section),
72 11
                'slug'       => $section->slug,
73 11
                'editUrl'    => $this->findEditUrl($section),
74 11
                'sort'       => $index,
75 11
                'trans'      => $section->trans ?? [],
76
            ];
77 47
        })->toArray();
78
79 47
        return PagebuilderField::make('sections')
0 ignored issues
show
Bug Best Practice introduced by
The expression return Thinktomorrow\Chi...leSets($available_sets) could return the type null|string which is incompatible with the type-hinted return Thinktomorrow\Chief\Fields\Types\PagebuilderField. Consider adding an additional type-check to rule them out.
Loading history...
80 47
                                ->translatable($this->model->availableLocales())
81 47
                                ->sections($sections)
82 47
                                ->availablePages($available_pages)
83 47
                                ->availableModules($available_modules)
84 47
                                ->availableSets($available_sets);
85
    }
86
87
    /**
88
     * @param $model
89
     * @return string|null
90
     */
91 11
    private function findEditUrl($model): ?string
92
    {
93 11
        if (! $model instanceof ManagedModel) {
94 2
            return null;
95
        }
96
97
        try {
98 9
            return app(Managers::class)->findByModel($model)->route('edit');
99 2
        } catch (NonRegisteredManager $e) {
100 2
            return null;
101
        }
102
    }
103
104
    /**
105
     * Section type is the grouping inside the pagebuilder (specifically the menu)
106
     *
107
     * @param $section
108
     * @return string
109
     */
110 11
    private function guessPagebuilderSectionType($section)
111
    {
112 11
        if ($section instanceof TextModule) {
113 6
            return 'text';
114
        }
115
116 5
        if ($section instanceof PagetitleModule) {
117 1
            return 'pagetitle';
118
        }
119
120 4
        if ($section instanceof StoredSetReference || $section instanceof SetReference) {
121
            // TODO: clean this up and replace 'pageset' with 'set';
122 2
            return 'pageset';
123
        }
124
125 2
        if ($section instanceof Page) {
126
            return 'page';
127
        }
128
129
        // We want all other types to be registered as modules
130 2
        return 'module';
131
    }
132
}
133