Manager::read()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\Image\Projection\Image;
4
5
use AmaTeam\Image\Projection\API\Image\ImageFactoryInterface;
6
use AmaTeam\Image\Projection\API\Image\ImageInterface;
7
use League\Flysystem\FilesystemInterface;
8
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
9
10
class Manager
11
{
12
    /**
13
     * @var FilesystemInterface
14
     */
15
    private $filesystem;
16
17
    /**
18
     * @var ImageFactoryInterface
19
     */
20
    private $imageFactory;
21
22
    /**
23
     * @param FilesystemInterface $filesystem
24
     * @param ImageFactoryInterface $imageFactory
25
     */
26
    public function __construct(
27
        FilesystemInterface $filesystem,
28
        ImageFactoryInterface $imageFactory
29
    ) {
30
        $this->filesystem = $filesystem;
31
        $this->imageFactory = $imageFactory;
32
    }
33
34
    /**
35
     * @param int $width
36
     * @param int $height
37
     * @return ImageInterface
38
     */
39
    public function create($width, $height)
40
    {
41
        return $this->imageFactory->create($width, $height);
42
    }
43
44
    /**
45
     * @param string $path
46
     * @return ImageInterface
47
     */
48
    public function read($path)
49
    {
50
        if (!$this->filesystem->has($path)) {
51
            $message = "Couldn\'t find image at $path";
52
            throw new BadMethodCallException($message);
53
        }
54
        $content = $this->filesystem->read($path);
55
        return $this->imageFactory->read($content);
56
    }
57
58
    /**
59
     * @param ImageInterface $image
60
     * @param string $path
61
     * @param string $format
62
     * @param EncodingOptions $options
63
     */
64
    public function save(
65
        ImageInterface $image,
66
        $path,
67
        $format,
68
        EncodingOptions $options = null
69
    ) {
70
        $blob = $image->getBinary($format, $options);
71
        if ($this->filesystem->has($path)) {
72
            $this->filesystem->delete($path);
73
        }
74
        $this->filesystem->write($path, $blob);
75
    }
76
}
77