Test Failed
Push — master ( d5753b...1305b9 )
by Petr
08:39
created

Autodetect::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace kalanis\kw_images\Graphics\Format;
4
5
6
use kalanis\kw_images\ImagesException;
7
use kalanis\kw_images\Interfaces\IIMTranslations;
8
9
10
/**
11
 * Class Autodetect
12
 * Get image using autodetection; cannot save this way, it needs to tell which format will be used
13
 * @package kalanis\kw_images\Graphics\Format
14
 */
15
class Autodetect extends AFormat
16
{
17
    /**
18
     * @param IIMTranslations|null $lang
19
     * @throws ImagesException
20
     */
21
    public function __construct(?IIMTranslations $lang = null)
22
    {
23
        $this->setImLang($lang);
24
        if (!function_exists('imagecreatefromstring')) {
25
            // @codeCoverageIgnoreStart
26
            throw new ImagesException($this->getImLang()->imImageMagicLibNotPresent());
27
        }
28
        // @codeCoverageIgnoreEnd
29
    }
30
31
    public function load(string $path)
32
    {
33
        $content = @file_get_contents($path);
34
        if (empty($content)) {
35
            throw new ImagesException($this->getImLang()->imCannotCreateFromResource());
36
        }
37
        $result = @imagecreatefromstring($content);
38
        if (false === $result) {
39
            throw new ImagesException($this->getImLang()->imCannotCreateFromResource());
40
        }
41
        return $result;
42
    }
43
44
    public function save(?string $path, $resource): void
45
    {
46
        throw new ImagesException($this->getImLang()->imCannotSaveResource());
47
    }
48
}
49