Completed
Branch proxy (a8ed88)
by leo
08:29
created

TicketGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 18 4
A generateOne() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: leo108
5
 * Date: 2016/10/26
6
 * Time: 10:22
7
 */
8
9
namespace Leo108\CAS\Services;
10
11
use Illuminate\Support\Str;
12
13
class TicketGenerator
14
{
15
16
    /**
17
     * @param integer  $totalLength
18
     * @param string   $prefix
19
     * @param callable $checkFunc
20
     * @param integer  $maxRetry
21
     * @return string|false
22
     */
23
    public function generate($totalLength, $prefix, callable $checkFunc, $maxRetry)
24
    {
25
        $ticket = false;
26
        $flag   = false;
27
        for ($i = 0; $i < $maxRetry; $i++) {
28
            $ticket = $this->generateOne($totalLength, $prefix);
29
            if (!call_user_func_array($checkFunc, [$ticket])) {
30
                $flag = true;
31
                break;
32
            }
33
        }
34
35
        if (!$flag) {
36
            return false;
37
        }
38
39
        return $ticket;
40
    }
41
42
    public function generateOne($totalLength, $prefix)
43
    {
44
        return $prefix.Str::random($totalLength - strlen($prefix));
45
    }
46
}
47