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
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Psr\Log\NullLogger; |
12
|
|
|
|
13
|
|
|
class Converter implements ConverterInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Registry |
17
|
|
|
*/ |
18
|
|
|
private $registry; |
19
|
|
|
/** |
20
|
|
|
* @var LoggerInterface |
21
|
|
|
*/ |
22
|
|
|
private $logger; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Registry $registry |
26
|
|
|
* @param LoggerInterface|null $logger |
27
|
|
|
*/ |
28
|
|
|
public function __construct( |
29
|
|
|
Registry $registry, |
30
|
|
|
LoggerInterface $logger = null |
31
|
|
|
) { |
32
|
|
|
$this->registry = $registry; |
33
|
|
|
$this->logger = $logger ?: new NullLogger(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param SpecificationInterface $source |
38
|
|
|
* @param SpecificationInterface[] $targets |
39
|
|
|
* @param FilterInterface[] $filters |
40
|
|
|
* @return Conversion[] |
41
|
|
|
*/ |
42
|
|
|
public function createConversions( |
43
|
|
|
SpecificationInterface $source, |
44
|
|
|
array $targets, |
45
|
|
|
...$filters |
46
|
|
|
) { |
47
|
|
|
$context = ['source' => $source, 'targets' => $targets]; |
48
|
|
|
$message = 'Converting {source} to targets {targets}'; |
49
|
|
|
$this->logger->debug($message, $context); |
50
|
|
|
$reader = $this->getReader($source); |
51
|
|
|
$pipelines = []; |
52
|
|
|
foreach ($targets as $target) { |
53
|
|
|
$pipelines[] = $this |
54
|
|
|
->instantiateConversion($reader, $target, $filters); |
55
|
|
|
} |
56
|
|
|
return $pipelines; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Creates conversion pipeline for single target. |
61
|
|
|
* |
62
|
|
|
* @param SpecificationInterface $source |
63
|
|
|
* @param SpecificationInterface $target |
64
|
|
|
* @param FilterInterface[] $filters |
65
|
|
|
* @return Conversion |
66
|
|
|
*/ |
67
|
|
|
public function createConversion( |
68
|
|
|
SpecificationInterface $source, |
69
|
|
|
SpecificationInterface $target, |
70
|
|
|
...$filters |
71
|
|
|
) { |
72
|
|
|
$context = ['source' => $source, 'target' => $target]; |
73
|
|
|
$message = 'Converting {source} to target {target}'; |
74
|
|
|
$this->logger->debug($message, $context); |
75
|
|
|
$reader = $this->getReader($source); |
76
|
|
|
return $this->instantiateConversion($reader, $target, $filters); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param SpecificationInterface $source |
81
|
|
|
* @return ReaderInterface |
82
|
|
|
*/ |
83
|
|
|
private function getReader(SpecificationInterface $source) |
84
|
|
|
{ |
85
|
|
|
return $this |
86
|
|
|
->registry |
87
|
|
|
->getHandler($source->getType()) |
88
|
|
|
->read($source); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param ReaderInterface $source |
93
|
|
|
* @param SpecificationInterface $target |
94
|
|
|
* @param FilterInterface[] $filters |
95
|
|
|
* @return Conversion |
96
|
|
|
*/ |
97
|
|
|
private function instantiateConversion( |
98
|
|
|
ReaderInterface $source, |
99
|
|
|
SpecificationInterface $target, |
100
|
|
|
array $filters |
101
|
|
|
) { |
102
|
|
|
$generator = $this |
103
|
|
|
->registry |
104
|
|
|
->getHandler($target->getType()) |
105
|
|
|
->createGenerator($source, $target, $filters); |
106
|
|
|
return new Conversion($target, $generator, $this->logger); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|