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

Framework   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 116
Duplicated Lines 20.69 %

Importance

Changes 0
Metric Value
dl 24
loc 116
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandler() 0 3 1
A convert() 10 10 1
A register() 0 4 1
A __construct() 0 12 3
A getRegistry() 0 3 1
A getConverter() 0 3 1
A convertAll() 11 11 2
A getRegisteredTypes() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AmaTeam\Image\Projection;
4
5
use AmaTeam\Image\Projection\API\ConverterInterface;
6
use AmaTeam\Image\Projection\API\FrameworkInterface;
7
use AmaTeam\Image\Projection\Framework\Converter;
8
use AmaTeam\Image\Projection\Conversion\Listener\SaveListener;
9
use AmaTeam\Image\Projection\Image\EncodingOptions;
10
use AmaTeam\Image\Projection\API\Image\Format;
11
use AmaTeam\Image\Projection\API\Type\HandlerInterface;
12
use AmaTeam\Image\Projection\Type\Registry;
13
use Psr\Log\LoggerInterface;
14
use Psr\Log\NullLogger;
15
16
class Framework implements FrameworkInterface
17
{
18
    /**
19
     * @var Registry
20
     */
21
    private $registry;
22
    /**
23
     * @var LoggerInterface
24
     */
25
    private $logger;
26
    /**
27
     * @var ConverterInterface
28
     */
29
    private $converter;
30
31
    /**
32
     * @param Registry $registry
33
     * @param LoggerInterface|null $logger
34
     */
35
    public function __construct(
36
        Registry $registry = null,
37
        LoggerInterface $logger = null
38
    ) {
39
        $logger = $logger ?: new NullLogger();
40
        if (!$registry) {
41
            $registry = (new Registry(null, null, $logger))
42
                ->registerDefaultTypes();
43
        }
44
        $this->registry = $registry;
45
        $this->logger = $logger;
46
        $this->converter = new Converter($registry);
47
    }
48
49
    /**
50
     * @param string $type
51
     *
52
     * @return HandlerInterface
53
     */
54
    public function getHandler($type)
55
    {
56
        return $this->registry->getHandler($type);
57
    }
58
59
    /**
60
     * @param Specification $source
61
     * @param Specification $target
62
     * @param string $format
63
     * @param EncodingOptions $options
64
     */
65 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...
66
        Specification $source,
67
        Specification $target,
68
        $format = Format::JPEG,
69
        EncodingOptions $options = null
70
    ) {
71
        $persistenceListener = new SaveListener($format, $options);
72
        $pipeline = $this->converter->createConversion($source, $target);
73
        $pipeline->addListener($persistenceListener);
74
        $pipeline->run();
75
    }
76
77
    /**
78
     * @param Specification $source
79
     * @param Specification[] $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
        Specification $source,
85
        array $targets,
86
        $format = Format::JPEG,
87
        EncodingOptions $options = null
88
    ) {
89
        $pipelines = $this->converter->createConversions($source, $targets);
90
        $listener = new SaveListener($format, $options);
91
        foreach ($pipelines as $pipeline) {
92
            $pipeline->addListener($listener);
93
            $pipeline->run();
94
        }
95
    }
96
97
    /**
98
     * Registers new type handler
99
     *
100
     * @param string $type
101
     * @param HandlerInterface $handler
102
     * @return $this
103
     */
104
    public function register($type, HandlerInterface $handler)
105
    {
106
        $this->registry->register($type, $handler);
107
        return $this;
108
    }
109
110
    /**
111
     * @return string[]
112
     */
113
    public function getRegisteredTypes()
114
    {
115
        return $this->registry->getRegisteredTypes();
116
    }
117
118
    /**
119
     * @return ConverterInterface
120
     */
121
    public function getConverter()
122
    {
123
        return $this->converter;
124
    }
125
126
    /**
127
     * @return Registry
128
     */
129
    public function getRegistry()
130
    {
131
        return $this->registry;
132
    }
133
}
134