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

File::fromLocal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4286
cc 1
eloc 7
nc 1
nop 2
crap 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 32
    public function __construct($path, $rootPath, $size, $createdAt, $modifiedAt)
57
    {
58 32
        $this->path = $path;
59 32
        $this->rootPath = rtrim(is_null($rootPath) ? '' : $rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
60 32
        $this->size = $size;
61 32
        $this->createdAt = (is_numeric($createdAt)) ? date_timestamp_set(new \DateTime(), $createdAt) : clone $createdAt;
62 32
        $this->modifiedAt = (is_numeric($modifiedAt)) ? date_timestamp_set(new \DateTime(), $modifiedAt) : clone $modifiedAt;
63 32
        $this->relativePath = null;
64 32
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 24
    public function getPath()
70
    {
71 24
        return $this->path;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getRootPath()
78
    {
79
        return $this->rootPath;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 24
    public function getRelativePath()
86
    {
87 24
        if (is_null($this->relativePath)) {
88
89 24
            $pos = strpos($this->path, $this->rootPath);
90
91 24
            if ($pos === 0) {
92 24
                $this->relativePath = substr_replace($this->path, '', $pos, strlen($this->rootPath));
93 12
            } else {
94
                $this->relativePath = $this->path;
95
            }
96 12
        }
97
98 24
        return $this->relativePath;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 18
    public function getSize()
105
    {
106 18
        return $this->size;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getCreatedAt()
113
    {
114
        return clone $this->createdAt;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 2
    public function getModifiedAt()
121
    {
122 2
        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 32
    public static function fromLocal($path, $rootPath = null)
133
    {
134 32
        return new static(
135 16
            $path,
136 16
            $rootPath,
137 16
            filesize($path),
138 16
            filectime($path),
139 16
            filemtime($path)
140 16
        );
141
    }
142
143
    /**
144
     * Create file instance from \SplFileInfo instance.
145
     *
146
     * @param \SplFileInfo $file
147
     * @param null|string $rootPath Root path of file.
148
     * @return File
149
     */
150 10
    public static function fromSplFileInfo(\SplFileInfo $file, $rootPath = null)
151
    {
152 10
        return new static(
153 10
            $file->getPathname(),
154 5
            $rootPath,
155 10
            $file->getSize(),
156 10
            $file->getCTime(),
157 10
            $file->getMTime()
158 5
        );
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
     */
168 4
    public static function fromFlysystemMetadata(array $metadata, $rootPath = null)
169
    {
170 4
        return new static(
171 4
            $metadata['path'],
172 2
            $rootPath,
173 4
            $metadata['size'],
174 4
            $metadata['timestamp'],
175 4
            $metadata['timestamp']
176 2
        );
177
    }
178
}
179