Test Failed
Pull Request — feature/unit-tests (#37)
by Daniel
11:03
created

FileDataFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 26
dl 0
loc 50
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isImage() 0 3 2
A __construct() 0 6 1
A create() 0 20 6
A getPublicPath() 0 7 1
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\File;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use Silverback\ApiComponentBundle\Dto\File\FileData;
18
use Silverback\ApiComponentBundle\Dto\File\ImageMetadata;
19
use Silverback\ApiComponentBundle\Entity\Utility\FileInterface;
20
use Symfony\Component\HttpFoundation\UrlHelper;
21
use Symfony\Component\Routing\RouterInterface;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
class FileDataFactory
27
{
28
    private IriConverterInterface $iriConverter;
29
    private RouterInterface $router;
30
    private ImagineMetadataFactory $imagineMetadataFactory;
31
    private UrlHelper $urlHelper;
32
33
    public function __construct(IriConverterInterface $iriConverter, RouterInterface $router, ImagineMetadataFactory $imagineMetadataFactory, UrlHelper $urlHelper)
34
    {
35
        $this->iriConverter = $iriConverter;
36
        $this->router = $router;
37
        $this->imagineMetadataFactory = $imagineMetadataFactory;
38
        $this->urlHelper = $urlHelper;
39
    }
40
41
    public function create(FileInterface $resource): ?FileData
42
    {
43
        if (!($filePath = $resource->getFilePath()) || !file_exists($filePath)) {
44
            return null;
45
        }
46
47
        if ($fileData = $resource->getFileData()) {
48
            return $fileData;
49
        }
50
51
        $publicPath = $this->getPublicPath($resource);
52
53
        $imageData = self::isImage($filePath) ? new ImageMetadata($filePath, $this->urlHelper->getAbsoluteUrl($publicPath)) : null;
54
55
        return new FileData(
56
            $this->urlHelper->getAbsoluteUrl($publicPath),
57
            pathinfo($filePath, PATHINFO_EXTENSION),
58
            filesize($filePath) ?: null,
59
            $imageData,
60
            $this->imagineMetadataFactory->create($resource)
61
        );
62
    }
63
64
    private static function isImage($filePath): bool
65
    {
66
        return exif_imagetype($filePath) || 'image/svg+xml' === mime_content_type($filePath);
67
    }
68
69
    private function getPublicPath(FileInterface $resource): string
70
    {
71
        $objectId = $this->iriConverter->getIriFromItem($resource);
72
73
        return $this->router->generate(
74
            'api_component_file_upload',
75
            ['field' => 'filePath', 'id' => $objectId]
76
        );
77
    }
78
}
79