Passed
Push — dev ( 329389...de7200 )
by Fike
02:32
created

Framework::getConverter()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\Image\Projection;
4
5
use AmaTeam\Image\Projection\API\ConversionInterface;
6
use AmaTeam\Image\Projection\API\ConverterInterface;
7
use AmaTeam\Image\Projection\API\FrameworkInterface;
8
use AmaTeam\Image\Projection\Framework\Converter;
9
use AmaTeam\Image\Projection\Conversion\Listener\SaveListener;
10
use AmaTeam\Image\Projection\Image\EncodingOptions;
11
use AmaTeam\Image\Projection\API\Image\Format;
12
use AmaTeam\Image\Projection\API\Type\HandlerInterface;
13
use AmaTeam\Image\Projection\Type\Registry;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\NullLogger;
16
17
class Framework implements FrameworkInterface
18
{
19
    /**
20
     * @var Registry
21
     */
22
    private $registry;
23
    /**
24
     * @var LoggerInterface
25
     */
26
    private $logger;
27
    /**
28
     * @var ConverterInterface
29
     */
30
    private $converter;
31
32
    /**
33
     * @param Registry $registry
34
     * @param LoggerInterface|null $logger
35
     */
36
    public function __construct(
37
        Registry $registry = null,
38
        LoggerInterface $logger = null
39
    ) {
40
        $logger = $logger ?: new NullLogger();
41
        if (!$registry) {
42
            $registry = (new Registry(null, null, $logger))
43
                ->registerDefaultTypes();
44
        }
45
        $this->registry = $registry;
46
        $this->logger = $logger;
47
        $this->converter = new Converter($registry);
48
    }
49
50
    /**
51
     * @param string $type
52
     *
53
     * @return HandlerInterface
54
     */
55
    public function getHandler($type)
56
    {
57
        return $this->registry->getHandler($type);
58
    }
59
60
    /**
61
     * @param Specification $source
62
     * @param Specification $target
63
     * @param string $format
64
     * @param EncodingOptions $options
65
     */
66 View Code Duplication
    public function convert(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
        Specification $source,
68
        Specification $target,
69
        $format = Format::JPEG,
70
        EncodingOptions $options = null
71
    ) {
72
        $conversion = $this->converter->createConversion($source, $target);
73
        $this->processConversion($conversion, $format, $options);
74
    }
75
76
    /**
77
     * @param Specification $source
78
     * @param Specification[] $targets
79
     * @param string $format
80
     * @param EncodingOptions|null $options
81
     */
82 View Code Duplication
    public function convertAll(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
        Specification $source,
84
        array $targets,
85
        $format = Format::JPEG,
86
        EncodingOptions $options = null
87
    ) {
88
        $conversions = $this->converter->createConversions($source, $targets);
89
        foreach ($conversions as $conversion) {
90
            $this->processConversion($conversion, $format, $options);
91
        }
92
    }
93
94
    /**
95
     * @param ConversionInterface $conversion
96
     * @param string $format
97
     * @param EncodingOptions $options
98
     * @return void
99
     */
100
    private function processConversion(
101
        ConversionInterface $conversion,
102
        $format,
103
        EncodingOptions $options = null
104
    ) {
105
        $listener = new SaveListener($format, $options);
106
        $conversion->addListener($listener);
107
        $conversion->run();
108
    }
109
110
    /**
111
     * Registers new type handler
112
     *
113
     * @param string $type
114
     * @param HandlerInterface $handler
115
     * @return $this
116
     */
117
    public function register($type, HandlerInterface $handler)
118
    {
119
        $this->registry->register($type, $handler);
120
        return $this;
121
    }
122
123
    /**
124
     * @return string[]
125
     */
126
    public function getRegisteredTypes()
127
    {
128
        return $this->registry->getRegisteredTypes();
129
    }
130
131
    /**
132
     * @return ConverterInterface
133
     */
134
    public function getConverter()
135
    {
136
        return $this->converter;
137
    }
138
139
    /**
140
     * @return Registry
141
     */
142
    public function getRegistry()
143
    {
144
        return $this->registry;
145
    }
146
}
147