Completed
Push — master ( b3ac2d...7fdc5c )
by Joschi
02:17
created

Tools::writer()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4
Metric Value
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 8.9197
cc 4
eloc 11
nc 3
nop 2
crap 4
1
<?php
2
3
/**
4
 * apparat-resource
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Resource
8
 * @subpackage  Apparat\Resource\Infrastructure
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Resource\Ports;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Resource\Domain\Contract\ReaderInterface;
41
use Apparat\Resource\Domain\Contract\WriterInterface;
42
use Apparat\Resource\Infrastructure\Io\File\AbstractFileReaderWriter;
43
use Apparat\Resource\Infrastructure\Io\File\Reader as FileReader;
44
use Apparat\Resource\Infrastructure\Io\File\Writer as FileWriter;
45
use Apparat\Resource\Infrastructure\Io\InMemory\AbstractInMemoryReaderWriter;
46
use Apparat\Resource\Infrastructure\Io\InMemory\Reader as InMemoryReader;
47
use Apparat\Resource\Infrastructure\Io\InMemory\Writer as InMemoryWriter;
48
use Apparat\Resource\Infrastructure\Service\Copy;
49
use Apparat\Resource\Infrastructure\Service\Delete;
50
use Apparat\Resource\Infrastructure\Service\Move;
51
52
/**
53
 * API tools
54
 *
55
 * @package     Apparat\Resource
56
 * @subpackage  Apparat\Resource\Infrastructure
57
 */
58
class Tools
59
{
60
    /**
61
     * Reader classes for stream wrappers
62
     *
63
     * @var array
64
     */
65
    protected static $reader = array(
66
        AbstractFileReaderWriter::WRAPPER => FileReader::class,
67
        AbstractInMemoryReaderWriter::WRAPPER => InMemoryReader::class,
68
    );
69
70
    /**
71
     * Writer classes for stream wrappers
72
     *
73
     * @var array
74
     */
75
    protected static $writer = array(
76
        AbstractFileReaderWriter::WRAPPER => FileWriter::class,
77
        AbstractInMemoryReaderWriter::WRAPPER => InMemoryWriter::class,
78
    );
79
80
    /**
81
     * Find and instantiate a reader for a particular source
82
     *
83
     * @param string $src Source
84
     * @param array $parameters Parameters
85
     * @return null|ReaderInterface  Reader instance
86
     */
87 28
    public static function reader(&$src, array $parameters = array())
88
    {
89 28
        $reader = null;
90
91
        // Run through all registered readers
92 28
        foreach (self::$reader as $wrapper => $readerClass) {
93 28
            $wrapperLength = strlen($wrapper);
94
95
            // If this wrapper is used: Instantiate the reader and resource
96 28
            if ($wrapperLength ? !strncmp($wrapper, $src, $wrapperLength) : !preg_match("%^[a-z0-9\.]+\:\/\/%", $src)) {
97 24
                array_unshift($parameters, substr($src, $wrapperLength));
98 24
                $reader = Kernel::create($readerClass, $parameters);
99 28
                break;
100
            }
101
        }
102
103 28
        return $reader;
104
    }
105
106
    /**
107
     * Find and instantiate a writer for a particular target
108
     *
109
     * @param string $target Target
110
     * @param array $parameters Parameters
111
     * @return null|WriterInterface  Writer instance
112
     */
113 15
    public static function writer(&$target, array $parameters = array())
114
    {
115 15
        $writer = null;
116
117
        // Run through all registered writer
118 15
        foreach (self::$writer as $wrapper => $writerClass) {
119 15
            $wrapperLength = strlen($wrapper);
120
121
            // If this wrapper is used: Instantiate the reader and resource
122 15
            if ($wrapperLength ? !strncmp($wrapper, $target, $wrapperLength) : !preg_match(
123 15
                "%^[a-z0-9\.]+\:\/\/%",
124
                $target
125
            )
126
            ) {
127 12
                array_unshift($parameters, substr($target, $wrapperLength));
128 12
                $writer = Kernel::create($writerClass, $parameters);
129 15
                break;
130
            }
131
        }
132
133 15
        return $writer;
134
    }
135
136
    /**
137
     * Copy a resource
138
     *
139
     * @param string $src Stream-wrapped source
140
     * @param array ...$parameters Reader parameters
141
     * @return Copy Copy handler
142
     * @api
143
     */
144 7
    public static function copy($src, ...$parameters)
145
    {
146 7
        $reader = self::reader($src, $parameters);
147 7
        if ($reader instanceof ReaderInterface) {
148 6
            return Kernel::create(Copy::class, [$reader]);
149
        }
150
151 1
        self::failInvalidReader();
152
        return null;
153
    }
154
155
    /**
156
     * Move / rename a resource
157
     *
158
     * @param string $src Stream-wrapped source
159
     * @param array ...$parameters Reader parameters
160
     * @return Move move handler
161
     * @api
162
     */
163 7
    public static function move($src, ...$parameters)
164
    {
165 7
        $reader = self::reader($src, $parameters);
166 7
        if ($reader instanceof ReaderInterface) {
167 6
            return Kernel::create(Move::class, [$reader]);
168
        }
169
170 1
        self::failInvalidReader();
171
        return null;
172
    }
173
174
    /**
175
     * Delete a resource
176
     *
177
     * @param string $src Stream-wrapped source
178
     * @param array ...$parameters Reader parameters
179
     * @return Move move handler
180
     * @api
181
     */
182 3
    public static function delete($src, ...$parameters)
183
    {
184 3
        $reader = self::reader($src, $parameters);
185 3
        if ($reader instanceof ReaderInterface) {
186
            /** @var Delete $deleter */
187 2
            $deleter = Kernel::create(Delete::class, [$reader]);
188 2
            return $deleter();
189
        }
190
191 1
        self::failInvalidReader();
192
        return null;
193
    }
194
195
    /**
196
     * Fail because of an invalid reader stream wrapper
197
     *
198
     * @return void
199
     * @throws \Apparat\Resource\Ports\InvalidArgumentException If the reader stream wrapper is invalid
200
     */
201 3
    protected static function failInvalidReader() {
202 3
        throw new InvalidArgumentException(
203 3
            'Invalid reader stream wrapper',
204 3
            InvalidArgumentException::INVALID_READER_STREAM_WRAPPER
205
        );
206
    }
207
}
208