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

Random   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 36
ccs 8
cts 11
cp 0.7272
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateRandomText() 0 8 2
A fromSharedKey() 0 3 1
A generateKeys() 0 4 1
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