PGTicketRepository   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 43.81 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 46
loc 105
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getByTicket() 9 9 4
A invalidTicketByUser() 0 4 1
A applyTicket() 26 26 4
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: 2016/10/25
6
 * Time: 16:34
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\PGTicket;
15
use Leo108\CAS\Services\TicketGenerator;
16
17
class PGTicketRepository
18
{
19
    /**
20
     * @var PGTicket
21
     */
22
    protected $pgTicket;
23
    /**
24
     * @var ServiceRepository
25
     */
26
    protected $serviceRepository;
27
28
    /**
29
     * @var TicketGenerator
30
     */
31
    protected $ticketGenerator;
32
33
    /**
34
     * PGTicketRepository constructor.
35
     * @param PGTicket          $pgTicket
36
     * @param ServiceRepository $serviceRepository
37
     * @param TicketGenerator   $ticketGenerator
38
     */
39 30
    public function __construct(
40
        PGTicket $pgTicket,
41
        ServiceRepository $serviceRepository,
42
        TicketGenerator $ticketGenerator
43
    ) {
44 30
        $this->pgTicket          = $pgTicket;
45 30
        $this->serviceRepository = $serviceRepository;
46 30
        $this->ticketGenerator   = $ticketGenerator;
47 30
    }
48
49
    /**
50
     * @param string $ticket
51
     * @param bool   $checkExpired
52
     * @return null|PGTicket
53
     */
54 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...
55
    {
56 1
        $record = $this->pgTicket->where('ticket', $ticket)->first();
57 1
        if (!$record) {
58 1
            return null;
59
        }
60
61 1
        return ($checkExpired && $record->isExpired()) ? null : $record;
62
    }
63
64
    /**
65
     * @param UserModel $user
66
     */
67 1
    public function invalidTicketByUser(UserModel $user)
68
    {
69 1
        $this->pgTicket->where('user_id', $user->getEloquentModel()->getKey())->delete();
70 1
    }
71
72
    /**
73
     * @param UserModel $user
74
     * @param string    $pgtUrl
75
     * @param array     $proxies
76
     * @return PGTicket
77
     * @throws CasException
78
     */
79 4 View Code Duplication
    public function applyTicket(UserModel $user, $pgtUrl, $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...
80
    {
81 4
        $service = $this->serviceRepository->getServiceByUrl($pgtUrl);
82 4
        if (!$service || !$service->allow_proxy) {
83 2
            throw new CasException(CasException::UNAUTHORIZED_SERVICE_PROXY);
84
        }
85
86 2
        $ticket = $this->getAvailableTicket(config('cas.pg_ticket_len', 64));
87 2
        if ($ticket === false) {
88 1
            throw new CasException(CasException::INTERNAL_ERROR, 'apply proxy-granting ticket failed');
89
        }
90 1
        $record = $this->pgTicket->newInstance(
91
            [
92 1
                'ticket'     => $ticket,
93 1
                'expire_at'  => new Carbon(sprintf('+%dsec', config('cas.pg_ticket_expire', 7200))),
94 1
                'created_at' => new Carbon(),
95 1
                'pgt_url'    => $pgtUrl,
96 1
                'proxies'    => $proxies,
97
            ]
98
        );
99 1
        $record->user()->associate($user->getEloquentModel());
100 1
        $record->service()->associate($service);
101 1
        $record->save();
102
103 1
        return $record;
104
    }
105
106
    /**
107
     * @param string $totalLength
108
     * @return string|false
109
     */
110 1 View Code Duplication
    protected function getAvailableTicket($totalLength)
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...
111
    {
112 1
        return $this->ticketGenerator->generate(
113 1
            $totalLength,
114 1
            'PGT-',
115
            function ($ticket) {
116 1
                return is_null($this->getByTicket($ticket, false));
117 1
            },
118 1
            10
119
        );
120
    }
121
}
122