Completed
Push — master ( c5a3b4...fa0d52 )
by Arne
01:49
created

IndexObject::equals()   C

Complexity

Conditions 10
Paths 257

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 6
c 0
b 0
f 0
cc 10
eloc 13
nc 257
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A IndexObject::getIndexRecord() 0 4 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Archivr;
4
5
use Archivr\Exception\Exception;
6
7
/**
8
 * An index object is the representation of one of this filesystem primitives contained in the index.
9
 */
10
class IndexObject
11
{
12
    const TYPE_DIR = 1;
13
    const TYPE_FILE = 2;
14
    const TYPE_LINK = 3;
15
16
    /**
17
     * @var string
18
     */
19
    protected $relativePath;
20
21
    /**
22
     * @var int
23
     */
24
    protected $type;
25
26
    /**
27
     * @var int
28
     */
29
    protected $mtime;
30
31
    /**
32
     * @var int
33
     */
34
    protected $ctime;
35
36
    /**
37
     * @var int
38
     */
39
    protected $mode;
40
41
    /**
42
     * @var int
43
     */
44
    protected $size;
45
46
    /**
47
     * @var string
48
     */
49
    protected $linkTarget;
50
51
    /**
52
     * @var string
53
     */
54
    protected $blobId;
55
56
    /**
57
     * Prevent construction not using static factory methods.
58
     */
59
    protected function __construct() {}
60
61
    public function getRelativePath(): string
62
    {
63
        return $this->relativePath;
64
    }
65
66
    public function getType(): int
67
    {
68
        return $this->type;
69
    }
70
71
    public function isDirectory(): bool
72
    {
73
        return $this->type === static::TYPE_DIR;
74
    }
75
76
    public function isFile(): bool
77
    {
78
        return $this->type === static::TYPE_FILE;
79
    }
80
81
    public function isLink(): bool
82
    {
83
        return $this->type === static::TYPE_LINK;
84
    }
85
86
    public function getMtime(): int
87
    {
88
        return $this->mtime;
89
    }
90
91
    public function getCtime(): int
92
    {
93
        return $this->ctime;
94
    }
95
96
    public function getMode(): int
97
    {
98
        return $this->mode;
99
    }
100
101
    public function getSize()
102
    {
103
        return $this->size;
104
    }
105
106
    public function getLinkTarget()
107
    {
108
        return $this->linkTarget;
109
    }
110
111
    public function getBlobId()
112
    {
113
        return $this->blobId;
114
    }
115
116
    public function setBlobId(string $blobId): IndexObject
117
    {
118
        $this->blobId = $blobId;
119
120
        return $this;
121
    }
122
123
    public function getIndexRecord(): array
124
    {
125
        return [$this->relativePath, $this->type, $this->mtime, $this->ctime, $this->mode, $this->size, $this->blobId, $this->linkTarget];
126
    }
127
128
    public static function fromIndexRecord(array $row): IndexObject
129
    {
130
        $object = new static;
131
        $object->relativePath = $row[0];
132
        $object->type = (int)$row[1];
133
        $object->mtime = (int)$row[2];
134
        $object->ctime = (int)$row[3];
135
        $object->mode = (int)$row[4];
136
        $object->size = (int)$row[5];
137
        $object->blobId = $row[6];
138
        $object->linkTarget = $row[7];
139
140
        return $object;
141
    }
142
143
    /**
144
     * Returns an instance representing the filesystem object that can be found under the given path.
145
     *
146
     * @param string $basePath
147
     * @param string $relativePath
148
     * @return IndexObject
149
     */
150
    public static function fromPath(string $basePath, string $relativePath): IndexObject
151
    {
152
        $absolutePath = rtrim($basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $relativePath;
153
154
        clearstatcache(null, $absolutePath);
155
156
        if (!($stat = @lstat($absolutePath)))
157
        {
158
            throw new Exception();
159
        }
160
161
        $object = new static;
162
        $object->relativePath = $relativePath;
163
        $object->mtime = $stat['mtime'];
164
        $object->ctime = $stat['ctime'];
165
        $object->mode = $stat['mode'];
166
167
        if (is_file($absolutePath))
168
        {
169
            $object->type = static::TYPE_FILE;
170
            $object->size = (int)$stat['size'];
171
        }
172
        elseif (is_dir($absolutePath))
173
        {
174
            $object->type = static::TYPE_DIR;
175
        }
176
        elseif (is_link($absolutePath))
177
        {
178
            $object->type = static::TYPE_LINK;
179
            $object->linkTarget = str_replace($basePath, '', readlink($absolutePath));
180
        }
181
        else
182
        {
183
            throw new \LogicException();
184
        }
185
186
        return $object;
187
    }
188
}
189