Passed
Push — master ( 8bcb22...ee485a )
by Peter
02:20
created

FileDownload::toData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 1
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|null $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 array|null
91
     */
92
    public function toData(): ?array
93
    {
94
        return [
95
            'id'            => $this->getId(),
96
            'file'          => [
97
                'id' => $this->getFile()->getId(),
98
            ],
99
            'user'          => [
100
                'id' => $this->getUser()->getId(),
101
            ],
102
            'downloaded_at' => $this->getDownloadedAt(),
103
        ];
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function toJSON(): string
110
    {
111
        return json_encode($this->toData());
112
    }
113
}
114