TicketGenerator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 13
cts 13
cp 1
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
     * @param integer  $totalLength
17
     * @param string   $prefix
18
     * @param callable $checkFunc
19
     * @param integer  $maxRetry
20
     * @return string|false
21
     */
22 2
    public function generate($totalLength, $prefix, callable $checkFunc, $maxRetry)
23
    {
24 2
        $ticket = false;
25 2
        $flag   = false;
26 2
        for ($i = 0; $i < $maxRetry; $i++) {
27 2
            $ticket = $this->generateOne($totalLength, $prefix);
28 2
            if (call_user_func_array($checkFunc, [$ticket])) {
29 1
                $flag = true;
30 1
                break;
31
            }
32
        }
33
34 2
        if (!$flag) {
35 1
            return false;
36
        }
37
38 1
        return $ticket;
39
    }
40
41
    /**
42
     * @param integer $totalLength
43
     * @param string  $prefix
44
     * @return string
45
     */
46 1
    public function generateOne($totalLength, $prefix)
47
    {
48 1
        return $prefix.Str::random($totalLength - strlen($prefix));
49
    }
50
}
51