Passed
Pull Request — master (#631)
by ANTHONIUS
08:16
created

FileUploadStrategy::getMetadata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2.5

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 1
cts 2
cp 0.5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.5
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** FileUploadStrategy.php */
11
namespace Core\Entity\Hydrator\Strategy;
12
13
use Auth\Entity\User;
14
use Core\Entity\File;
15
use Core\Entity\FileMetadataInterface;
16
use Core\Service\FileManager;
17
use Laminas\Hydrator\Strategy\StrategyInterface;
18
use Core\Entity\FileInterface;
19
20
class FileUploadStrategy implements StrategyInterface
21
{
22
    protected ?FileMetadataInterface $metadata = null;
23
    private User $user;
24
    private string $metadataClass;
25
    private FileManager $fileManager;
26
    private string $entityClass;
27 1
28
    /**
29 1
     * FileUploadStrategy constructor.
30
     *
31
     * @param FileManager $fileManager
32
     * @param User $user
33
     * @param string $metadataClass
34
     * @param string $entityClass
35
     */
36
    public function __construct(
37 1
        FileManager $fileManager,
38
        User $user,
39 1
        string $metadataClass,
40 1
        string $entityClass
41
    ){
42
43
        $this->user = $user;
44
        $this->metadataClass = $metadataClass;
45
        $this->fileManager = $fileManager;
46
        $this->entityClass = $entityClass;
47
    }
48
49
    /**
50
     * @param FileMetadataInterface $metadata
51
     *
52
     * @return $this
53
     */
54
    public function setMetadata(FileMetadataInterface $metadata)
55
    {
56
        $this->metadata = $metadata;
57
        return $this;
58
    }
59
60 1
    /**
61
     * @return FileMetadataInterface
62 1
     */
63 1
    public function getMetadata()
64
    {
65
        if (is_null($this->metadata)) {
66
            $metadata = new $this->metadataClass();
67
            $this->setMetadata($metadata);
68
        }
69
        return clone $this->metadata;
70
    }
71
72
    /**
73
     * @param mixed $value
74
     *
75
     * @param object|null $object
76
     * @return mixed|null
77
     */
78
    public function extract($value, ?object $object = null)
79
    {
80
        if (!$value instanceof FileInterface) {
81
            return null;
82
        }
83
        
84
        return $value->getId();
85
    }
86
87
    /**
88
     * @param mixed $value
89
     *
90
     * @param array|null $data
91
     * @return object|File|null
92
     */
93
    public function hydrate($value, ?array $data)
94
    {
95
        if (!UPLOAD_ERR_OK == $value['error']) {
96
            return null;
97
        }
98
        $fileManager = $this->fileManager;
99
        $metadata = $this->getMetadata();
100
        
101
        $metadata
102
            ->setContentType($value['type'])
103
            ->setUser($this->user)
104
        ;
105
106
        return $fileManager->uploadFromFile(
107
            $this->entityClass,
108
            $metadata,
109
            $value['tmp_name'],
110
            $value['name']
111
        );
112
    }
113
}
114