Completed
Push — master ( 4e395d...7b9c22 )
by Nikola
06:42
created

Backup   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 77.08%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 20
c 5
b 1
f 2
lcom 1
cbo 2
dl 0
loc 152
ccs 37
cts 48
cp 0.7708
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 4
A getName() 0 4 1
A setName() 0 6 1
A addFiles() 0 8 2
A addFile() 0 7 1
A getFiles() 0 4 1
A setFiles() 0 10 2
A getSize() 0 4 1
A setSize() 0 6 1
A getCreatedAt() 0 4 1
A setCreatedAt() 0 6 2
A getModifiedAt() 0 4 1
A setModifiedAt() 0 6 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 42
    public function __construct($name, array $files = array(), $size = 0, $createdAt = null, $modifiedAt = null)
53
    {
54 42
        $this->name = Filename::sanitize($name);
55 42
        $this->size = $size;
56 42
        $this->createdAt = is_null($createdAt) ? new \DateTime('now') : $createdAt;
57 42
        $this->modifiedAt = is_null($modifiedAt) ? new \DateTime('now') : $modifiedAt;
58
59 42
        if (count($files)) {
60 18
            $this->setFiles($files);
61 18
        } else {
62 24
            $this->files = array();
63
        }
64 42
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 38
    public function getName()
70
    {
71 38
        return $this->name;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    public function setName($name)
78
    {
79 2
        $this->name = Filename::sanitize($name);
80
81 2
        return $this;
82
    }
83
84 4
    public function addFiles(array $files)
85
    {
86 4
        foreach ($files as $file) {
87 2
            $this->addFile($file);
88 4
        }
89
90 4
        return $this;
91
    }
92
93 2
    public function addFile(FileInterface $file)
94
    {
95 2
        $this->files[] = $file;
96 2
        $this->size += $file->getSize();
97
98 2
        return $this;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 26
    public function getFiles()
105
    {
106 26
        return $this->files;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 20
    public function setFiles(array $files)
113
    {
114 20
        $this->files = $files;
115 20
        $this->size = 0;
116 20
        foreach ($this->files as $file) {
117 18
            $this->size += $file->getSize();
118 20
        }
119
120 20
        return $this;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 8
    public function getSize()
127
    {
128 8
        return $this->size;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function setSize($size)
135
    {
136
        $this->size = $size;
137
138
        return $this;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 10
    public function getCreatedAt()
145
    {
146 10
        return $this->createdAt;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function setCreatedAt($createdAt)
153
    {
154
        $this->createdAt = \DateTimeImmutable::createFromMutable((is_integer($createdAt) ? date_timestamp_set(new \DateTime(), $createdAt) : $createdAt));
155
156
        return $this;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function getModifiedAt()
163
    {
164
        return $this->modifiedAt;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function setModifiedAt($modifiedAt)
171
    {
172
        $this->modifiedAt = \DateTimeImmutable::createFromMutable((is_integer($modifiedAt) ? date_timestamp_set(new \DateTime(), $modifiedAt) : $modifiedAt));
173
174
        return $this;
175
    }
176
}
177