Passed
Push — master ( 617db1...ffc38e )
by Peter
02:43
created

FileDownload::toJSON()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Domain\Entities;
6
7
use AbterPhp\Admin\Domain\Entities\User;
8
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
9
use DateTime;
10
11
class FileDownload implements IStringerEntity
12
{
13
    const DATE_FORMAT = 'Y-m-d H:i:s';
14
15
    /** @var string */
16
    protected $id;
17
18
    /** @var File */
19
    protected $file;
20
21
    /** @var User */
22
    protected $user;
23
24
    /** @var DateTime */
25
    protected $downloadedAt;
26
27
    /**
28
     * @param string   $id
29
     * @param File     $file
30
     * @param User     $user
31
     * @param DateTime $downloadedAt
32
     */
33
    public function __construct(string $id, File $file, User $user, DateTime $downloadedAt = null)
34
    {
35
        $this->id           = $id;
36
        $this->file         = $file;
37
        $this->user         = $user;
38
        $this->downloadedAt = $downloadedAt ?: new DateTime();
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getId()
45
    {
46
        return $this->id;
47
    }
48
49
    /**
50
     * @param string $id
51
     */
52
    public function setId($id)
53
    {
54
        $this->id = $id;
55
    }
56
57
    /**
58
     * @return File
59
     */
60
    public function getFile(): File
61
    {
62
        return $this->file;
63
    }
64
65
    /**
66
     * @return User
67
     */
68
    public function getUser(): User
69
    {
70
        return $this->user;
71
    }
72
73
    /**
74
     * @return DateTime
75
     */
76
    public function getDownloadedAt(): DateTime
77
    {
78
        return $this->downloadedAt;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function __toString(): string
85
    {
86
        return '#' . $this->getId();
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function toJSON(): string
93
    {
94
        return json_encode(
95
            [
96
                'id'            => $this->getId(),
97
                'file'          => [
98
                    'id' => $this->getFile()->getId(),
99
                ],
100
                'user'          => [
101
                    'id' => $this->getUser(),
102
                ],
103
                'downloaded_at' => $this->getDownloadedAt(),
104
            ]
105
        );
106
    }
107
}
108