Adapter::keys()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.0342

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 29
ccs 16
cts 18
cp 0.8889
rs 8.439
c 1
b 0
f 1
cc 5
eloc 19
nc 8
nop 0
crap 5.0342
1
<?php
2
/*
3
 * Copyright (C) 2017 by TEQneers GmbH & Co. KG
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
/**
25
 * Git Stream Wrapper for PHP
26
 *
27
 * @category   TQ
28
 * @package    TQ_VCS
29
 * @subpackage VCS
30
 * @copyright  Copyright (C) 2018 by TEQneers GmbH & Co. KG
31
 */
32
33
namespace TQ\Vcs\Gaufrette;
34
35
use Gaufrette\Adapter as AdapterInterface;
36
use Gaufrette\Adapter\ChecksumCalculator;
37
use Gaufrette\Adapter\StreamFactory;
38
use Gaufrette\Stream\Local;
39
use Gaufrette\Util\Checksum;
40
use TQ\Vcs\Repository\RepositoryInterface;
41
42
/**
43
 * The Gaufrette VCS adapter
44
 *
45
 * @author     Stefan Gehrig <gehrigteqneers.de>
46
 * @category   TQ
47
 * @package    TQ_VCS
48
 * @subpackage VCS
49
 * @copyright  Copyright (C) 2018 by TEQneers GmbH & Co. KG
50
 */
51
class Adapter implements AdapterInterface, StreamFactory, ChecksumCalculator
52
{
53
    /**
54
     * The repository
55
     *
56
     * @var RepositoryInterface
57
     */
58
    protected $repository;
59
60
    /**
61
     * Creates a new VCS adapter to be used with a Gaufrette filesystem
62
     *
63
     * @param   RepositoryInterface     $repository
64
     */
65 28
    public function __construct(RepositoryInterface $repository)
66
    {
67 28
        $this->repository = $repository;
68 28
    }
69
70
    /**
71
     * Returns the repository
72
     *
73
     * @return  RepositoryInterface
74
     */
75 14
    public function getRepository()
76
    {
77 14
        return $this->repository;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83 2
    public function read($key)
84
    {
85 2
        return $this->repository->showFile($key);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 2
    public function write($key, $content)
92
    {
93 2
        $this->repository->writeFile($key, $content);
94 2
        return true;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100 10
    public function rename($sourceKey, $targetKey)
101
    {
102 10
        $this->repository->renameFile($sourceKey, $targetKey);
103 10
        return true;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109 2
    public function exists($key)
110
    {
111 2
        return file_exists($this->repository->resolveFullPath($key));
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     */
117 2
    public function keys()
118
    {
119
        try {
120 2
            $iterator = new \RecursiveIteratorIterator(
121 2
                new \RecursiveDirectoryIterator($this->repository->getRepositoryPath(),
122 2
                      \FilesystemIterator::SKIP_DOTS
123 2
                    | \FilesystemIterator::UNIX_PATHS
124
                )
125
            );
126
        } catch (\Exception $e) {
127
            $iterator = new \EmptyIterator;
128
        }
129
130 2
        $keys = array();
131 2
        foreach ($iterator as $file) {
132 2
            $path   = $this->repository->resolveLocalPath($file);
133 2
            if (preg_match('~\.(?:svn|git)~i', $path)) {
134 2
                continue;
135
            }
136 2
            $keys[] = $key = $path;
137 2
            if ('.' !== dirname($key)) {
138 2
                $keys[] = dirname($key);
139
            }
140
        }
141 2
        $keys   = array_unique($keys);
142 2
        sort($keys);
143
144 2
        return $keys;
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150 2
    public function mtime($key)
151
    {
152 2
        return filemtime($this->repository->resolveFullPath($key));
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158 2
    public function delete($key)
159
    {
160 2
        $this->repository->removeFile($key);
161 2
        return true;
162
    }
163
164
    /**
165
     * {@inheritDoc}
166
     */
167 2
    public function isDirectory($key)
168
    {
169 2
        return is_dir($this->repository->resolveFullPath($key));
170
    }
171
172
    /**
173
     * {@inheritDoc}
174
     */
175 2
    public function createStream($key)
176
    {
177 2
        return new Local($this->repository->resolveFullPath($key));
178
    }
179
180
    /**
181
     * {@inheritDoc}
182
     */
183 2
    public function checksum($key)
184
    {
185 2
        return Checksum::fromFile($this->repository->resolveFullPath($key));
186
    }
187
}
188