Framework::convert()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 8
loc 8
rs 9.4285
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\API\SpecificationInterface;
9
use AmaTeam\Image\Projection\Framework\Converter;
10
use AmaTeam\Image\Projection\Conversion\Listener\SaveListener;
11
use AmaTeam\Image\Projection\Image\EncodingOptions;
12
use AmaTeam\Image\Projection\API\Image\Format;
13
use AmaTeam\Image\Projection\API\Type\HandlerInterface;
14
use AmaTeam\Image\Projection\Type\Registry;
15
use Psr\Log\LoggerInterface;
16
use Psr\Log\NullLogger;
17
18
class Framework implements FrameworkInterface
19
{
20
    /**
21
     * @var Registry
22
     */
23
    private $registry;
24
    /**
25
     * @var LoggerInterface
26
     */
27
    private $logger;
28
    /**
29
     * @var ConverterInterface
30
     */
31
    private $converter;
32
33
    /**
34
     * @param Registry $registry
35
     * @param LoggerInterface|null $logger
36
     */
37
    public function __construct(
38
        Registry $registry = null,
39
        LoggerInterface $logger = null
40
    ) {
41
        $logger = $logger ?: new NullLogger();
42
        if (!$registry) {
43
            $registry = (new Registry(null, null, $logger))
44
                ->registerDefaultTypes();
45
        }
46
        $this->registry = $registry;
47
        $this->logger = $logger;
48
        $this->converter = new Converter($registry, $logger);
49
    }
50
51
    /**
52
     * @param string $type
53
     *
54
     * @return HandlerInterface
55
     */
56
    public function getHandler($type)
57
    {
58
        return $this->registry->getHandler($type);
59
    }
60
61
    /**
62
     * @param SpecificationInterface $source
63
     * @param SpecificationInterface $target
64
     * @param string $format
65
     * @param EncodingOptions $options
66
     */
67 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...
68
        SpecificationInterface $source,
69
        SpecificationInterface $target,
70
        $format = Format::JPEG,
71
        EncodingOptions $options = null
72
    ) {
73
        $conversion = $this->converter->createConversion($source, $target);
74
        $this->processConversion($conversion, $format, $options);
75
    }
76
77
    /**
78
     * @param SpecificationInterface $source
79
     * @param SpecificationInterface[] $targets
80
     * @param string $format
81
     * @param EncodingOptions|null $options
82
     */
83 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...
84
        SpecificationInterface $source,
85
        array $targets,
86
        $format = Format::JPEG,
87
        EncodingOptions $options = null
88
    ) {
89
        $conversions = $this->converter->createConversions($source, $targets);
90
        foreach ($conversions as $conversion) {
91
            $this->processConversion($conversion, $format, $options);
92
        }
93
    }
94
95
    /**
96
     * @param ConversionInterface $conversion
97
     * @param string $format
98
     * @param EncodingOptions $options
99
     * @return void
100
     */
101
    private function processConversion(
102
        ConversionInterface $conversion,
103
        $format,
104
        EncodingOptions $options = null
105
    ) {
106
        $filesystem = $this->registry->getFilesystem();
107
        $listener = new SaveListener(
108
            $filesystem,
109
            $format,
110
            $options,
111
            $this->logger
112
        );
113
        $conversion->addListener($listener);
114
        $conversion->run();
115
    }
116
117
    /**
118
     * Registers new type handler
119
     *
120
     * @param string $type
121
     * @param HandlerInterface $handler
122
     * @return $this
123
     */
124
    public function register($type, HandlerInterface $handler)
125
    {
126
        $this->registry->register($type, $handler);
127
        return $this;
128
    }
129
130
    /**
131
     * @return string[]
132
     */
133
    public function getRegisteredTypes()
134
    {
135
        return $this->registry->getRegisteredTypes();
136
    }
137
138
    /**
139
     * @return ConverterInterface
140
     */
141
    public function getConverter()
142
    {
143
        return $this->converter;
144
    }
145
146
    /**
147
     * @return Registry
148
     */
149
    public function getRegistry()
150
    {
151
        return $this->registry;
152
    }
153
}
154