Completed
Push — master ( 1d6670...2e71c9 )
by Carsten
05:01 queued 10s
created

DevelopingFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 4
dl 0
loc 66
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
B __invoke() 0 27 7
1
<?php
2
namespace FilmTools\Developing;
3
4
use FilmTools\Commons\Zones;
5
use FilmTools\Commons\FStops;
6
7
class DevelopingFactory
8
{
9
10
    /**
11
     * PHP class name (FQDN) of the DevelopingInterface instance this factory produces.
12
     *
13
     * @var string
14
     */
15
    public $developing_php_class;
16
17
18
    /**
19
     * @param string|null $developing_php_class DevelopingInterface instance FQDN
20
     *
21
     * @throws InvalidArgumentException If FQDN does not implement DevelopingInterface
22
     */
23 30
    public function __construct( string $developing_php_class = null )
24
    {
25 30
        $this->developing_php_class = $developing_php_class ?: Developing::class;
26
27 30
        if (!is_subclass_of($this->developing_php_class, DevelopingInterface::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\Developing\DevelopingInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
28 3
            $msg = sprintf("Class name must implement '%s'.", DevelopingInterface::class);
29 3
            throw new DevelopingInvalidArgumentException( $msg );
30
        endif;
31 27
    }
32
33
34
    /**
35
     * The factory method.
36
     *
37
     * Expects an array with at least elements "time", "densities", and "exposures".
38
     *
39
     * If no "exposures" are given, but "zones" numbers are instead, the zone numbers
40
     * will be converted internally.
41
     *
42
     * @param  array $developing
43
     * @return DevelopingInterface
44
     */
45 27
    public function __invoke( $developing )
46
    {
47 27
        $densities = $developing['densities'] ?? array();
48 27
        $fstops    = $developing['fstops']     ?? array();
49 27
        $zones     = $developing['zones']     ?? array();
50 27
        $exposures = $developing['exposures'] ?? array();
51
52 27
        if (!array_key_exists("time", $developing))
53 3
            throw new NoTimeGivenException("The data array must contain a 'time' element.");
54
55 24
        $time = $developing['time'] ?? false;
56 24
        if (filter_var($time, \FILTER_VALIDATE_INT, [
57 24
            'options' => array( 'min_range' => 0 )
58 24
        ]) === false)
59 12
            throw new NoTimeGivenException("The developing time must be integer (positive or 0).");
60
61 12
        if (empty($exposures) and !empty($zones)):
62 6
            $exposures = new Zones( $zones );
63
        endif;
64
65 12
        if (empty($exposures) and !empty($fstops)):
66 3
            $exposures = new FStops( $fstops );
67
        endif;
68
69 12
        $developing_php_class = $this->developing_php_class;
70 12
        return new $developing_php_class( $exposures, $densities, $time );
71
    }
72
}
73