Passed
Pull Request — master (#54)
by Vincent
06:15
created

FileHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 46.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 13
c 1
b 0
f 0
dl 0
loc 30
ccs 7
cts 15
cp 0.4667
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getConfiguration() 0 3 1
A uploadFile() 0 11 2
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 Silverback\ApiComponentBundle\Annotation\File;
19
use Silverback\ApiComponentBundle\Exception\InvalidArgumentException;
20
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait;
21
use Symfony\Component\HttpFoundation\FileBag;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
final class FileHelper extends AbstractHelper
27
{
28
    use ClassMetadataTrait;
29
30 9
    public function __construct(Reader $reader, ManagerRegistry $registry)
31
    {
32 9
        $this->reader = $reader;
33 9
        $this->initRegistry($registry);
34 9
        $this->initReader($reader);
35 9
    }
36
37
    /**
38
     * @param object|string $class
39
     */
40 9
    public function getConfiguration($class): File
41
    {
42 9
        return $this->getAnnotationConfiguration($class, File::class);
43
    }
44
45
    public function uploadFile(object $resource, FileBag $fileBag)
46
    {
47
        if (!$this->isConfigured($resource)) {
48
            throw new InvalidArgumentException('%s is not configured as a File');
49
        }
50
        $configuration = $this->getConfiguration($resource);
51
        $classMetadata = $this->getClassMetadata($resource);
52
        $uploadedFile = $fileBag->get($configuration->fileFieldName);
53
        $classMetadata->setFieldValue($resource, $configuration->filePathFieldName, '/uploaded_file_path');
54
55
        return $resource;
56
    }
57
}
58