Passed
Push — master ( 1b6475...610123 )
by Petr
10:18
created

Random::generateRandomText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\UploadPerPartes\Keys;
4
5
6
use kalanis\UploadPerPartes\Uploader\TargetSearch;
7
8
9
/**
10
 * Class Random
11
 * @package kalanis\UploadPerPartes\Keys
12
 * Connect shared key and local which has been generated by as random string
13
 */
14
class Random extends AKey
15
{
16
    /** @var int */
17
    protected $keyLength = 64;
18
19
    /** @var string[] */
20
    public static $possibilities = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
21
22 1
    public function fromSharedKey(string $key): string
23
    {
24 1
        return $key . TargetSearch::FILE_DRIVER_SUFF;
25
    }
26
27
    /**
28
     * @return AKey
29
     * @codeCoverageIgnore do you really want to check random key generator?
30
     */
31
    public function generateKeys(): parent
32
    {
33
        $this->sharedKey = $this->generateRandomText($this->keyLength, static::$possibilities);
34
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type kalanis\UploadPerPartes\Keys\Random which is incompatible with the type-hinted return parent.
Loading history...
35
    }
36
37
    /**
38
     * @param int $length
39
     * @param string[] $possibilities
40
     * @return string
41
     */
42 1
    public static function generateRandomText(int $length, array $possibilities): string
43
    {
44 1
        $string = '';
45 1
        for ($i = 0; $i < $length; $i++) {
46 1
            $rand = mt_rand(0, count($possibilities) - 1);
47 1
            $string .= $possibilities[$rand];
48
        }
49 1
        return $string;
50
    }
51
}
52