FilesystemMetadata::fixWindowsPath()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
crap 12
1
<?php
2
3
/*
4
 * This file is part of the puli/repository package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Repository\Resource\Metadata;
13
14
use Puli\Repository\Api\Resource\ResourceMetadata;
15
16
/**
17
 * Metadata about a file on the filesystem.
18
 *
19
 * @since  1.0
20
 *
21
 * @author Bernhard Schussek <[email protected]>
22
 */
23
class FilesystemMetadata extends ResourceMetadata
24
{
25
    private $filesystemPath;
26
27
    public function __construct($filesystemPath)
28
    {
29
        $this->filesystemPath = $filesystemPath;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getCreationTime()
36
    {
37
        if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
38
            $path = $this->fixWindowsPath($this->filesystemPath);
39
            clearstatcache(true, $path);
40
41
            return filectime($path);
42
        }
43
44
        // On Unix, filectime() returns the change time of the inode, not the
45
        // creation time.
46
        return 0;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getAccessTime()
53
    {
54
        $path = $this->fixWindowsPath($this->filesystemPath);
55
        clearstatcache(true, $path);
56
57
        return fileatime($path);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getModificationTime()
64
    {
65
        $path = $this->fixWindowsPath($this->filesystemPath);
66
        clearstatcache(true, $path);
67
68
        return filemtime($path);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getSize()
75
    {
76
        $path = $this->fixWindowsPath($this->filesystemPath);
77
        clearstatcache(true, $path);
78
79
        return filesize($path);
80
    }
81
82
    /**
83
     * On Windows, fileXtime functions see only changes
84
     * on the symlink file and not the original one.
85
     *
86
     * @param string $path
87
     *
88
     * @return string
89
     */
90
    private function fixWindowsPath($path)
91
    {
92
        if (is_link($path) && defined('PHP_WINDOWS_VERSION_MAJOR')) {
93
            $path = readlink($path);
94
        }
95
96
        return $path;
97
    }
98
}
99