Completed
Push — develop ( a66ac3...261059 )
by Carsten
17:30 queued 09:59
created

DevelopingFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 59
c 0
b 0
f 0
wmc 8
lcom 0
cbo 3
ccs 19
cts 19
cp 1
rs 10

2 Methods

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