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

FixtureFileUploader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
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