Passed
Pull Request — master (#58)
by Daniel
06:28
created

UploadableHelper::getMediaObjects()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 14
nop 1
dl 0
loc 31
ccs 0
cts 18
cp 0
crap 56
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components 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\ApiComponentsBundle\Uploadable;
15
16
use Doctrine\Persistence\ManagerRegistry;
17
use Liip\ImagineBundle\Service\FilterService;
18
use Silverback\ApiComponentsBundle\Annotation\UploadableField;
19
use Silverback\ApiComponentsBundle\AnnotationReader\UploadableAnnotationReader;
20
use Silverback\ApiComponentsBundle\Entity\Utility\ImagineFiltersInterface;
21
use Silverback\ApiComponentsBundle\Flysystem\FilesystemProvider;
22
use Silverback\ApiComponentsBundle\Imagine\FlysystemDataLoader;
23
use Silverback\ApiComponentsBundle\Model\Uploadable\UploadedDataUriFile;
24
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait;
25
use Symfony\Component\HttpFoundation\FileBag;
26
use Symfony\Component\PropertyAccess\PropertyAccess;
27
28
/**
29
 * @author Daniel West <[email protected]>
30
 */
31
class UploadableHelper
32
{
33
    use ClassMetadataTrait;
34
35
    private UploadableAnnotationReader $annotationReader;
36
    private FilesystemProvider $filesystemProvider;
37
    private ?FilterService $filterService;
38
    private FlysystemDataLoader $flysystemDataLoader;
39
40
    public function __construct(
41
        ManagerRegistry $registry,
42
        UploadableAnnotationReader $annotationReader,
43
        FilesystemProvider $filesystemProvider,
44
        FlysystemDataLoader $flysystemDataLoader,
45
        ?FilterService $filterService = null
46
    ) {
47
        $this->initRegistry($registry);
48
        $this->annotationReader = $annotationReader;
49
        $this->filesystemProvider = $filesystemProvider;
50
        $this->flysystemDataLoader = $flysystemDataLoader;
51
        $this->filterService = $filterService;
52
    }
53
54
    public function setUploadedFilesFromFileBag(object $object, FileBag $fileBag): void
55
    {
56
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
57
        $configuredProperties = $this->annotationReader->getConfiguredProperties($object, false, true);
58
59
        /**
60
         * @var UploadableField[] $configuredProperties
61
         */
62
        foreach ($configuredProperties as $fileProperty => $fieldConfiguration) {
63
            if ($file = $fileBag->get($fileProperty, null)) {
64
                $propertyAccessor->setValue($object, $fileProperty, $file);
65
            }
66
        }
67
    }
68
69
    public function storeFilesMetadata(object $object): void
70
    {
71
        $configuredProperties = $this->annotationReader->getConfiguredProperties($object, true, true);
72
        $classMetadata = $this->getClassMetadata($object);
73
74
        foreach ($configuredProperties as $fileProperty => $fieldConfiguration) {
75
            // Let the data loader which should be configured for imagine to know which adapter to use
76
            $this->flysystemDataLoader->setAdapter($fieldConfiguration->adapter);
77
78
            $filename = $classMetadata->getFieldValue($object, $fieldConfiguration->property);
79
80
            if ($object instanceof ImagineFiltersInterface && $this->filterService) {
81
                $filters = $object->getImagineFilters($fileProperty, null);
82
                foreach ($filters as $filter) {
83
                    // This will trigger the cached file to be store
84
                    // When cached files are store we save the file info
85
                    $this->filterService->getUrlOfFilteredImage($filename, $filter);
86
                }
87
            }
88
        }
89
    }
90
91
    public function persistFiles(object $object): void
92
    {
93
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
94
        $classMetadata = $this->getClassMetadata($object);
95
96
        $configuredProperties = $this->annotationReader->getConfiguredProperties($object, true, true);
97
        /**
98
         * @var UploadableField[] $configuredProperties
99
         */
100
        foreach ($configuredProperties as $fileProperty => $fieldConfiguration) {
101
            $currentFilepath = $classMetadata->getFieldValue($object, $fieldConfiguration->property);
102
            if ($currentFilepath) {
103
                $this->removeFilepath($object, $fieldConfiguration);
0 ignored issues
show
Bug introduced by
It seems like $fieldConfiguration can also be of type string; however, parameter $fieldConfiguration of Silverback\ApiComponents...elper::removeFilepath() does only seem to accept Silverback\ApiComponents...otation\UploadableField, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
                $this->removeFilepath($object, /** @scrutinizer ignore-type */ $fieldConfiguration);
Loading history...
104
            }
105
            /** @var UploadedDataUriFile|null $file */
106
            $file = $propertyAccessor->getValue($object, $fileProperty);
107
            if (!$file) {
108
                $classMetadata->setFieldValue($object, $fieldConfiguration->property, null);
109
                continue;
110
            }
111
112
            $filesystem = $this->filesystemProvider->getFilesystem($fieldConfiguration->adapter);
0 ignored issues
show
Bug introduced by
It seems like $fieldConfiguration->adapter can also be of type null; however, parameter $name of Silverback\ApiComponents...ovider::getFilesystem() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
            $filesystem = $this->filesystemProvider->getFilesystem(/** @scrutinizer ignore-type */ $fieldConfiguration->adapter);
Loading history...
113
114
            $path = $fieldConfiguration->prefix ?? '';
115
            $path .= $file->getFilename();
116
            $stream = fopen($file->getRealPath(), 'r');
117
            $filesystem->writeStream($path, $stream, [
118
                'mimetype' => $file->getMimeType(),
119
            ]);
120
            $classMetadata->setFieldValue($object, $fieldConfiguration->property, $path);
121
            $propertyAccessor->setValue($object, $fileProperty, null);
122
        }
123
    }
124
125
    public function deleteFiles(object $object): void
126
    {
127
        $classMetadata = $this->getClassMetadata($object);
128
129
        $configuredProperties = $this->annotationReader->getConfiguredProperties($object, true, true);
130
        foreach ($configuredProperties as $fileProperty => $fieldConfiguration) {
131
            $currentFilepath = $classMetadata->getFieldValue($object, $fieldConfiguration->property);
132
            if ($currentFilepath) {
133
                $this->removeFilepath($object, $fieldConfiguration);
0 ignored issues
show
Bug introduced by
It seems like $fieldConfiguration can also be of type string; however, parameter $fieldConfiguration of Silverback\ApiComponents...elper::removeFilepath() does only seem to accept Silverback\ApiComponents...otation\UploadableField, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
                $this->removeFilepath($object, /** @scrutinizer ignore-type */ $fieldConfiguration);
Loading history...
134
            }
135
        }
136
    }
137
138
    private function removeFilepath(object $object, UploadableField $fieldConfiguration): void
139
    {
140
        $classMetadata = $this->getClassMetadata($object);
141
142
        $filesystem = $this->filesystemProvider->getFilesystem($fieldConfiguration->adapter);
0 ignored issues
show
Bug introduced by
It seems like $fieldConfiguration->adapter can also be of type null; however, parameter $name of Silverback\ApiComponents...ovider::getFilesystem() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
        $filesystem = $this->filesystemProvider->getFilesystem(/** @scrutinizer ignore-type */ $fieldConfiguration->adapter);
Loading history...
143
        $currentFilepath = $classMetadata->getFieldValue($object, $fieldConfiguration->property);
144
        if ($filesystem->fileExists($currentFilepath)) {
145
            $filesystem->delete($currentFilepath);
146
        }
147
    }
148
}
149