DensitiesFactory::fromValues()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.9
cc 3
nc 3
nop 1
crap 3
1
<?php
2
namespace FilmTools\Commons;
3
4
5
class DensitiesFactory
6
{
7
8
    /**
9
     * @var string
10
     */
11
    public $php_densities_class;
12
13
14
    /**
15
     * @param string|null $php_densities_class
16
     */
17 21
    public function __construct( string $php_densities_class = null )
18
    {
19 21
        $this->php_densities_class = $php_densities_class ?: Densities::class;
20
21 21
        if (!is_subclass_of($this->php_densities_class, DensitiesInterface::class ))
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \FilmTools\Commons\DensitiesInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
22 3
            throw new FilmToolsInvalidArgumentException("Class name must implement DensitiesInterface.");
23
24 18
    }
25
26
27
    /**
28
     * @param  array  $data
29
     * @return DensitiesInterface
30
     */
31 18
    public function fromValues( $data ) : DensitiesInterface
32
    {
33 18
        if ($data instanceOf \Traversable )
34 6
            $data = iterator_to_array( $data );
35 12
        else if (!is_array($data))
36 9
            throw new FilmToolsInvalidArgumentException("Array or Traversable expected");
37
38
39 9
        $php_class = $this->php_densities_class;
40 9
        return new $php_class( array_values( $data ));
41
    }
42
43
}
44