Completed
Push — master ( d7c148...f76de2 )
by Nikola
02:27
created

File::getSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
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
67
        $pos = strpos($this->path, $this->rootPath);
68
69
        if ($pos === 0) {
70
            $this->relativePath = substr_replace($this->path, '', $pos, strlen($this->rootPath));
71
        } else {
72
            $this->relativePath = $this->path;
73
        }
74
75
        $this->size = $size;
76
        $this->createdAt = \DateTimeImmutable::createFromMutable((is_integer($createdAt) ? date_timestamp_set(new \DateTime(), $createdAt) : $createdAt));
77
        $this->modifiedAt = \DateTimeImmutable::createFromMutable((is_integer($modifiedAt) ? date_timestamp_set(new \DateTime(), $modifiedAt) : $modifiedAt));
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getName()
84
    {
85
        return $this->name;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getPath()
92
    {
93
        return $this->path;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getRootPath()
100
    {
101
        return $this->rootPath;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function getRelativePath()
108
    {
109
        return $this->relativePath;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function getSize()
116
    {
117
        return $this->size;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function getCreatedAt()
124
    {
125
        return $this->createdAt;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function getModifiedAt()
132
    {
133
        return $this->modifiedAt;
134
    }
135
136
    /**
137
     * Create File instance from local, mounted filesystem.
138
     *
139
     * @param string $path Path to file.
140
     * @param null|string $rootPath Root path of file.
141
     * @param null|string $name Filename to use instead of original one (if provided).
142
     * @return File Created backup file instance.
143
     */
144
    public static function fromLocal($path, $rootPath = null, $name = null)
145
    {
146
        return new static(
147
            is_null($name) ? basename($path) : $name,
148
            $path,
149
            $rootPath,
150
            filesize($path),
151
            filectime($path),
152
            filemtime($path)
153
        );
154
    }
155
}