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 = []) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.