Completed
Push — develop ( f21c4e...9941da )
by Daniel
10:22
created

FixtureFileUploader   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A upload() 0 15 2
A __construct() 0 7 1
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Uploader;
4
5
use Silverback\ApiComponentBundle\Entity\Content\FileInterface;
6
use Silverback\ApiComponentBundle\Factory\Entity\Content\Component\AbstractComponentFactory;
7
use Symfony\Component\HttpFoundation\File\File;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
class FixtureFileUploader
11
{
12
    private $fileUploader;
13
    private $uploadsDir;
14
15
    public function __construct(
16
        FileUploader $fileUploader,
17
        string $projectDir = ''
18
    )
19
    {
20
        $this->fileUploader = $fileUploader;
21
        $this->uploadsDir = sprintf('%s/var/uploads/', $projectDir);
22
    }
23
24
    /**
25
     * @param AbstractComponentFactory $factory
26
     * @param array $data
27
     * @param string $file
28
     * @param string $field
29
     * @return FileInterface
30
     * @throws \Exception
31
     */
32
    public function upload(AbstractComponentFactory $factory, array $data, File $file, string $field = 'filePath'): FileInterface
33
    {
34
        $entity = $factory->create($data);
35
        if (!($entity instanceof FileInterface)) {
36
            throw new \Exception('Invalid entity returned from FixtureFileUploader::upload factory');
37
        }
38
        $uploadedFile = new UploadedFile(
39
            $file->getRealPath(),
40
            $file->getFilename(),
41
            null,
42
            null,
43
            true
44
        );
45
        $this->fileUploader->upload($entity, $field, $uploadedFile);
46
        return $entity;
47
    }
48
}
49