Autodetect::save()   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 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 6
    public function __construct(?IIMTranslations $lang = null)
22
    {
23 6
        $this->setImLang($lang);
24 6
        if (!function_exists('imagecreatefromstring')) {
25
            // @codeCoverageIgnoreStart
26
            throw new ImagesException($this->getImLang()->imImageMagicLibNotPresent(), ImagesException::FORMAT_NO_LIBRARY);
27
        }
28
        // @codeCoverageIgnoreEnd
29
    }
30
31 6
    public function load(string $path)
32
    {
33 6
        $content = @file_get_contents($path);
34 6
        if (empty($content)) {
35 1
            throw new ImagesException($this->getImLang()->imCannotCreateFromResource(),ImagesException::FORMAT_AUTO_NO_FILE);
36
        }
37 5
        $result = @imagecreatefromstring($content);
38 5
        if (false === $result) {
39 4
            throw new ImagesException($this->getImLang()->imCannotCreateFromResource(), ImagesException::FORMAT_AUTO_NO_IMAGE);
40
        }
41 1
        return $result;
42
    }
43
44 1
    public function save(?string $path, $resource): void
45
    {
46 1
        throw new ImagesException($this->getImLang()->imCannotSaveResource(), ImagesException::FORMAT_AUTO_CANNOT_SAVE);
47
    }
48
}
49