TicketRepository   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 43.27 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 45
loc 104
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A applyTicket() 25 25 4
A getByTicket() 9 9 4
A invalidTicket() 0 4 1
A getAvailableTicket() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: leo108
5
 * Date: 16/9/17
6
 * Time: 20:19
7
 */
8
9
namespace Leo108\CAS\Repositories;
10
11
use Carbon\Carbon;
12
use Leo108\CAS\Contracts\Models\UserModel;
13
use Leo108\CAS\Exceptions\CAS\CasException;
14
use Leo108\CAS\Models\Ticket;
15
use Leo108\CAS\Services\TicketGenerator;
16
17
class TicketRepository
18
{
19
    /**
20
     * @var Ticket
21
     */
22
    protected $ticket;
23
24
    /**
25
     * @var ServiceRepository
26
     */
27
    protected $serviceRepository;
28
29
    /**
30
     * @var TicketGenerator
31
     */
32
    protected $ticketGenerator;
33
34
    /**
35
     * TicketRepository constructor.
36
     * @param Ticket            $ticket
37
     * @param ServiceRepository $serviceRepository
38
     * @param TicketGenerator   $ticketGenerator
39
     */
40 19
    public function __construct(Ticket $ticket, ServiceRepository $serviceRepository, TicketGenerator $ticketGenerator)
41
    {
42 19
        $this->ticket            = $ticket;
43 19
        $this->serviceRepository = $serviceRepository;
44 19
        $this->ticketGenerator   = $ticketGenerator;
45 19
    }
46
47
    /**
48
     * @param UserModel $user
49
     * @param string    $serviceUrl
50
     * @param array     $proxies
51
     * @throws CasException
52
     * @return Ticket
53
     */
54 1 View Code Duplication
    public function applyTicket(UserModel $user, $serviceUrl, $proxies = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
55
    {
56 1
        $service = $this->serviceRepository->getServiceByUrl($serviceUrl);
57 1
        if (!$service) {
58 1
            throw new CasException(CasException::INVALID_SERVICE);
59
        }
60 1
        $ticket = $this->getAvailableTicket(config('cas.ticket_len', 32), empty($proxies) ? 'ST-' : 'PT-');
61 1
        if ($ticket === false) {
62 1
            throw new CasException(CasException::INTERNAL_ERROR, 'apply ticket failed');
63
        }
64 1
        $record = $this->ticket->newInstance(
65
            [
66 1
                'ticket'      => $ticket,
67 1
                'expire_at'   => new Carbon(sprintf('+%dsec', config('cas.ticket_expire', 300))),
68 1
                'created_at'  => new Carbon(),
69 1
                'service_url' => $serviceUrl,
70 1
                'proxies'     => $proxies,
71
            ]
72
        );
73 1
        $record->user()->associate($user->getEloquentModel());
74 1
        $record->service()->associate($service);
75 1
        $record->save();
76
77 1
        return $record;
78
    }
79
80
    /**
81
     * @param string $ticket
82
     * @param bool   $checkExpired
83
     * @return null|Ticket
84
     */
85 1 View Code Duplication
    public function getByTicket($ticket, $checkExpired = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
86
    {
87 1
        $record = $this->ticket->where('ticket', $ticket)->first();
88 1
        if (!$record) {
89 1
            return null;
90
        }
91
92 1
        return ($checkExpired && $record->isExpired()) ? null : $record;
93
    }
94
95
    /**
96
     * @param Ticket $ticket
97
     * @return bool|null
98
     */
99 1
    public function invalidTicket(Ticket $ticket)
100
    {
101 1
        return $ticket->delete();
102
    }
103
104
    /**
105
     * @param integer $totalLength
106
     * @param string  $prefix
107
     * @return string|false
108
     */
109 1 View Code Duplication
    protected function getAvailableTicket($totalLength, $prefix)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
110
    {
111 1
        return $this->ticketGenerator->generate(
112 1
            $totalLength,
113 1
            $prefix,
114
            function ($ticket) {
115 1
                return is_null($this->getByTicket($ticket, false));
116 1
            },
117 1
            10
118
        );
119
    }
120
}
121