Completed
Push — master ( 2c5dd4...1c78ad )
by Nikola
02:14
created

File::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 $name;
30
31
    /**
32
     * @var string
33
     */
34
    private $path;
35
36
    /**
37
     * @var string;
38
     */
39
    private $rootPath;
40
41
    /**
42
     * @var string;
43
     */
44
    private $relativePath;
45
46
    /**
47
     * @var integer
48
     */
49
    private $size;
50
51
    /**
52
     * @var \DateTimeInterface
53
     */
54
    private $createdAt;
55
56
    /**
57
     * @var \DateTimeInterface
58
     */
59
    private $modifiedAt;
60
61
    public function __construct($name, $path, $rootPath, $size, $createdAt, $modifiedAt)
62
    {
63
        $this->name = $name;
64
        $this->path = $path;
65
        $this->rootPath = rtrim(is_null($rootPath) ? '' : $rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
66
        $this->size = $size;
67
        $this->createdAt = (is_numeric($createdAt)) ? date_timestamp_set(new \DateTime(), $createdAt) : clone $createdAt;
68
        $this->modifiedAt = (is_numeric($modifiedAt)) ? date_timestamp_set(new \DateTime(), $modifiedAt) : clone $modifiedAt;
69
        $this->relativePath = null;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getName()
76
    {
77
        return $this->name;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getPath()
84
    {
85
        return $this->path;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getRootPath()
92
    {
93
        return $this->rootPath;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getRelativePath()
100
    {
101
        if (is_null($this->relativePath)) {
102
103
            $pos = strpos($this->path, $this->rootPath);
104
105
            if ($pos === 0) {
106
                $this->relativePath = substr_replace($this->path, '', $pos, strlen($this->rootPath));
107
            } else {
108
                $this->relativePath = $this->path;
109
            }
110
        }
111
112
        return $this->relativePath;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getSize()
119
    {
120
        return $this->size;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getCreatedAt()
127
    {
128
        return clone $this->createdAt;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getModifiedAt()
135
    {
136
        return clone $this->modifiedAt;
137
    }
138
139
    /**
140
     * Create File instance from local, mounted filesystem.
141
     *
142
     * @param string $path Path to file.
143
     * @param null|string $rootPath Root path of file.
144
     * @param null|string $name Filename to use instead of original one (if provided).
145
     * @return File Created backup file instance.
146
     */
147
    public static function fromLocal($path, $rootPath = null, $name = null)
148
    {
149
        return new static(
150
            is_null($name) ? basename($path) : $name,
151
            $path,
152
            $rootPath,
153
            filesize($path),
154
            filectime($path),
155
            filemtime($path)
156
        );
157
    }
158
159
    /**
160
     * Create file instane from \SplFileInfo instance.
161
     *
162
     * @param \SplFileInfo $file
163
     * @param null|string $rootPath Root path of file.
164
     * @param null|string $name Filename to use instead of original one (if provided).
165
     * @return static
166
     */
167
    public static function fromSplFileInfo(\SplFileInfo $file, $rootPath = null, $name = null)
168
    {
169
        return new static(
170
            is_null($name) ? $file->getFilename() : $name,
171
            $file->getPath(),
172
            $rootPath,
173
            $file->getSize(),
174
            $file->getCTime(),
175
            $file->getMTime()
176
        );
177
    }
178
}