Completed
Push — master ( 37fe77...5f884d )
by Jonathan
03:10
created

Api::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 5
cp 0.8
rs 10
cc 1
nc 1
nop 2
crap 1.008
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 20
    public function __construct(ImageManager $imageManager, array $manipulators)
29
    {
30 20
        $this->setImageManager($imageManager);
31 20
        $this->setManipulators($manipulators);
32 20
    }
33
34
    /**
35
     * Set the image manager.
36
     * @param ImageManager $imageManager Intervention image manager.
37
     */
38 20
    public function setImageManager(ImageManager $imageManager)
39
    {
40 20
        $this->imageManager = $imageManager;
41 20
    }
42
43
    /**
44
     * Get the image manager.
45
     * @return ImageManager Intervention image manager.
46
     */
47 4
    public function getImageManager()
48
    {
49 4
        return $this->imageManager;
50
    }
51
52
    /**
53
     * Set the manipulators.
54
     * @param array $manipulators Collection of manipulators.
55
     */
56 20
    public function setManipulators(array $manipulators)
57
    {
58 20
        foreach ($manipulators as $manipulator) {
59 12
            if (!($manipulator instanceof ManipulatorInterface)) {
60 2
                throw new InvalidArgumentException('Not a valid manipulator.');
61
            }
62 20
        }
63
64 20
        $this->manipulators = $manipulators;
65 20
    }
66
67
    /**
68
     * Get the manipulators.
69
     * @return array Collection of manipulators.
70
     */
71 4
    public function getManipulators()
72
    {
73 4
        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 2
    public function run($source, array $params)
83
    {
84 2
        $image = $this->imageManager->make($source);
85
86 2
        foreach ($this->manipulators as $manipulator) {
87 2
            $manipulator->setParams($params);
88
89 2
            $image = $manipulator->run($image);
90 2
        }
91
92 2
        return $image->getEncoded();
93
    }
94
}
95