Completed
Push — master ( 212b1e...c0d8c8 )
by Sebastian
01:56
created

File::__construct()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 6
nop 1
dl 0
loc 16
ccs 13
cts 13
cp 1
crap 5
rs 8.8571
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
        $modDate      = !empty($ftpInfo['modify'])
79 1
                      ? DateTimeImmutable::createFromFormat('YmdHis', $ftpInfo['modify'])
80 5
                      : false;
81 5
        $this->modify = !empty($modDate) ? $modDate : new DateTimeImmutable();
82 5
    }
83
84
    /**
85
     * Return unique identifier.
86
     *
87
     * @return string
88
     */
89 1
    public function getUniqueName() : string
90
    {
91 1
        return $this->unique;
92
    }
93
94
    /**
95
     * Return last modification date.
96
     *
97
     * @return \DateTimeImmutable
98
     */
99 1
    public function getLastModifyDate() : DateTimeImmutable
100
    {
101 1
        return $this->modify;
102
    }
103
104
    /**
105
     * Return the file name.
106
     *
107
     * @return string
108
     */
109 1
    public function getFilename() : string
110
    {
111 1
        return $this->name;
112
    }
113
114
    /**
115
     * Return file size in bytes.
116
     *
117
     * @return int
118
     */
119 1
    public function getSize() : int
120
    {
121 1
        return $this->size;
122
    }
123
124
    /**
125
     * Return true if a directory.
126
     *
127
     * @return bool
128
     */
129 2
    public function isDir() : bool
130
    {
131 2
        return $this->type === 'dir';
132
    }
133
134
    /**
135
     * Return true if not a directory.
136
     *
137
     * @return bool
138
     */
139 3
    public function isFile() : bool
140
    {
141 3
        return $this->type === 'file';
142
    }
143
}
144