Completed
Pull Request — master (#237)
by Loïc
03:13 queued 01:33
created

File::getFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
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 App\Entity\Media;
15
16
use App\Entity\IdentifiableTrait;
17
use Doctrine\ORM\Mapping as ORM;
18
use Sylius\Component\Resource\Model\ResourceInterface;
19
use Sylius\Component\Resource\Model\TimestampableTrait;
20
use Symfony\Component\Serializer\Annotation as Serializer;
21
22
/**
23
 * @ORM\MappedSuperclass
24
 */
25
abstract class File implements ResourceInterface
26
{
27
    use IdentifiableTrait;
28
    use TimestampableTrait;
29
30
    protected static $uri = '/media/file';
31
32
    /** @var \SplFileInfo|null */
33
    protected $file;
34
35
    /**
36
     * @var string|null
37
     *
38
     * @ORM\Column(type="string")
39
     *
40
     * @Serializer\Groups({"Default", "Detailed"})
41
     */
42
    protected $path;
43
44
    /**
45
     * @var \DateTimeInterface|null
46
     *
47
     * @ORM\Column(type="datetime")
48
     */
49
    protected $createdAt;
50
51
    /**
52
     * @var \DateTimeInterface|null
53
     *
54
     * @ORM\Column(type="datetime", nullable=true)
55
     */
56
    protected $updatedAt;
57
58
    public function __construct()
59
    {
60
        $this->createdAt = new \DateTime();
61
    }
62
63
    public function getFile(): ?\SplFileInfo
64
    {
65
        return $this->file;
66
    }
67
68
    public function setFile(?\SplFileInfo $file): void
69
    {
70
        $this->file = $file;
71
72
        // VERY IMPORTANT:
73
        // It is required that at least one field changes if you are using Doctrine,
74
        // otherwise the event listeners won't be called and the file is lost
75
        if ($file) {
76
            // if 'updatedAt' is not defined in your entity, use another property
77
            $this->updatedAt = new \DateTime('now');
78
        }
79
    }
80
81
    public function getPath(): ?string
82
    {
83
        return $this->path;
84
    }
85
86
    public function setPath(?string $path): void
87
    {
88
        $this->path = $path;
89
    }
90
91
    public function getWebPath(): ?string
92
    {
93
        if (null == $path = $this->path) {
94
            return null;
95
        }
96
97
        return static::$uri.'/'.$path;
98
    }
99
}
100