Completed
Push — master ( 8a7993...1a063d )
by Nikola
05:00
created

Backup::addFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
/*
3
 * This file is part of the Backup package, an RunOpenCode project.
4
 *
5
 * (c) 2015 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * This project is fork of "kbond/php-backup", for full credits info, please
11
 * view CREDITS file that was distributed with this source code.
12
 */
13
14
namespace RunOpenCode\Backup\Backup;
15
16
use RunOpenCode\Backup\Contract\FileInterface;
17
use RunOpenCode\Backup\Contract\BackupInterface;
18
use RunOpenCode\Backup\Utils\Filename;
19
20
/**
21
 * Class Backup.
22
 *
23
 * Backup is abstraction of collection of files for backup.
24
 */
25
final class Backup implements BackupInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    private $name;
31
32
    /**
33
     * @var FileInterface[]
34
     */
35
    private $files;
36
37
    /**
38
     * @var int
39
     */
40
    private $size;
41
42
    /**
43
     * @var \DateTimeInterface
44
     */
45
    private $createdAt;
46
47
    /**
48
     * @var \DateTimeInterface
49
     */
50
    private $modifiedAt;
51
52 28
    public function __construct($name, array $files = array(), $size = 0, $createdAt = null, $modifiedAt = null)
53
    {
54 28
        $this->name = Filename::sanitize($name);
55 28
        $this->size = $size;
56 28
        $this->createdAt = is_null($createdAt) ? new \DateTime('now') : $createdAt;
57 28
        $this->modifiedAt = is_null($modifiedAt) ? new \DateTime('now') : $modifiedAt;
58
59 28
        if (count($files)) {
60 18
            $this->setFiles($files);
61 9
        }
62 28
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 28
    public function getName()
68
    {
69 28
        return $this->name;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function setName($name)
76
    {
77
        $this->name = Filename::sanitize($name);
78
79
        return $this;
80
    }
81
82
    public function addFiles(array $files)
83
    {
84
        foreach ($files as $file) {
85
            $this->addFile($file);
86
        }
87
88
        return $this;
89
    }
90
91
    public function addFile(FileInterface $file)
92
    {
93
        $this->files[] = $file;
94
        $this->size += $file->getSize();
95
96
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 18
    public function getFiles()
103
    {
104 18
        return $this->files;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 18
    public function setFiles(array $files)
111
    {
112 18
        $this->files = $files;
113 18
        $this->size = 0;
114 18
        foreach ($this->files as $file) {
115 18
            $this->size += $file->getSize();
116 9
        }
117
118 18
        return $this;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 8
    public function getSize()
125
    {
126 8
        return $this->size;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function setSize($size)
133
    {
134
        $this->size = $size;
135
136
        return $this;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 10
    public function getCreatedAt()
143
    {
144 10
        return $this->createdAt;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function setCreatedAt($createdAt)
151
    {
152
        $this->createdAt = \DateTimeImmutable::createFromMutable((is_integer($createdAt) ? date_timestamp_set(new \DateTime(), $createdAt) : $createdAt));
153
154
        return $this;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function getModifiedAt()
161
    {
162
        return $this->modifiedAt;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function setModifiedAt($modifiedAt)
169
    {
170
        $this->modifiedAt = \DateTimeImmutable::createFromMutable((is_integer($modifiedAt) ? date_timestamp_set(new \DateTime(), $modifiedAt) : $modifiedAt));
171
172
        return $this;
173
    }
174
}
175