BreadcrumbsComposer::parseSegments()   D
last analyzed

Complexity

Conditions 19
Paths 1

Size

Total Lines 41
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 41
rs 4.5166
cc 19
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
14
class BreadcrumbsComposer
15
{
16
    /**
17
     * The request instance.
18
     *
19
     * @var \Illuminate\Http\Request
20
     */
21
    protected $request;
22
23
    /**
24
     * Initialize a new composer instance.
25
     *
26
     * @param  \Illuminate\Http\Request  $request
27
     * @return void
28
     */
29
    public function __construct(Request $request)
30
    {
31
        $this->request = $request;
32
    }
33
34
    /**
35
     * Bind data to the view.
36
     *
37
     * @param  \Illuminate\View\View  $view
38
     * @return void
39
     */
40
    public function compose(View $view)
41
    {
42
        $segments = $this->parseSegments();
43
        $breadcrumbs_lang = Lang::get('common/breadcrumbs');
44
        $portal_specific_lang = [];
45
46
        if (WhichPortal::isManagerPortal()) {
47
            $segments = $segments->slice(1);
48
            $portal_specific_lang = $breadcrumbs_lang['managerPortal'];
49
        } elseif (WhichPortal::isHrPortal()) {
50
            $segments = $segments->slice(1);
51
            $portal_specific_lang = $breadcrumbs_lang['hrPortal'];
52
        }
53
54
        $view->with('breadcrumbs', $segments);
55
        $view->with('breadcrumbs_lang', $breadcrumbs_lang);
56
        $view->with('portal_specific_lang', $portal_specific_lang);
57
    }
58
59
    /**
60
     * Parse the request route segments.
61
     *
62
     * @return \Illuminate\Support\Collection
63
     */
64
    protected function parseSegments()
65
    {
66
        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

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