SearchController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 84
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSearchResults() 0 20 4
A search() 0 13 2
A getAutorizedModules() 0 22 4
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Core;
4
5
use Spatie\Searchable\Search;
0 ignored issues
show
Bug introduced by
The type Spatie\Searchable\Search was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Uccello\Core\Models\Domain;
7
use Uccello\Core\Models\Module;
8
9
class SearchController extends Controller
10
{
11
    protected $viewName = 'search.main';
12
13
    /**
14
     * @var \Uccello\Core\Models\Domain
15
     */
16
    protected $domain;
17
18
    /**
19
     * Search records in all modules where user has retrieve capability.
20
     *
21
     * @param \Uccello\Core\Models\Domain
22
     * @return \Illuminate\Support\Collection
23
     */
24
    public function search(?Domain $domain)
25
    {
26
        // Pre-process. We force module to be 'home'
27
        $this->preProcess($domain, ucmodule('home'), request());
0 ignored issues
show
Bug introduced by
It seems like ucmodule('home') can also be of type null; however, parameter $module of Uccello\Core\Http\Contro...ontroller::preProcess() does only seem to accept Uccello\Core\Models\Module, maybe add an additional type check? ( Ignorable by Annotation )

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

27
        $this->preProcess($domain, /** @scrutinizer ignore-type */ ucmodule('home'), request());
Loading history...
28
29
        if (!request('q')) {
30
            return collect();
31
        }
32
33
        $searchResults = $this->getSearchResults();
34
35
36
        return $this->autoView(compact('searchResults'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->autoView(compact('searchResults')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Collection.
Loading history...
37
    }
38
39
    /**
40
     * Search records in all autorized modules
41
     *
42
     * @return \Illuminate\Support\Collection
43
     */
44
    protected function getSearchResults()
45
    {
46
        $searchResults = new Search();
47
48
        // Get all modules in autorized modules
49
        $modules = $this->getAutorizedModules();
50
51
        foreach ($modules as $module) {
52
            $modelClass = $module->model_class;
53
54
            if (method_exists($modelClass, 'getSearchResult') && property_exists($modelClass, 'searchableColumns')) {
55
                $searchResults->registerModel($modelClass, (array) (new $modelClass)->searchableColumns);
56
            }
57
        }
58
59
        $searchQuery = request('q');
60
61
        return $searchResults
62
            ->search($searchQuery)
63
            ->take(config('uccello.max_results.search', 50));
64
    }
65
66
    /**
67
     * Retrieve all modules in witch the user can search.
68
     *
69
     * @return \Illuminate\Support\Collection
70
     */
71
    protected function getAutorizedModules()
72
    {
73
        $autorizedModules = collect();
74
75
        // Restrct on CRUD modules activated on the domain
76
        $query = $this->domain->modules()->whereNotNull('model_class');
77
78
        // If we want to restrict the search in a module add the condition
79
        if (request('module')) {
80
            $query = $query->where('name', request('module'));
81
        }
82
83
        // Get all module
84
        $modules = $query->get();
85
86
        foreach ($modules as $module) {
87
            if (auth()->user()->canRetrieve($this->domain, $module)) {
88
                $autorizedModules->push($module);
89
            }
90
        }
91
92
        return $autorizedModules;
93
    }
94
}
95