Api   A
last analyzed

↳ Parent: Project

Coupling/Cohesion

Components 1
Dependencies 1

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 86
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setImageManager() 0 4 1
A getImageManager() 0 4 1
A setManipulators() 0 10 3
A getManipulators() 0 4 1
A run() 0 12 2
1
<?php
2
3
namespace League\Glide\Api;
4
5
use Intervention\Image\ImageManager;
6
use InvalidArgumentException;
7
use League\Glide\Manipulators\ManipulatorInterface;
8
9
class Api implements ApiInterface
10
{
11
    /**
12
     * Intervention image manager.
13
     * @var ImageManager
14
     */
15
    protected $imageManager;
16
17
    /**
18
     * Collection of manipulators.
19
     * @var array
20
     */
21
    protected $manipulators;
22
23
    /**
24
     * Create API instance.
25
     * @param ImageManager $imageManager Intervention image manager.
26
     * @param array        $manipulators Collection of manipulators.
27
     */
28 30
    public function __construct(ImageManager $imageManager, array $manipulators)
29
    {
30 30
        $this->setImageManager($imageManager);
31 30
        $this->setManipulators($manipulators);
32 30
    }
33
34
    /**
35
     * Set the image manager.
36
     * @param ImageManager $imageManager Intervention image manager.
37
     */
38 30
    public function setImageManager(ImageManager $imageManager)
39
    {
40 30
        $this->imageManager = $imageManager;
41 30
    }
42
43
    /**
44
     * Get the image manager.
45
     * @return ImageManager Intervention image manager.
46
     */
47 6
    public function getImageManager()
48
    {
49 6
        return $this->imageManager;
50
    }
51
52
    /**
53
     * Set the manipulators.
54
     * @param array $manipulators Collection of manipulators.
55
     */
56 30
    public function setManipulators(array $manipulators)
57
    {
58 30
        foreach ($manipulators as $manipulator) {
59 18
            if (!($manipulator instanceof ManipulatorInterface)) {
60 3
                throw new InvalidArgumentException('Not a valid manipulator.');
61
            }
62 30
        }
63
64 30
        $this->manipulators = $manipulators;
65 30
    }
66
67
    /**
68
     * Get the manipulators.
69
     * @return array Collection of manipulators.
70
     */
71 6
    public function getManipulators()
72
    {
73 6
        return $this->manipulators;
74
    }
75
76
    /**
77
     * Perform image manipulations.
78
     * @param  string $source Source image binary data.
79
     * @param  array  $params The manipulation params.
80
     * @return string Manipulated image binary data.
81
     */
82 3
    public function run($source, array $params)
83
    {
84 3
        $image = $this->imageManager->make($source);
85
86 3
        foreach ($this->manipulators as $manipulator) {
87 3
            $manipulator->setParams($params);
88
89 3
            $image = $manipulator->run($image);
90 3
        }
91
92 3
        return $image->getEncoded();
93
    }
94
}
95