TicketControllable::close()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 25

Duplication

Lines 13
Ratio 52 %

Importance

Changes 0
Metric Value
dl 13
loc 25
rs 8.8977
c 0
b 0
f 0
cc 6
nc 5
nop 1
1
<?php
2
3
4
namespace RexlManu\LaravelTickets\Controllers;
5
6
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Http\RedirectResponse;
9
use Illuminate\Http\Request;
10
use Illuminate\Validation\Rule;
11
use Illuminate\View\View;
12
use RexlManu\LaravelTickets\Models\Ticket;
13
use RexlManu\LaravelTickets\Models\TicketMessage;
14
use RexlManu\LaravelTickets\Models\TicketReference;
15
use RexlManu\LaravelTickets\Models\TicketUpload;
16
use RexlManu\LaravelTickets\Rule\TicketReferenceRule;
17
use Symfony\Component\HttpFoundation\BinaryFileResponse;
18
19
/**
20
 * Class TicketController
21
 *
22
 * The main logic of the ticket system. All actions are performed here.
23
 *
24
 * If the accept header is json, the response will be a json response
25
 *
26
 * @package RexlManu\LaravelTickets\Controllers
27
 */
28
trait TicketControllable
29
{
30
31
    /**
32
     * @link TicketControllable constructor
33
     */
34
    public function __construct()
35
    {
36
        if (! config('laravel-tickets.permission')) {
37
            return;
38
        }
39
40
        $this->middleware(config('laravel-tickets.permissions.list-ticket'))->only('index');
0 ignored issues
show
Bug introduced by
It seems like middleware() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
41
        $this->middleware(config('laravel-tickets.permissions.create-ticket'))->only('store', 'create');
0 ignored issues
show
Bug introduced by
It seems like middleware() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
42
        $this->middleware(config('laravel-tickets.permissions.close-ticket'))->only('close');
0 ignored issues
show
Bug introduced by
It seems like middleware() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
43
        $this->middleware(config('laravel-tickets.permissions.show-ticket'))->only('show');
0 ignored issues
show
Bug introduced by
It seems like middleware() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
44
        $this->middleware(config('laravel-tickets.permissions.message-ticket'))->only('message');
0 ignored issues
show
Bug introduced by
It seems like middleware() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
        $this->middleware(config('laravel-tickets.permissions.download-ticket'))->only('download');
0 ignored issues
show
Bug introduced by
It seems like middleware() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
46
    }
47
48
    /**
49
     * Show every @return View|JsonResponse
50
     *
51
     * @link Ticket that the user has created
52
     *
53
     * If the accept header is json, the response will be a json response
54
     *
55
     */
56
    public function index()
57
    {
58
        if (\request()->user()->can(config('laravel-tickets.permissions.all-ticket'))) {
59
            $tickets = Ticket::query();
60
        } else {
61
            $tickets = request()->user()->tickets();
62
        }
63
        $tickets = $tickets->with('user')->orderBy('id', 'desc')->paginate(10);
64
65
        return request()->wantsJson() ?
66
            response()->json(compact('tickets')) :
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
            view('laravel-tickets::tickets.index',
68
                compact('tickets')
69
            );
70
    }
71
72
    /**
73
     * Show the create form
74
     *
75
     * @return View
76
     */
77
    public function create()
78
    {
79
        return view('laravel-tickets::tickets.create');
80
    }
81
82
    /**
83
     * Creates a @param Request $request the request
84
     *
85
     * @return View|JsonResponse|RedirectResponse
86
     * @link Ticket
87
     *
88
     */
89
    public function store(Request $request)
90
    {
91
        $rules = [
92
            'subject' => [ 'required', 'string', 'max:191' ],
93
            'priority' => [ 'required', Rule::in(config('laravel-tickets.priorities')) ],
94
            'message' => [ 'required', 'string' ],
95
            'files' => [ 'max:' . config('laravel-tickets.file.max-files') ],
96
            'files.*' => [
97
                'sometimes',
98
                'file',
99
                'max:' . config('laravel-tickets.file.size-limit'),
100
                'mimes:' . config('laravel-tickets.file.mimetype'),
101
            ],
102
        ];
103
        if (config('laravel-tickets.category')) {
104
            $rules[ 'category_id' ] = [
105
                'required',
106
                Rule::exists(config('laravel-tickets.database.ticket-categories-table'), 'id'),
107
            ];
108
        }
109
        if (config('laravel-tickets.references')) {
110
            $rules[ 'reference' ] = [
111
                config('laravel-tickets.references-nullable') ? 'nullable' : 'required',
112
                new TicketReferenceRule(),
113
            ];
114
        }
115
        $data = $request->validate($rules);
0 ignored issues
show
Bug introduced by
The call to validate() misses a required argument $...$params.

This check looks for function calls that miss required arguments.

Loading history...
116
        if ($request->user()->tickets()->where('state', '!=', 'CLOSED')->count() >= config('laravel-tickets.maximal-open-tickets')) {
117
            $message = trans('You have reached the limit of open tickets');
118
            return \request()->wantsJson() ?
119
                response()->json(compact('message')) :
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
120
                back()->with(
121
                    'message',
122
                    $message
123
                );
124
        }
125
        $ticket = $request->user()->tickets()->create(
126
            $data
127
        );
128
129
        if (array_key_exists('reference', $data)) {
130
            $reference = explode(',', $data[ 'reference' ]);
131
            $ticketReference = new TicketReference();
132
            $ticketReference->ticket()->associate($ticket);
133
            $ticketReference->referenceable()->associate(
134
                resolve($reference[ 0 ])->find($reference[ 1 ])
135
            );
136
            $ticketReference->save();
137
        }
138
139
        $ticketMessage = new TicketMessage($data);
140
        $ticketMessage->user()->associate($request->user());
141
        $ticketMessage->ticket()->associate($ticket);
142
        $ticketMessage->save();
143
144
        $this->handleFiles($data[ 'files' ] ?? [], $ticketMessage);
145
146
        $message = trans('The ticket was successfully created');
147
        return $request->wantsJson() ?
148
            response()->json(compact('message', 'ticket', 'ticketMessage')) :
149
            redirect(route(
150
                'laravel-tickets.tickets.show',
151
                compact('ticket')
152
            ))->with(
153
                'message',
154
                $message
155
            );
156
    }
157
158
    /**
159
     * Show detailed informations about the @param Ticket $ticket
160
     *
161
     * @return View|JsonResponse|RedirectResponse|void
162
     * @link Ticket and the informations
163
     *
164
     */
165
    public function show(Ticket $ticket)
166
    {
167 View Code Duplication
        if (! $ticket->user()->get()->contains(\request()->user()) &&
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...
168
            ! request()->user()->can(config('laravel-tickets.permissions.all-ticket'))) {
169
            return abort(403);
170
        }
171
172
        $messages = $ticket->messages()->with([ 'user', 'uploads' ])->orderBy('created_at', 'desc');
173
174
        return \request()->wantsJson() ?
175
            response()->json(compact(
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
176
                'ticket',
177
                'messages'
178
            )) :
179
            view('laravel-tickets::tickets.show',
180
                compact(
181
                    'ticket',
182
                    'messages'
183
                )
184
            );
185
    }
186
187
    /**
188
     * Send a message to the @param Request $request
189
     *
190
     * @param Ticket $ticket
191
     *
192
     * @return JsonResponse|RedirectResponse|void
193
     * @link Ticket
194
     *
195
     */
196
    public function message(Request $request, Ticket $ticket)
197
    {
198 View Code Duplication
        if (! $ticket->user()->get()->contains(\request()->user()) &&
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...
199
            ! request()->user()->can(config('laravel-tickets.permissions.all-ticket'))) {
200
            return abort(403);
201
        }
202
203 View Code Duplication
        if (! config('laravel-tickets.open-ticket-with-answer') && $ticket->state === 'CLOSED') {
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...
204
            $message = trans('You cannot reply to a closed ticket');
205
            return \request()->wantsJson() ?
206
                response()->json(compact('message')) :
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
207
                back()->with(
208
                    'message',
209
                    $message
210
                );
211
        }
212
213
        $data = $request->validate([
0 ignored issues
show
Bug introduced by
The call to validate() misses a required argument $...$params.

This check looks for function calls that miss required arguments.

Loading history...
214
            'message' => [ 'required', 'string' ],
215
            'files' => [ 'max:' . config('laravel-tickets.file.max-files') ],
216
            'files.*' => [
217
                'sometimes',
218
                'file',
219
                'max:' . config('laravel-tickets.file.size-limit'),
220
                'mimes:' . config('laravel-tickets.file.mimetype'),
221
            ]
222
        ]);
223
224
        $ticketMessage = new TicketMessage($data);
225
        $ticketMessage->user()->associate($request->user());
226
        $ticketMessage->ticket()->associate($ticket);
227
        $ticketMessage->save();
228
229
        $this->handleFiles($data[ 'files' ] ?? [], $ticketMessage);
230
231
        $ticket->update([ 'state' => 'OPEN' ]);
232
233
        $message = trans('Your answer was sent successfully');
234
        return $request->wantsJson() ?
235
            response()->json(compact('message')) :
236
            back()->with(
237
                'message',
238
                $message
239
            );
240
    }
241
242
    /**
243
     * Declare the @param Ticket $ticket
244
     *
245
     * @return JsonResponse|RedirectResponse|void
246
     * @link Ticket as closed.
247
     *
248
     */
249
    public function close(Ticket $ticket)
250
    {
251 View Code Duplication
        if (! $ticket->user()->get()->contains(\request()->user()) &&
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...
252
            ! request()->user()->can(config('laravel-tickets.permissions.all-ticket'))) {
253
            return abort(403);
254
        }
255 View Code Duplication
        if ($ticket->state === 'CLOSED') {
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...
256
            $message = trans('The ticket is already closed');
257
            return \request()->wantsJson() ?
258
                response()->json(compact('message')) :
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
259
                back()->with(
260
                    'message',
261
                    $message
262
                );
263
        }
264
        $ticket->update([ 'state' => 'CLOSED' ]);
265
266
        $message = trans('The ticket was successfully closed');
267
        return \request()->wantsJson() ?
268
            response()->json(compact('message')) :
269
            back()->with(
270
                'message',
271
                $message
272
            );
273
    }
274
275
    /**
276
     * Downloads the file from @param Ticket $ticket
277
     *
278
     * @param TicketUpload $ticketUpload
279
     *
280
     * @return BinaryFileResponse
281
     * @link TicketUpload
282
     *
283
     */
284
    public function download(Ticket $ticket, TicketUpload $ticketUpload)
285
    {
286 View Code Duplication
        if (! $ticket->user()->get()->contains(\request()->user()) &&
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...
287
            ! request()->user()->can(config('laravel-tickets.permissions.all-ticket'))) {
288
            return abort(403);
289
        }
290
291
        $storagePath = storage_path('app/' . $ticketUpload->path);
292
        if (config('laravel-tickets.pdf-force-preview') && pathinfo($ticketUpload->path, PATHINFO_EXTENSION) === 'pdf') {
293
            return response()->file($storagePath);
0 ignored issues
show
Bug introduced by
The method file does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
294
        }
295
296
        return response()->download($storagePath);
0 ignored issues
show
Bug introduced by
The method download does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
297
    }
298
299
    /**
300
     * Handles the uploaded files for the @param $files array uploaded files
301
     *
302
     * @param TicketMessage $ticketMessage
303
     *
304
     * @link TicketMessage
305
     *
306
     */
307
    private function handleFiles($files, TicketMessage $ticketMessage)
308
    {
309
        if (! config('laravel-tickets.files') || $files == null) {
310
            return;
311
        }
312
        foreach ($files as $file) {
313
            $ticketMessage->uploads()->create([
314
                'path' => $file->storeAs(
315
                    config('laravel-tickets.file.path') . $ticketMessage->id,
316
                    $file->getClientOriginalName(),
317
                    config('laravel-tickets.file.driver')
318
                )
319
            ]);
320
        }
321
    }
322
323
}
324