Completed
Push — feature/admin-avatar ( a097f4...669194 )
by Loïc
01:46
created

File::getWebPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
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 string|null
46
     *
47
     * @ORM\Column(type="string", nullable=true)
48
     */
49
    protected $type;
50
51
    /**
52
     * @var \DateTimeInterface|null
53
     *
54
     * @ORM\Column(type="datetime")
55
     */
56
    protected $createdAt;
57
58
    /**
59
     * @var \DateTimeInterface|null
60
     *
61
     * @ORM\Column(type="datetime", nullable=true)
62
     */
63
    protected $updatedAt;
64
65
    public function __construct()
66
    {
67
        $this->createdAt = new \DateTime();
68
    }
69
70
    public function getFile(): ?\SplFileInfo
71
    {
72
        return $this->file;
73
    }
74
75
    public function setFile(?\SplFileInfo $file): void
76
    {
77
        $this->file = $file;
78
79
        // VERY IMPORTANT:
80
        // It is required that at least one field changes if you are using Doctrine,
81
        // otherwise the event listeners won't be called and the file is lost
82
        if ($file) {
83
            // if 'updatedAt' is not defined in your entity, use another property
84
            $this->updatedAt = new \DateTime('now');
85
        }
86
    }
87
88
    public function getPath(): ?string
89
    {
90
        return $this->path;
91
    }
92
93
    public function setPath(?string $path): void
94
    {
95
        $this->path = $path;
96
    }
97
98
    public function getType(): ?string
99
    {
100
        return $this->type;
101
    }
102
103
    public function setType(?string $type): void
104
    {
105
        $this->type = $type;
106
    }
107
108
    public function getWebPath(): ?string
109
    {
110
        if (null == $path = $this->path) {
111
            return null;
112
        }
113
114
        return static::$uri.'/'.$path;
115
    }
116
}
117