Completed
Push — master ( 27b3cb...f1c80d )
by Nikola
02:33
created

Backup   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 158
Duplicated Lines 11.39 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 71.15%

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 26
c 6
b 1
f 2
lcom 3
cbo 2
dl 18
loc 158
ccs 37
cts 52
cp 0.7115
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 2
A setCreatedAt() 9 9 4
A getModifiedAt() 0 4 2
A setModifiedAt() 9 9 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Exception\BackupException;
19
use RunOpenCode\Backup\Utils\Filename;
20
21
/**
22
 * Class Backup.
23
 *
24
 * Backup is abstraction of collection of files for backup.
25
 */
26
final class Backup implements BackupInterface
27
{
28
    /**
29
     * @var string
30
     */
31
    private $name;
32
33
    /**
34
     * @var FileInterface[]
35
     */
36
    private $files;
37
38
    /**
39
     * @var int
40
     */
41
    private $size;
42
43
    /**
44
     * @var \DateTimeInterface
45
     */
46
    private $createdAt;
47
48
    /**
49
     * @var \DateTimeInterface
50
     */
51
    private $modifiedAt;
52
53 54
    public function __construct($name, array $files = array(), $size = 0, $createdAt = null, $modifiedAt = null)
54
    {
55 54
        $this->name = Filename::sanitize($name);
56 54
        $this->size = $size;
57 54
        $this->createdAt = is_null($createdAt) ? new \DateTime('now') : $createdAt;
58 54
        $this->modifiedAt = is_null($modifiedAt) ? new \DateTime('now') : $modifiedAt;
59
60 54
        if (count($files)) {
61 18
            $this->setFiles($files);
62 18
        } else {
63 36
            $this->files = array();
64
        }
65 54
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 44
    public function getName()
71
    {
72 44
        return $this->name;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 2
    public function setName($name)
79
    {
80 2
        $this->name = Filename::sanitize($name);
81
82 2
        return $this;
83
    }
84
85 6
    public function addFiles(array $files)
86
    {
87 6
        foreach ($files as $file) {
88 2
            $this->addFile($file);
89 6
        }
90
91 6
        return $this;
92
    }
93
94 2
    public function addFile(FileInterface $file)
95
    {
96 2
        $this->files[] = $file;
97 2
        $this->size += $file->getSize();
98
99 2
        return $this;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 28
    public function getFiles()
106
    {
107 28
        return $this->files;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 20
    public function setFiles(array $files)
114
    {
115 20
        $this->files = $files;
116 20
        $this->size = 0;
117 20
        foreach ($this->files as $file) {
118 18
            $this->size += $file->getSize();
119 20
        }
120
121 20
        return $this;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 8
    public function getSize()
128
    {
129 8
        return $this->size;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function setSize($size)
136
    {
137
        $this->size = $size;
138
139
        return $this;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 10
    public function getCreatedAt()
146
    {
147 10
        return (!is_null($this->createdAt)) ? clone $this->createdAt : null;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 View Code Duplication
    public function setCreatedAt($createdAt)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        if (!is_integer($createdAt) && !$createdAt instanceof \DateTime) {
156
            throw new \InvalidArgumentException(sprintf('Integer or \DateTime expected, got: "%s".', BackupException::typeOf($createdAt)));
157
        }
158
159
        $this->createdAt = is_integer($createdAt) ? date_timestamp_set(new \DateTime(), $createdAt) : $createdAt;
160
        return $this;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function getModifiedAt()
167
    {
168
        return (!is_null($this->modifiedAt)) ? clone $this->modifiedAt : null;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174 View Code Duplication
    public function setModifiedAt($modifiedAt)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        if (!is_integer($modifiedAt) && !$modifiedAt instanceof \DateTime) {
177
            throw new \InvalidArgumentException(sprintf('Integer or \DateTime expected, got: "%s".', BackupException::typeOf($modifiedAt)));
178
        }
179
180
        $this->modifiedAt = is_integer($modifiedAt) ? date_timestamp_set(new \DateTime(), $modifiedAt) : $modifiedAt;
181
        return $this;
182
    }
183
}
184