Completed
Push — master ( a2c400...8a7993 )
by Nikola
09:09
created

File   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 0

Test Coverage

Coverage 89.58%

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 15
c 6
b 0
f 3
lcom 4
cbo 0
dl 0
loc 155
ccs 43
cts 48
cp 0.8958
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 4
A getPath() 0 4 1
A getRootPath() 0 4 1
A getRelativePath() 0 15 3
A getSize() 0 4 1
A getCreatedAt() 0 4 1
A getModifiedAt() 0 4 1
A fromLocal() 0 10 1
A fromSplFileInfo() 0 10 1
A fromFlysystemMetadata() 0 10 1
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
namespace RunOpenCode\Backup\Backup;
14
15
use RunOpenCode\Backup\Contract\FileInterface;
16
17
/**
18
 * Class File
19
 *
20
 * Backup file abstraction.
21
 *
22
 * @package RunOpenCode\Backup\Backup
23
 */
24
final class File implements FileInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    private $path;
30
31
    /**
32
     * @var string;
33
     */
34
    private $rootPath;
35
36
    /**
37
     * @var string;
38
     */
39
    private $relativePath;
40
41
    /**
42
     * @var integer
43
     */
44
    private $size;
45
46
    /**
47
     * @var \DateTimeInterface
48
     */
49
    private $createdAt;
50
51
    /**
52
     * @var \DateTimeInterface
53
     */
54
    private $modifiedAt;
55
56
    public function __construct($path, $rootPath, $size, $createdAt, $modifiedAt)
57
    {
58
        $this->path = $path;
59
        $this->rootPath = rtrim(is_null($rootPath) ? '' : $rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
60
        $this->size = $size;
61 32
        $this->createdAt = (is_numeric($createdAt)) ? date_timestamp_set(new \DateTime(), $createdAt) : clone $createdAt;
62
        $this->modifiedAt = (is_numeric($modifiedAt)) ? date_timestamp_set(new \DateTime(), $modifiedAt) : clone $modifiedAt;
63 32
        $this->relativePath = null;
64 32
    }
65 32
66 32
    /**
67 32
     * {@inheritdoc}
68 32
     */
69 32
    public function getPath()
70 32
    {
71
        return $this->path;
72
    }
73
74
    /**
75 14
     * {@inheritdoc}
76
     */
77 14
    public function getRootPath()
78
    {
79
        return $this->rootPath;
80
    }
81
82
    /**
83 24
     * {@inheritdoc}
84
     */
85 24
    public function getRelativePath()
86
    {
87
        if (is_null($this->relativePath)) {
88
89
            $pos = strpos($this->path, $this->rootPath);
90
91
            if ($pos === 0) {
92
                $this->relativePath = substr_replace($this->path, '', $pos, strlen($this->rootPath));
93
            } else {
94
                $this->relativePath = $this->path;
95
            }
96
        }
97
98
        return $this->relativePath;
99 22
    }
100
101 22
    /**
102
     * {@inheritdoc}
103 22
     */
104
    public function getSize()
105 22
    {
106 22
        return $this->size;
107 22
    }
108
109
    /**
110 22
     * {@inheritdoc}
111
     */
112 22
    public function getCreatedAt()
113
    {
114
        return clone $this->createdAt;
115
    }
116
117
    /**
118 18
     * {@inheritdoc}
119
     */
120 18
    public function getModifiedAt()
121
    {
122
        return clone $this->modifiedAt;
123
    }
124
125
    /**
126
     * Create File instance from local, mounted filesystem.
127
     *
128
     * @param string $path Path to file.
129
     * @param null|string $rootPath Root path of file.
130
     * @return File Created backup file instance.
131
     */
132
    public static function fromLocal($path, $rootPath = null)
133
    {
134 2
        return new static(
135
            $path,
136 2
            $rootPath,
137
            filesize($path),
138
            filectime($path),
139
            filemtime($path)
140
        );
141
    }
142
143
    /**
144
     * Create file instance from \SplFileInfo instance.
145
     *
146
     * @param \SplFileInfo $file
147 32
     * @param null|string $rootPath Root path of file.
148
     * @return File
149 32
     */
150 32
    public static function fromSplFileInfo(\SplFileInfo $file, $rootPath = null)
151 32
    {
152 32
        return new static(
153 32
            $file->getPathname(),
154 32
            $rootPath,
155 32
            $file->getSize(),
156 32
            $file->getCTime(),
157
            $file->getMTime()
158
        );
159
    }
160
161
    /**
162
     * Create file instance from Flysystem file metadata.
163
     *
164
     * @param array $metadata Flysystem file metadata.
165
     * @param null|string $rootPath Root path of file.
166
     * @return File
167 10
     */
168
    public static function fromFlysystemMetadata(array $metadata, $rootPath = null)
169 10
    {
170 10
        return new static(
171 10
            $metadata['path'],
172 10
            $rootPath,
173 10
            $metadata['size'],
174 10
            $metadata['timestamp'],
175 10
            $metadata['timestamp']
176 10
        );
177
    }
178
}
179