Passed
Push — feature/publishable ( c26268...7bd202 )
by Daniel
18:12 queued 11:31
created

FileHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
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 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