Completed
Pull Request — master (#1)
by leo
03:23
created

PGTicketRepository::applyTicket()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 17

Duplication

Lines 26
Ratio 100 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 3
nop 3
dl 26
loc 26
ccs 18
cts 18
cp 1
crap 4
rs 8.5806
c 0
b 0
f 0
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();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Leo108\CAS\Models\PGTicket>? 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...
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();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Leo108\CAS\Models\PGTicket>? 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...
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 1
        );
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 1
            function ($ticket) {
116 1
                return is_null($this->getByTicket($ticket, false));
117 1
            },
118
            10
119 1
        );
120
    }
121
}
122