Completed
Push — master ( 8f8ed1...dca6d3 )
by Shcherbak
01:55
created

RandomString::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Funivan\CabbageCore\String\StringObject\Random;
6
7
use Funivan\CabbageCore\String\StringList\StringListInterface;
8
use Funivan\CabbageCore\String\StringObject\StringInterface;
9
10
class RandomString implements StringInterface
11
{
12
    /**
13
     * @var StringListInterface
14
     */
15
    private $list;
16
17 1
    public function __construct(StringListInterface $list)
18
    {
19 1
        $this->list = $list;
20 1
    }
21
22 1
    public function value(): string
23
    {
24 1
        $items = \iterator_to_array($this->list->all());
25 1
        shuffle($items);
26 1
        $result = reset($items);
27 1
        if (!is_string($result)) {
28
            throw new \RuntimeException('Can not fetch random string from the list');
29
        }
30 1
        return $result;
31
    }
32
}
33