Completed
Push — master ( 63b887...b350e3 )
by Arne
01:47
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

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