ManipulatorFactory::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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