AgentLayout::inbox()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\ViewComposers;
4
5
use App\Model\helpdesk\Agent\Department;
6
use App\Model\helpdesk\Settings\Company;
7
use App\Model\helpdesk\Ticket\Tickets;
8
use App\User;
9
use Auth;
10
use Illuminate\View\View;
11
12
class AgentLayout
13
{
14
    /**
15
     * The user repository implementation.
16
     *
17
     * @var UserRepository
18
     */
19
    protected $company;
20
    protected $users;
21
    protected $tickets;
22
    protected $department;
23
24
    /**
25
     * Create a new profile composer.
26
     *
27
     * @param
28
     *
29
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     */
31
    public function __construct(Company $company, User $users, Tickets $tickets, Department $department)
32
    {
33
        $this->company = $company;
0 ignored issues
show
Documentation Bug introduced by
It seems like $company of type object<App\Model\helpdesk\Settings\Company> is incompatible with the declared type object<App\Http\ViewComposers\UserRepository> of property $company.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
        $this->auth = Auth::user();
0 ignored issues
show
Bug introduced by
The property auth does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
        $this->users = $users;
36
        $this->tickets = $tickets;
37
        $this->department = $department;
38
    }
39
40
    /**
41
     * Bind data to the view.
42
     *
43
     * @param View $view
44
     *
45
     * @return void
46
     */
47
    public function compose(View $view)
48
    {
49
        $notifications = \App\Http\Controllers\Common\NotificationController::getNotifications();
50
        $view->with([
51
            'company'         => $this->company,
52
            'notifications'   => $notifications,
53
            'myticket'        => $this->myTicket(),
54
            'unassigned'      => $this->unassigned(),
55
            'followup_ticket' => $this->followupTicket(),
56
            'deleted'         => $this->deleted(),
57
            'tickets'         => $this->inbox(),
58
            'department'      => $this->departments(),
59
            'overdues'        => $this->overdues(),
60
            'due_today'       => $this->getDueToday(),
61
        ]);
62
    }
63
64
    public function users()
65
    {
66
        return $this->users->select('id', 'profile_pic');
0 ignored issues
show
Documentation Bug introduced by
The method select does not exist on object<App\User>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
67
    }
68
69
    public function tickets()
70
    {
71
        return $this->tickets->select('id', 'ticket_number');
0 ignored issues
show
Documentation Bug introduced by
The method select does not exist on object<App\Model\helpdesk\Ticket\Tickets>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
72
    }
73
74
    public function departments()
75
    {
76
        $array = [];
0 ignored issues
show
Unused Code introduced by
$array is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
        $tickets = $this->tickets;
78
        if (\Auth::user()->role == 'agent') {
79
            $tickets = $tickets->where('tickets.dept_id', '=', \Auth::user()->primary_dpt);
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\helpdesk\Ticket\Tickets>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
        }
81
        $tickets = $tickets
82
                ->leftJoin('department as dep', 'tickets.dept_id', '=', 'dep.id')
83
                ->leftJoin('ticket_status', 'tickets.status', '=', 'ticket_status.id')
84
                ->select('dep.name as name', 'ticket_status.name as status', \DB::raw('COUNT(ticket_status.name) as count'))
85
                ->groupBy('dep.name', 'ticket_status.name')
86
                ->get();
87
        $grouped = $tickets->groupBy('name');
88
        $status = [];
89
        foreach ($grouped as $key => $group) {
90
            $status[$key] = $group->keyBy('status');
91
        }
92
93
        return collect($status);
94
    }
95
96
    public function myTicket()
97
    {
98
        $ticket = $this->tickets();
99
        if ($this->auth->role == 'admin') {
100
            return $ticket->where('assigned_to', $this->auth->id)
101
                    ->where('status', '1');
102
        } elseif ($this->auth->role == 'agent') {
103
            return $ticket->where('assigned_to', $this->auth->id)
104
                    ->where('status', '1');
105
        }
106
    }
107
108
    public function unassigned()
109
    {
110
        $ticket = $this->tickets();
111
        if ($this->auth->role == 'admin') {
112
            return $ticket->where('assigned_to', '=', null)
113
                    ->where('status', '=', '1')
114
                    ->select('id');
115
        } elseif ($this->auth->role == 'agent') {
116
            return $ticket->where('assigned_to', '=', null)
117
                    ->where('status', '=', '1')
118
                    ->where('dept_id', '=', $this->auth->primary_dpt)
119
                    ->select('id');
120
        }
121
    }
122
123
    public function followupTicket()
124
    {
125
        $ticket = $this->tickets();
126
        if ($this->auth->role == 'admin') {
127
            return $ticket->where('status', '1')->where('follow_up', '1')->select('id');
128
        } elseif ($this->auth->role == 'agent') {
129
            return $ticket->where('status', '1')->where('follow_up', '1')->select('id');
130
        }
131
    }
132
133
    public function deleted()
134
    {
135
        $ticket = $this->tickets();
136
        if ($this->auth->role == 'admin') {
137
            return $ticket->where('status', '5')->select('id');
138
        } elseif ($this->auth->role == 'agent') {
139
            return $ticket->where('status', '5')->where('dept_id', '=', $this->auth->primary_dpt)
140
                    ->select('id');
141
        }
142
    }
143
144
    public function inbox()
145
    {
146
        $table = $this->tickets();
147
        if (Auth::user()->role == 'agent') {
148
            $id = Auth::user()->primary_dpt;
149
            $table = $table->where('tickets.dept_id', '=', $id)->orWhere('assigned_to', '=', Auth::user()->id);
150
        }
151
152
        return $table->Join('ticket_status', function ($join) {
153
            $join->on('ticket_status.id', '=', 'tickets.status')
154
                        ->whereIn('ticket_status.id', [1, 7]);
155
        });
156
    }
157
158
    public function overdues()
159
    {
160
        $ticket = $this->tickets();
161
        if ($this->auth->role == 'admin') {
162
            return $ticket->where('status', '=', 1)
163
                            ->where('isanswered', '=', 0)
164
                            ->whereNotNull('tickets.duedate')
165
                            ->where('tickets.duedate', '!=', '00-00-00 00:00:00')
166
                            ->where('tickets.duedate', '<', \Carbon\Carbon::now())
167
                            ->select('tickets.id');
168 View Code Duplication
        } elseif ($this->auth->role == 'agent') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
            return $ticket->where('status', '=', 1)
170
                            ->where('isanswered', '=', 0)
171
                            ->whereNotNull('tickets.duedate')
172
                            ->where('dept_id', '=', $this->auth->primary_dpt)
173
                            ->where('tickets.duedate', '!=', '00-00-00 00:00:00')
174
                            ->where('tickets.duedate', '<', \Carbon\Carbon::now())
175
                            ->select('tickets.id');
176
        }
177
    }
178
179
    public function getDueToday()
180
    {
181
        $ticket = $this->tickets();
182
        if ($this->auth->role == 'admin') {
183
            return $ticket->where('status', '=', 1)
184
                            ->where('status', '=', 1)
185
                            ->where('isanswered', '=', 0)
186
                            ->whereNotNull('duedate')
187
                            ->whereRaw('date(duedate) = ?', [date('Y-m-d')]);
188 View Code Duplication
        } elseif ($this->auth->role == 'agent') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
            return $ticket->where('status', '=', 1)
190
                            ->where('status', '=', 1)
191
                            ->where('isanswered', '=', 0)
192
                            ->whereNotNull('duedate')
193
                            ->where('dept_id', '=', $this->auth->primary_dpt)
194
                            ->whereRaw('date(duedate) = ?', [date('Y-m-d')]);
195
        }
196
    }
197
}
198