Breadcrumbs   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 141
ccs 0
cts 50
cp 0
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 9 1
A assembly() 0 3 1
A render() 0 8 1
A edit() 0 11 1
A currentAction() 0 9 2
A index() 0 5 1
A view() 0 10 1
A presentEloquent() 0 7 2
1
<?php
2
3
namespace Terranet\Administrator\Services;
4
5
use DaveJamesMiller\Breadcrumbs\BreadcrumbsGenerator;
6
use DaveJamesMiller\Breadcrumbs\BreadcrumbsManager;
7
use Terranet\Administrator\Contracts\Module;
8
use Terranet\Administrator\Services\Breadcrumbs\EloquentPresenter;
9
10
class Breadcrumbs
11
{
12
    /**
13
     * Current scaffold module.
14
     *
15
     * @var Module
16
     */
17
    protected $module;
18
19
    /**
20
     * Breadcrumbs manager.
21
     *
22
     * @var BreadcrumbsManager
23
     */
24
    protected $manager;
25
26
    /**
27
     * @param BreadcrumbsManager $manager
28
     * @param Module $module
29
     */
30
    public function __construct(BreadcrumbsManager $manager, Module $module)
31
    {
32
        $this->module = $module;
33
        $this->manager = $manager;
34
    }
35
36
    /**
37
     * Render breadcrumbs.
38
     *
39
     * @throws \DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException
40
     * @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException
41
     * @throws \DaveJamesMiller\Breadcrumbs\Exceptions\ViewNotSetException
42
     *
43
     * @return string
44
     */
45
    public function render()
46
    {
47
        $action = $this->currentAction();
48
49
        // assembly breadcrumbs
50
        $this->assembly($action);
51
52
        return $this->manager->view('administrator::partials.breadcrumbs', $action);
53
    }
54
55
    /**
56
     * Detect current action.
57
     *
58
     * @return null|string
59
     */
60
    protected function currentAction()
61
    {
62
        $action = substr($action = app('router')->currentRouteAction(), strpos($action, '@') + 1);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

62
        $action = substr($action = /** @scrutinizer ignore-call */ app('router')->currentRouteAction(), strpos($action, '@') + 1);
Loading history...
63
64
        if (!method_exists($this, $action)) {
65
            $action = 'index';
66
        }
67
68
        return $action;
69
    }
70
71
    /**
72
     * @param $action
73
     *
74
     * @return mixed
75
     */
76
    protected function assembly($action)
77
    {
78
        return \call_user_func_array([$this, $action], []);
79
    }
80
81
    /**
82
     * Render `Index page` breadcrumbs.
83
     */
84
    protected function index()
85
    {
86
        $this->manager->register('index', function (BreadcrumbsGenerator $breadcrumbs) {
87
            $breadcrumbs->push($this->module->title(), route('scaffold.index', [
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

87
            $breadcrumbs->push($this->module->title(), /** @scrutinizer ignore-call */ route('scaffold.index', [
Loading history...
88
                'module' => $this->module->url(),
89
            ]));
90
        });
91
    }
92
93
    /**
94
     * Render `Edit page` breadcrumbs.
95
     */
96
    protected function edit()
97
    {
98
        $this->index();
99
100
        $this->manager->register('edit', function (BreadcrumbsGenerator $breadcrumbs) {
101
            $breadcrumbs->parent('index');
102
103
            $breadcrumbs->push(trans('administrator::module.action.edit', [
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

103
            $breadcrumbs->push(/** @scrutinizer ignore-call */ trans('administrator::module.action.edit', [
Loading history...
104
                'resource' => $this->module->singular(),
0 ignored issues
show
Bug introduced by
The method singular() does not exist on Terranet\Administrator\Contracts\Module. Since it exists in all sub-types, consider adding an abstract or default implementation to Terranet\Administrator\Contracts\Module. ( Ignorable by Annotation )

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

104
                'resource' => $this->module->/** @scrutinizer ignore-call */ singular(),
Loading history...
105
                'instance' => $this->presentEloquent(),
106
            ]), null);
107
        });
108
    }
109
110
    /**
111
     * Render `Create page` breadcrumbs.
112
     */
113
    protected function create()
114
    {
115
        $this->index();
116
117
        $this->manager->register('create', function (BreadcrumbsGenerator $breadcrumbs) {
118
            $breadcrumbs->parent('index');
119
            $breadcrumbs->push(trans('administrator::module.action.create', [
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

119
            $breadcrumbs->push(/** @scrutinizer ignore-call */ trans('administrator::module.action.create', [
Loading history...
120
                'resource' => $this->module->singular(),
121
            ]), null);
122
        });
123
    }
124
125
    /**
126
     * Render `View page` breadcrumbs.
127
     */
128
    protected function view()
129
    {
130
        $this->index();
131
132
        $this->manager->register('view', function (BreadcrumbsGenerator $breadcrumbs) {
133
            $breadcrumbs->parent('index');
134
            $breadcrumbs->push(trans('administrator::module.action.view', [
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

134
            $breadcrumbs->push(/** @scrutinizer ignore-call */ trans('administrator::module.action.view', [
Loading history...
135
                'resource' => $this->module->singular(),
136
                'instance' => $this->presentEloquent(),
137
            ]), route('scaffold.view', ['module' => $this->module->url(), 'id' => app('scaffold.model')->getKey()]));
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

137
            ]), route('scaffold.view', ['module' => $this->module->url(), 'id' => /** @scrutinizer ignore-call */ app('scaffold.model')->getKey()]));
Loading history...
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

137
            ]), /** @scrutinizer ignore-call */ route('scaffold.view', ['module' => $this->module->url(), 'id' => app('scaffold.model')->getKey()]));
Loading history...
138
        });
139
    }
140
141
    /**
142
     * @return null|string
143
     */
144
    protected function presentEloquent(): ?string
145
    {
146
        if (!$model = app('scaffold.model')) {
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

146
        if (!$model = /** @scrutinizer ignore-call */ app('scaffold.model')) {
Loading history...
147
            $model = app('scaffold.module')->model();
148
        }
149
150
        return (new EloquentPresenter($model))->present();
151
    }
152
}
153