Passed
Push — master ( 3948f8...d3c224 )
by Dāvis
07:09
created

Generator::generateFromPattern()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 24
nop 3
dl 0
loc 27
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Utils;
4
5
use RandomLib\Generator as RandomGenerator;
0 ignored issues
show
Bug introduced by
The type RandomLib\Generator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use RandomLib\Factory;
0 ignored issues
show
Bug introduced by
The type RandomLib\Factory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SecurityLib\Strength;
0 ignored issues
show
Bug introduced by
The type SecurityLib\Strength was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
/**
10
 * @link    https://github.com/gajus/paggern for the canonical source repository
11
 * @license https://github.com/gajus/paggern/blob/master/LICENSE BSD 3-Clause
12
 */
13
class Generator
14
{
15
    private $generator;
16
17
    /**
18
     * @param RandomLib\Generator $generator
0 ignored issues
show
Bug introduced by
The type Sludio\HelperBundle\Scri...ils\RandomLib\Generator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
     */
20
    public function __construct(RandomGenerator $generator = null)
21
    {
22
        if ($generator === null) {
23
            $factory = new Factory;
24
            $this->generator = $factory->getGenerator(new Strength(Strength::MEDIUM));
25
        }
26
    }
27
28
    /**
29
     * Generate a set of random codes based on Paggern pattern.
30
     * Codes are guaranteed to be unique within the set.
31
     *
32
     * @param string $pattern   Paggern pattern.
33
     * @param int    $amount    Number of codes to generate.
34
     * @param int    $safeguard Number of additional codes generated in case there are duplicates that need to be replaced.
35
     *
36
     * @return array
37
     */
38
    public function generateFromPattern($pattern, $amount = 1, $safeguard = 100)
39
    {
40
        $lexer = new \Gajus\Paggern\Lexer();
0 ignored issues
show
Bug introduced by
The type Gajus\Paggern\Lexer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
        $tokens = $lexer->tokenise($pattern, true);
42
        $codes = array_fill(0, $amount + $safeguard, '');
43
        foreach ($tokens as &$token) {
44
            if ($token['type'] !== 'literal') {
45
                $token['pool'] = $this->generator->generateString($token['repetition'] * ($amount + $safeguard), $token['haystack']);
46
            }
47
            unset($token);
48
        }
49
        foreach ($codes as $i => &$code) {
50
            foreach ($tokens as $token) {
51
                if ($token['type'] === 'literal') {
52
                    $code .= $token['string'];
53
                } else {
54
                    $code .= mb_substr($token['pool'], $token['repetition'] * $i, $token['repetition']);
55
                }
56
            }
57
            unset($code);
58
        }
59
        $codes = array_slice(array_unique($codes), 0, $amount);
60
        if (count($codes) < $amount) {
61
            throw new Exception\RuntimeException('Unique combination pool exhausted.');
0 ignored issues
show
Bug introduced by
The type Sludio\HelperBundle\Scri...eption\RuntimeException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
62
        }
63
64
        return $codes;
65
    }
66
}
67