Passed
Push — v2 ( e7f8e2...a41bff )
by Daniel
04:01
created

ImagineMetadataFactory::isImagineFilePath()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
ccs 0
cts 7
cp 0
crap 20
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\Factory;
15
16
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
17
use Silverback\ApiComponentBundle\Dto\File\ImageMetadata;
18
use Silverback\ApiComponentBundle\Dto\File\ImagineMetadata;
19
use Silverback\ApiComponentBundle\Entity\Utility\FileInterface;
20
use Silverback\ApiComponentBundle\Imagine\PathResolver;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 */
25
class ImagineMetadataFactory
26
{
27
    private CacheManager $cacheManager;
28
    private PathResolver $pathResolver;
29
    private string $projectDirectory;
30
31
    public function __construct(CacheManager $cacheManager, PathResolver $pathResolver, string $projectDirectory)
32
    {
33
        $this->cacheManager = $cacheManager;
34
        $this->pathResolver = $pathResolver;
35
        $this->projectDirectory = $projectDirectory;
36
    }
37
38
    public static function isImagineFilePath(?string $filePath): bool
39
    {
40
        if (!$filePath || !file_exists($filePath)) {
41
            return false;
42
        }
43
        try {
44
            $imageType = exif_imagetype($filePath);
45
        } catch (\Exception $e) {
46
            return false;
47
        }
48
49
        return \in_array($imageType, [IMAGETYPE_JPEG, IMAGETYPE_JPEG2000, IMAGETYPE_PNG, IMAGETYPE_GIF], true);
50
    }
51
52
    public function create(FileInterface $file): ?ImagineMetadata
53
    {
54
        $filePath = $file->getFilePath();
55
        if (!class_exists(CacheManager::class) || !self::isImagineFilePath($filePath)) {
56
            return null;
57
        }
58
        $imagineMetadata = new ImagineMetadata();
59
        foreach ($file::getImagineFilters() as $returnKey => $filter) {
60
            $resolvedPath = $this->pathResolver->resolve($filePath);
61
            $imagineBrowserPath = $this->cacheManager->getBrowserPath($resolvedPath, $filter);
62
            $imagineFilePath = urldecode(ltrim(
63
                parse_url(
64
                    $imagineBrowserPath,
65
                    PHP_URL_PATH
66
                ),
67
                '/'
68
            ));
69
            $realPath = sprintf('%s/public/%s', $this->projectDirectory, $imagineFilePath);
70
            $imagineMetadata->addFilter($returnKey, new ImageMetadata($realPath, $imagineFilePath, $filter));
71
        }
72
73
        return $imagineMetadata;
74
    }
75
}
76