Completed
Push — master ( c0d8c8...36af8e )
by Sebastian
01:51
created

File::setModificationDate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of SebastianFeldmann\Ftp.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\Ftp;
11
12
use DateTimeImmutable;
13
use RuntimeException;
14
15
/**
16
 * Class File
17
 *
18
 * @package SebastianFeldmann\Ftp
19
 * @author  Sebastian Feldmann <[email protected]>
20
 * @link    https://github.com/sebastianfeldmann/ftp
21
 * @since   Class available since Release 1.0.0
22
 */
23
class File
24
{
25
    /**
26
     * Unique file id.
27
     *
28
     * @var string
29
     */
30
    private $unique;
31
32
    /**
33
     * Last modification date.
34
     *
35
     * @var \DateTimeImmutable
36
     */
37
    private $modify;
38
39
    /**
40
     * Filename.
41
     *
42
     * @var string
43
     */
44
    private $name;
45
46
    /**
47
     * Size in bytes.
48
     *
49
     * @var int
50
     */
51
    private $size;
52
53
    /**
54
     * File type 'dir' or 'file'.
55
     *
56
     * @var string
57
     */
58
    private $type;
59
60
    /**
61
     * File constructor.
62
     *
63
     * @param  array $ftpInfo
64
     * @throws \Exception
65
     */
66 7
    public function __construct(array $ftpInfo)
67
    {
68 7
        if (!isset($ftpInfo['type'])) {
69 1
            throw new RuntimeException('invalid file info, index type is missing');
70
        }
71 6
        if (!isset($ftpInfo['name'])) {
72 1
            throw new RuntimeException('invalid file info, index name is missing');
73
        }
74 5
        $this->type   = $ftpInfo['type'];
75 5
        $this->name   = $ftpInfo['name'];
76 5
        $this->unique = $ftpInfo['unique'] ?? '';
77 5
        $this->size   = $ftpInfo['size']   ?? 0;
78 5
        $this->setModificationDate($ftpInfo);
79 5
    }
80
81
    /**
82
     * Set modification date.
83
     *
84
     * @param  array $ftpInfo
85
     * @throws \Exception
86
     */
87 5
    private function setModificationDate(array $ftpInfo)
88
    {
89 5
        $modDate      = !empty($ftpInfo['modify'])
90 1
                      ? DateTimeImmutable::createFromFormat('YmdHis', $ftpInfo['modify'])
91 5
                      : false;
92 5
        $this->modify = !empty($modDate) ? $modDate : new DateTimeImmutable();
93 5
    }
94
95
    /**
96
     * Return unique identifier.
97
     *
98
     * @return string
99
     */
100 1
    public function getUniqueName() : string
101
    {
102 1
        return $this->unique;
103
    }
104
105
    /**
106
     * Return last modification date.
107
     *
108
     * @return \DateTimeImmutable
109
     */
110 1
    public function getLastModifyDate() : DateTimeImmutable
111
    {
112 1
        return $this->modify;
113
    }
114
115
    /**
116
     * Return the file name.
117
     *
118
     * @return string
119
     */
120 1
    public function getFilename() : string
121
    {
122 1
        return $this->name;
123
    }
124
125
    /**
126
     * Return file size in bytes.
127
     *
128
     * @return int
129
     */
130 1
    public function getSize() : int
131
    {
132 1
        return $this->size;
133
    }
134
135
    /**
136
     * Return true if a directory.
137
     *
138
     * @return bool
139
     */
140 2
    public function isDir() : bool
141
    {
142 2
        return $this->type === 'dir';
143
    }
144
145
    /**
146
     * Return true if not a directory.
147
     *
148
     * @return bool
149
     */
150 3
    public function isFile() : bool
151
    {
152 3
        return $this->type === 'file';
153
    }
154
}
155