Passed
Push — dev ( 2242bc...62ccfd )
by Fike
02:22
created

Converter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\Image\Projection\Framework;
4
5
use AmaTeam\Image\Projection\API\ConverterInterface;
6
use AmaTeam\Image\Projection\API\SpecificationInterface;
7
use AmaTeam\Image\Projection\API\Type\ReaderInterface;
8
use AmaTeam\Image\Projection\API\Conversion\FilterInterface;
9
use AmaTeam\Image\Projection\Type\Registry;
10
11
class Converter implements ConverterInterface
12
{
13
    /**
14
     * @var Registry
15
     */
16
    private $registry;
17
18
    /**
19
     * @param Registry $registry
20
     */
21
    public function __construct(Registry $registry)
22
    {
23
        $this->registry = $registry;
24
    }
25
26
    /**
27
     * @param SpecificationInterface $source
28
     * @param SpecificationInterface[] $targets
29
     * @param FilterInterface[] $filters
30
     * @return Conversion[]
31
     */
32
    public function createConversions(
33
        SpecificationInterface $source,
34
        array $targets,
35
        ...$filters
36
    ) {
37
        $reader = $this->getReader($source);
38
        $pipelines = [];
39
        foreach ($targets as $target) {
40
            $pipelines[] = $this
41
                ->instantiateConversion($reader, $target, $filters);
42
        }
43
        return $pipelines;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $pipelines returns the type array|AmaTeam\Image\Proj...\Framework\Conversion[] which is incompatible with the return type mandated by AmaTeam\Image\Projection...ce::createConversions() of AmaTeam\Image\Projection\API\ConversionInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
44
    }
45
46
    /**
47
     * Creates conversion pipeline for single target.
48
     *
49
     * @param SpecificationInterface $source
50
     * @param SpecificationInterface $target
51
     * @param FilterInterface[] $filters
52
     * @return Conversion
53
     */
54
    public function createConversion(
55
        SpecificationInterface $source,
56
        SpecificationInterface $target,
57
        ...$filters
58
    ) {
59
        $reader = $this->getReader($source);
60
        return $this->instantiateConversion($reader, $target, $filters);
61
    }
62
63
    /**
64
     * @param SpecificationInterface $source
65
     * @return ReaderInterface
66
     */
67
    private function getReader(SpecificationInterface $source)
68
    {
69
        return $this
70
            ->registry
71
            ->getHandler($source->getType())
72
            ->read($source);
73
    }
74
75
    /**
76
     * @param ReaderInterface $source
77
     * @param SpecificationInterface $target
78
     * @param FilterInterface[] $filters
79
     * @return Conversion
80
     */
81
    private function instantiateConversion(
82
        ReaderInterface $source,
83
        SpecificationInterface $target,
84
        array $filters
85
    ) {
86
        $generator = $this
87
            ->registry
88
            ->getHandler($target->getType())
89
            ->createGenerator($source, $target, $filters);
90
        return new Conversion($target, $generator);
91
    }
92
}
93