Stat   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 162
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getMethod() 0 4 1
A handle() 0 17 4
A getWithMetadata() 0 26 4
A mergeMeta() 0 22 4
1
<?php
2
3
namespace Twistor\Flysystem\Plugin;
4
5
use League\Flysystem\AdapterInterface;
6
use Twistor\FlysystemStreamWrapper;
7
use Twistor\PosixUid;
8
use Twistor\Uid;
9
10
class Stat extends AbstractPlugin
11
{
12
    /**
13
     * Default return value of url_stat().
14
     *
15
     * @var array
16
     */
17
    protected static $defaultMeta = [
18
        'dev' => 0,
19
        'ino' => 0,
20
        'mode' => 0,
21
        'nlink' => 0,
22
        'uid' => 0,
23
        'gid' => 0,
24
        'rdev' => 0,
25
        'size' => 0,
26
        'atime' => 0,
27
        'mtime' => 0,
28
        'ctime' => 0,
29
        'blksize' => -1,
30
        'blocks' => -1,
31
    ];
32
33
    /**
34
     * Permission map.
35
     *
36
     * @var array
37
     */
38
    protected $permissions;
39
40
    /**
41
     * Required metadata.
42
     *
43
     * @var array
44
     */
45
    protected $required;
46
47
    /**
48
     * @var \Twistor\Uid
49
     */
50
    protected $uid;
51
52
    /**
53
     * Constructs a Stat object.
54
     *
55
     * @param array $permissions An array of permissions.
56
     * @param array $metadata    The default required metadata.
57
     */
58 213
    public function __construct(array $permissions, array $metadata)
59
    {
60 213
        $this->permissions = $permissions;
61 213
        $this->required = array_combine($metadata, $metadata);
62 213
        $this->uid = \extension_loaded('posix') ? new PosixUid() : new Uid();
63 213
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 210
    public function getMethod()
69
    {
70 210
        return 'stat';
71
    }
72
73
    /**
74
     * Emulates stat().
75
     *
76
     * @param string $path
77
     * @param int $flags
78
     *
79
     * @return array Output similar to stat().
80
     *
81
     * @throws \League\Flysystem\FileNotFoundException
82
     *
83
     * @see stat()
84
     */
85 87
    public function handle($path, $flags)
86
    {
87 87
        if ($path === '') {
88 6
            return $this->mergeMeta(['type' => 'dir', 'visibility' => AdapterInterface::VISIBILITY_PUBLIC]);
89
        }
90
91 81
        $ignore = $flags & FlysystemStreamWrapper::STREAM_URL_IGNORE_SIZE ? ['size'] : [];
92
93 81
        $metadata = $this->getWithMetadata($path, $ignore);
94
95
        // It's possible for getMetadata() to fail even if a file exists.
96 69
        if (empty($metadata)) {
97 3
            return static::$defaultMeta;
98
        }
99
100 69
        return $this->mergeMeta($metadata + ['visibility' => AdapterInterface::VISIBILITY_PUBLIC]);
101
    }
102
103
    /**
104
     * Returns metadata.
105
     *
106
     * @param string $path The path to get metadata for.
107
     * @param array $ignore Metadata to ignore.
108
     *
109
     * @return array The metadata as returned by Filesystem::getMetadata().
110
     *
111
     * @throws \League\Flysystem\FileNotFoundException
112
     *
113
     * @see \League\Flysystem\Filesystem::getMetadata()
114
     */
115 81
    protected function getWithMetadata($path, array $ignore)
116
    {
117 81
        $metadata = $this->filesystem->getMetadata($path);
118
119 69
        if (empty($metadata)) {
120 3
            return [];
121
        }
122
123 69
        $keys = array_diff($this->required, array_keys($metadata), $ignore);
124
125 69
        foreach ($keys as $key) {
126 69
            $method = 'get' . ucfirst($key);
127
128
            try {
129 69
                $metadata[$key] = $this->filesystem->$method($path);
130 25
            } catch (\LogicException $e) {
131
                // Some adapters don't support certain metadata. For instance,
132
                // the Dropbox adapter throws exceptions when calling
133
                // getVisibility(). Remove the required key so we don't keep
134
                // calling it.
135 47
                unset($this->required[$key]);
136
            }
137 23
        }
138
139 69
        return $metadata;
140
    }
141
142
    /**
143
     * Merges the available metadata from Filesystem::getMetadata().
144
     *
145
     * @param array $metadata The metadata.
146
     *
147
     * @return array All metadata with default values filled in.
148
     */
149 75
    protected function mergeMeta(array $metadata)
150
    {
151 75
        $ret = static::$defaultMeta;
152
153 75
        $ret['uid'] = $this->uid->getUid();
154 75
        $ret['gid'] = $this->uid->getGid();
155
156 75
        $ret['mode'] = $metadata['type'] === 'dir' ? 040000 : 0100000;
157 75
        $ret['mode'] += $this->permissions[$metadata['type']][$metadata['visibility']];
158
159 75
        if (isset($metadata['size'])) {
160 69
            $ret['size'] = (int) $metadata['size'];
161 23
        }
162 75
        if (isset($metadata['timestamp'])) {
163 69
            $ret['mtime'] = (int) $metadata['timestamp'];
164 69
            $ret['ctime'] = (int) $metadata['timestamp'];
165 23
        }
166
167 75
        $ret['atime'] = time();
168
169 75
        return array_merge(array_values($ret), $ret);
170
    }
171
}
172