Passed
Pull Request — master (#52)
by Vincent
11:12 queued 04:00
created

FileHelper::getConfiguration()   A

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 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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\Helper;
15
16
use Doctrine\Common\Annotations\Reader;
17
use Doctrine\Persistence\ManagerRegistry;
18
use League\Flysystem\Filesystem;
19
use Ramsey\Uuid\Uuid;
20
use Silverback\ApiComponentBundle\Annotation\File;
21
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException;
22
use Silverback\ApiComponentBundle\Flysystem\FilesystemProvider;
23
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait;
24
use Symfony\Component\HttpFoundation\FileBag;
25
use Symfony\Component\PropertyAccess\PropertyAccess;
26
27
/**
28
 * @author Daniel West <[email protected]>
29
 */
30
final class FileHelper extends AbstractHelper
31
{
32
    use ClassMetadataTrait;
33
34
    private FilesystemProvider $filesystemProvider;
35
36 9
    public function __construct(Reader $reader, ManagerRegistry $registry, FilesystemProvider $filesystemProvider)
37
    {
38 9
        $this->filesystemProvider = $filesystemProvider;
39 9
        $this->initRegistry($registry);
40 9
        $this->initReader($reader);
41 9
    }
42
43
    /**
44
     * @param object|string $class
45
     */
46 9
    public function getConfiguration($class): File
47
    {
48 9
        return $this->getClassAnnotationConfiguration($class, File::class);
49
    }
50
51
    public function setUploadedFile(object $resource, FileBag $fileBag)
52
    {
53
        if (!$this->isConfigured($resource)) {
54
            throw new InvalidArgumentException('%s is not configured as a File');
55
        }
56
        $configuration = $this->getConfiguration($resource);
57
        $fileField = $configuration->fileFieldName;
58
        $uploadedFile = $fileBag->get($fileField);
59
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
60
        $propertyAccessor->setValue($resource, $fileField, $uploadedFile);
61
62
        return $resource;
63
    }
64
65
    public function persistUploadedFile(object $resource, ?array $entityChangeSet = null): void
66
    {
67
        if (!$this->isConfigured($resource)) {
68
            throw new InvalidArgumentException('%s is not configured as a File');
69
        }
70
        $configuration = $this->getConfiguration($resource);
71
72
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
73
        $file = $propertyAccessor->getValue($resource, $configuration->fileFieldName);
74
        if (!$file) {
75
            return;
76
        }
77
78
        $filesystem = $this->getFilesystem($configuration->adapter);
79
80
        $stream = fopen($file->getRealPath(), 'r');
81
82
        $path = Uuid::uuid4()->getHex()->toString();
83
84
        $filesystem->writeStream($path, $stream, [
85
            'mimetype' => $file->getMimeType(),
86
        ]);
87
88
        $classMetadata = $this->getClassMetadata($resource);
89
        $classMetadata->setFieldValue($resource, $configuration->filePathFieldName, $path);
90
    }
91
92
    public function removeFile(object $resource): void
93
    {
94
        $this->getFilesystem($this->getConfiguration($resource)->adapter)->delete(Uuid::uuid4()->getHex()->toString());
95
    }
96
97
    private function getFilesystem($adapterName): Filesystem
98
    {
99
        return new Filesystem($this->filesystemProvider->getAdapter($adapterName));
100
    }
101
}
102