Passed
Pull Request — 2.x (#1360)
by Harings
09:37
created

Browser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 86
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 3 1
A __construct() 0 64 4
1
<?php
2
3
namespace A17\Twill\View\Components;
4
5
use Illuminate\Support\Str;
6
7
class Browser extends TwillFormComponent
8
{
9
    public $moduleName;
10
    public $modules;
11
    public $endpoints;
12
    public $max;
13
    public $note;
14
    public $fieldNote;
15
    public $browserNote;
16
    public $itemLabel;
17
    public $buttonOnTop;
18
    public $wide;
19
    public $sortable;
20
    public $endpoint;
21
    public $routePrefix;
22
    public $params;
23
24
    public function __construct(
25
        $label,
26
        $name = null,
27
        $renderForBlocks = false,
28
        $renderForModal = false,
29
        $moduleName = null,
30
        $modules = [],
31
        $endpoints = [],
32
        $endpoint = null,
33
        $max = 1,
34
        $note = null,
35
        $fieldNote = null,
36
        $browserNote = null,
37
        $itemLabel = null,
38
        $buttonOnTop = false,
39
        $wide = false,
40
        $sortable = true,
41
        $routePrefix = null,
42
        $params = []
43
    ) {
44
        $name = $name ?? $moduleName;
45
        parent::__construct($name, $label, $renderForBlocks, $renderForModal);
46
        $this->name = $name;
47
        $this->moduleName = $moduleName;
48
        $this->modules = $modules;
49
        $this->endpoints = $endpoints;
50
        $this->endpoint = $endpoint;
51
        $this->max = $max;
52
        $this->note = $note;
53
        $this->fieldNote = $fieldNote;
54
        $this->browserNote = $browserNote;
55
        $this->itemLabel = $itemLabel;
56
        $this->buttonOnTop = $buttonOnTop;
57
        $this->wide = $wide;
58
        $this->sortable = $sortable;
59
        $this->routePrefix = $routePrefix;
60
        $this->params = $params;
61
62
        $endpointsFromModules = isset($this->modules) ? collect($this->modules)->map(function ($module) {
63
            return [
64
                'label' => $module['label'] ?? ucfirst($module['name']),
65
                'value' => moduleRoute(
66
                    $module['name'],
67
                    $module['routePrefix'] ?? null,
68
                    'browser',
69
                    $module['params'] ?? [],
70
                    false
71
                ),
72
            ];
73
        })->toArray() : null;
74
75
        $this->endpoints = $this->endpoints ?? $endpointsFromModules ?? [];
76
        $this->endpoint = $this->endpoint ?? (!empty($endpoints) ? null : moduleRoute(
77
                $this->moduleName,
78
                $this->routePrefix,
79
                'browser',
80
                $this->params,
81
                false
82
            ));
83
84
        $this->itemLabel = $this->itemLabel ?? strtolower($this->label);
85
86
        $this->note = $this->note ??
87
            'Add' . ($this->max > 1 ? " up to {$this->max} " . $itemLabel : ' one ' . Str::singular($this->itemLabel));
88
    }
89
90
    public function render()
91
    {
92
        return view('twill::partials.form._browser');
93
    }
94
}
95