Test Setup Failed
Push — ft/command-palette ( bece37...ccd98a )
by
unknown
11:42
created

CommandPaletteController::getAllPageModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Controllers\Back\CommandPalette;
4
5
use Illuminate\Support\Collection;
6
use Thinktomorrow\Chief\App\Http\Controllers\Controller;
7
use Thinktomorrow\Chief\Managers\Register\Registry;
8
9
class CommandPaletteController extends Controller
10
{
11
    public function __construct()
12
    {
13
        $this->pageModels = $this->getAllPageModels();
0 ignored issues
show
Bug Best Practice introduced by
The property pageModels does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
14
    }
15
16
    public function search(string $term = '')
17
    {
18
        $results = [];
19
20
        foreach ($this->pageModels as $model) {
21
            if ($term === $model->adminConfig()->getModelName()) {
22
                array_push($results, [
23
                    'title' => $model->title,
24
                    'url' => '/admin/' . $model->managedModelKey() . '/' . $model->id . '/edit',
25
                ]);
26
            }
27
        }
28
29
        return response()->view('chief::layout.nav.command-palette._result', [
30
            'term' => $term,
31
            'results' => $results,
32
        ]);
33
    }
34
35
    private function getAllPageModels(): Collection
36
    {
37
        return collect(app(Registry::class)->models())
38
            // Filter out fragment models
39
            ->filter(function ($model) {
40
                return ! in_array('Thinktomorrow\Chief\Fragments\Fragmentable', class_implements($model));
41
                // Return all instances of the models
42
            })->map(function ($model) {
43
                return $model::all();
44
                // Flatten all models to the same level
45
            })->flatten(1);
46
    }
47
}
48