ManipulatorFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 22
dl 0
loc 78
ccs 24
cts 25
cp 0.96
rs 10
c 2
b 1
f 1
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A checkManipulators() 0 4 2
A create() 0 3 1
A get() 0 8 2
A initManipulators() 0 9 2
A createForMedia() 0 11 3
A getManipulators() 0 5 1
1
<?php
2
3
namespace ByTIC\MediaLibrary\Media\Manipulators;
4
5
use ByTIC\MediaLibrary\Media\Manipulators\Images\ImageManipulator;
6
use ByTIC\MediaLibrary\Media\Media;
7
8
/**
9
 * Class ManipulatorFactory.
10
 */
11
class ManipulatorFactory
12
{
13
    /**
14
     * @var AbstractManipulator[]
15
     */
16
    protected static $manipulators = null;
17
18
    /**
19
     * @param $media
20
     *
21
     * @return AbstractManipulator
22
     */
23 3
    public static function createForMedia(Media $media)
24
    {
25 3
        $manipulators = self::getManipulators();
26
27 3
        foreach ($manipulators as $manipulator) {
28 3
            if ($manipulator->canConvert($media)) {
29 2
                return $manipulator;
30
            }
31
        }
32
33 1
        return self::get('base');
34
    }
35
36
    /**
37
     * @return AbstractManipulator[]
38
     */
39 3
    public static function getManipulators()
40
    {
41 3
        self::checkManipulators();
42
43 3
        return self::$manipulators;
44
    }
45
46
    /**
47
     * @param string $class
48
     *
49
     * @return AbstractManipulator
50
     */
51 1
    public static function create($class)
52
    {
53 1
        return new $class();
54
    }
55
56
    /**
57
     * @param string $name
58
     *
59
     * @throws \Exception
60
     *
61
     * @return AbstractManipulator
62
     */
63 1
    public static function get($name)
64
    {
65 1
        self::checkManipulators();
66 1
        if (isset(self::$manipulators[$name])) {
67 1
            return self::$manipulators[$name];
68
        }
69
70
        throw new \Exception('Invalid manipulator name');
71
    }
72
73 3
    protected static function checkManipulators()
74
    {
75 3
        if (self::$manipulators === null) {
0 ignored issues
show
introduced by
The condition self::manipulators === null is always false.
Loading history...
76 1
            self::initManipulators();
77
        }
78 3
    }
79
80 1
    protected static function initManipulators()
81
    {
82
        $classes = [
83 1
            'image' => ImageManipulator::class,
84
            'base'  => BaseManipulator::class,
85
        ];
86 1
        self::$manipulators = [];
87 1
        foreach ($classes as $type => $class) {
88 1
            self::$manipulators[$type] = self::create($class);
89
        }
90 1
    }
91
}
92