Passed
Push — task/refactor-breadcrumbs ( 91a962...bee80d )
by Yonathan
04:22
created

BreadcrumbsComposer::parseSegments()   C

Complexity

Conditions 17
Paths 1

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 38
rs 5.2166
c 2
b 0
f 0
cc 17
nc 1
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\ViewComposers;
4
5
use App\Models\Applicant;
6
use App\Models\JobApplication;
7
use App\Models\JobPoster;
8
use App\Models\Manager;
9
use Facades\App\Services\WhichPortal;
10
use Illuminate\View\View;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Facades\Lang;
13
use Illuminate\Support\Facades\Log;
14
15
class BreadcrumbsComposer
16
{
17
    /**
18
     * The request instance.
19
     *
20
     * @var \Illuminate\Http\Request
21
     */
22
    protected $request;
23
24
    /**
25
     * Initialize a new composer instance.
26
     *
27
     * @param  \Illuminate\Http\Request  $request
28
     * @return void
29
     */
30
    public function __construct(Request $request)
31
    {
32
        $this->request = $request;
33
    }
34
35
    /**
36
     * Bind data to the view.
37
     *
38
     * @param  \Illuminate\View\View  $view
39
     * @return void
40
     */
41
    public function compose(View $view)
42
    {
43
        $segments = $this->parseSegments();
44
        $breadcrumbs_lang = Lang::get('common/breadcrumbs');
45
        $portal_specific_lang = [];
46
47
        if (WhichPortal::isManagerPortal()) {
48
            $segments = $segments->slice(1);
49
            $portal_specific_lang = $breadcrumbs_lang['managerPortal'];
50
        } elseif (WhichPortal::isHrPortal()) {
51
            $segments = $segments->slice(1);
52
            $portal_specific_lang = $breadcrumbs_lang['hrPortal'];
53
        }
54
55
        $view->with('breadcrumbs', $segments);
56
        $view->with('breadcrumbs_lang', $breadcrumbs_lang);
57
        $view->with('portal_specific_lang', $portal_specific_lang);
58
    }
59
60
    /**
61
     * Parse the request route segments.
62
     *
63
     * @return \Illuminate\Support\Collection
64
     */
65
    protected function parseSegments()
66
    {
67
        return collect($this->request->segments())->mapWithKeys(function ($segment, $key) {
0 ignored issues
show
Bug introduced by
The function collect 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

67
        return /** @scrutinizer ignore-call */ collect($this->request->segments())->mapWithKeys(function ($segment, $key) {
Loading history...
68
            // Replaces any segment ID in url with the objects name or title.
69
            if ($this->request->jobPoster === $segment) {
70
                $poster = JobPoster::find($this->request->jobPoster);
71
                if ($poster !== null) {
72
                    $segment = $poster->title;
73
                }
74
            } elseif (is_object($this->request->jobPoster) && $this->request->jobPoster->id == $segment) {
75
                $segment = $this->request->jobPoster->title;
76
            }
77
            if ($this->request->manager === $segment) {
78
                $poster = Manager::find($this->request->manager);
79
                if ($poster !== null) {
80
                    $segment = $poster->title;
81
                }
82
            } elseif (is_object($this->request->manager) && $this->request->manager->id == $segment) {
83
                $segment = $this->request->manager->title;
84
            }
85
            if ($this->request->applicant === $segment) {
86
                $poster = Applicant::find($this->request->applicant);
87
                if ($poster !== null) {
88
                    $segment = $poster->title;
89
                }
90
            } elseif (is_object($this->request->applicant) && $this->request->applicant->id == $segment) {
91
                $segment = $this->request->applicant->title;
92
            }
93
            if ($this->request->application === $segment) {
94
                $poster = JobApplication::find($this->request->application);
95
                if ($poster !== null) {
96
                    $segment = $poster->title;
97
                }
98
            } elseif (is_object($this->request->application) && $this->request->application->id == $segment) {
99
                $segment = $this->request->application->title;
100
            }
101
            return [
102
                $segment => implode('/', array_slice($this->request->segments(), 0, $key + 1)),
103
            ];
104
        });
105
    }
106
}
107