Passed
Pull Request — master (#52)
by Daniel
05:37
created

FileHelper::setUploadedFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 17
ccs 0
cts 11
cp 0
rs 9.9332
cc 2
nc 2
nop 2
crap 6
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 Silverback\ApiComponentBundle\Annotation\File;
20
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException;
21
use Silverback\ApiComponentBundle\Flysystem\FilesystemProvider;
22
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait;
23
use Symfony\Component\HttpFoundation\FileBag;
24
use Symfony\Component\PropertyAccess\PropertyAccess;
25
26
/**
27
 * @author Daniel West <[email protected]>
28
 */
29
final class FileHelper extends AbstractHelper
30
{
31
    use ClassMetadataTrait;
32
33
    private FilesystemProvider $filesystemProvider;
34
35 9
    public function __construct(Reader $reader, ManagerRegistry $registry, FilesystemProvider $filesystemProvider)
36
    {
37 9
        $this->filesystemProvider = $filesystemProvider;
38 9
        $this->initRegistry($registry);
39 9
        $this->initReader($reader);
40 9
    }
41
42
    /**
43
     * @param object|string $class
44
     */
45 9
    public function getConfiguration($class): File
46
    {
47 9
        return $this->getAnnotationConfiguration($class, File::class);
48
    }
49
50
    public function setUploadedFile(object $resource, FileBag $fileBag)
51
    {
52
        if (!$this->isConfigured($resource)) {
53
            throw new InvalidArgumentException('%s is not configured as a File');
54
        }
55
        $configuration = $this->getConfiguration($resource);
56
        $fileField = $configuration->fileFieldName;
57
        $uploadedFile = $fileBag->get($fileField);
58
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
59
        $propertyAccessor->setValue($resource, $fileField, $uploadedFile);
60
61
        $classMetadata = $this->getClassMetadata($resource);
62
63
        // This is set now so that we will always trigger the doctrine lifecycle events to later persist this file to a filesystem
64
        $classMetadata->setFieldValue($resource, $configuration->uploadedAtFieldName, new \DateTime());
65
66
        return $resource;
67
    }
68
69
    public function persistUploadedFile(object $resource, ?array $entityChangeSet = null): void
70
    {
71
        if (!$this->isConfigured($resource)) {
72
            throw new InvalidArgumentException('%s is not configured as a File');
73
        }
74
        $configuration = $this->getConfiguration($resource);
75
76
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
77
        $file = $propertyAccessor->getValue($resource, $configuration->fileFieldName);
78
        if (!$file) {
79
            return;
80
        }
81
82
        $filesystem = $this->getFilesystem('local');
83
84
        $stream = fopen($file->getRealPath(), 'r');
85
86
        // Need to resolve the path
87
        $path = 'test_file';
88
89
        $filesystem->writeStream($path, $stream, [
90
            'mimetype' => $file->getMimeType(),
91
        ]);
92
93
        $classMetadata = $this->getClassMetadata($resource);
94
        $classMetadata->setFieldValue($resource, $configuration->filePathFieldName, $path);
95
    }
96
97
    public function removeFile(object $resource): void
98
    {
99
        $fs = $this->getFilesystem('local');
100
101
        // Need to resolve the path
102
        $path = 'test_file';
103
104
        $fs->delete($path);
105
    }
106
107
    private function getFilesystem($adapterName): Filesystem
108
    {
109
        $flysystemAdapter = $this->filesystemProvider->getAdapter($adapterName);
110
111
        return new Filesystem($flysystemAdapter);
112
    }
113
}
114