Test Failed
Push — develop ( f6d190...8ff4ed )
by Daniel
04:04
created

FixtureFileUploader::upload()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 3
nop 4
dl 0
loc 24
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\FIle\Uploader;
4
5
use Silverback\ApiComponentBundle\Entity\Component\FileInterface;
6
use Silverback\ApiComponentBundle\Factory\Entity\AbstractFactory;
0 ignored issues
show
Bug introduced by
The type Silverback\ApiComponentB...\Entity\AbstractFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Component\Filesystem\Filesystem;
8
use Symfony\Component\HttpFoundation\File\File;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
11
class FixtureFileUploader
12
{
13
    private $fileUploader;
14
15
    public function __construct(
16
        FileUploader $fileUploader
17
    ) {
18
        $this->fileUploader = $fileUploader;
19
    }
20
21
    /**
22
     * @param AbstractFactory $factory
23
     * @param array $data
24
     * @param File $file
25
     * @param string $field
26
     * @return \Silverback\ApiComponentBundle\Entity\Component\FileInterface
27
     * @throws \Exception
28
     */
29
    public function upload(AbstractFactory $factory, array $data, File $file, string $field = 'filePath'): FileInterface
30
    {
31
        $entity = $factory->create($data);
32
        if (!($entity instanceof FileInterface)) {
33
            throw new \Exception('Invalid entity returned from FixtureFileUploader::upload factory');
34
        }
35
        $tempFile = tmpfile();
36
        if (false === $tempFile) {
37
            throw new \Exception('Could not create temporary file');
38
        }
39
        $tempPath = stream_get_meta_data($tempFile)['uri'];
40
        fclose($tempFile);
41
42
        $fs = new Filesystem();
43
        $fs->copy($file->getRealPath(), $tempPath, true);
44
        $uploadedFile = new UploadedFile(
45
            $tempPath,
46
            $file->getFilename(),
47
            $file->getMimeType(),
48
            null,
49
            true
50
        );
51
        $this->fileUploader->upload($entity, $field, $uploadedFile);
52
        return $entity;
53
    }
54
}
55