|
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\Infrastructure\Service; |
|
38
|
|
|
|
|
39
|
|
|
use Apparat\Resource\Domain\Contract\WriterInterface; |
|
40
|
|
|
use Apparat\Resource\Infrastructure\Io\File\Reader as FileReader; |
|
41
|
|
|
use Apparat\Resource\Infrastructure\Io\File\Writer as FileWriter; |
|
42
|
|
|
use Apparat\Resource\Infrastructure\Io\InMemory\Writer as InMemoryWriter; |
|
43
|
|
|
use Apparat\Resource\Ports\InvalidArgumentException; |
|
44
|
|
|
use Apparat\Resource\Ports\InvalidWriterArgumentException; |
|
45
|
|
|
use Apparat\Resource\Ports\Tools; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Resource move operation |
|
49
|
|
|
* |
|
50
|
|
|
* @package Apparat\Resource |
|
51
|
|
|
* @subpackage Apparat\Resource\Infrastructure |
|
52
|
|
|
*/ |
|
53
|
|
|
class Move extends AbstractService |
|
54
|
|
|
{ |
|
55
|
|
|
/******************************************************************************* |
|
56
|
|
|
* PUBLIC METHODS |
|
57
|
|
|
*******************************************************************************/ |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Move / rename the source resource to a target resource |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $target Stream wrapped target |
|
63
|
|
|
* @param array $parameters Writer parameters |
|
64
|
|
|
* @return WriterInterface Writer instance |
|
65
|
|
|
* @throws InvalidWriterArgumentException If the writer stream wrapper is invalid |
|
66
|
|
|
*/ |
|
67
|
6 |
|
public function toTarget($target, ...$parameters) |
|
68
|
|
|
{ |
|
69
|
6 |
|
$writer = Tools::writer($target, $parameters); |
|
70
|
|
|
|
|
71
|
|
|
// If it's a file writer |
|
72
|
6 |
|
if ($writer instanceof FileWriter) { |
|
73
|
3 |
|
return $this->moveToFile($writer); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// If it's an in-memory writer |
|
77
|
3 |
|
if ($writer instanceof InMemoryWriter) { |
|
78
|
2 |
|
return $this->moveToInMemory($writer); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
throw new InvalidArgumentException( |
|
82
|
1 |
|
'Invalid writer stream wrapper', |
|
83
|
1 |
|
InvalidWriterArgumentException::INVALID_WRITER_STREAM_WRAPPER |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/******************************************************************************* |
|
88
|
|
|
* PRIVATE METHODS |
|
89
|
|
|
*******************************************************************************/ |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Move / rename the resource to a file |
|
93
|
|
|
* |
|
94
|
|
|
* @param FileWriter $writer Target file writer |
|
95
|
|
|
* @return FileWriter File writer instance |
|
96
|
|
|
* @throws RuntimeException If the resource cannot be moved / renamed |
|
97
|
|
|
*/ |
|
98
|
3 |
|
protected function moveToFile(FileWriter $writer) |
|
99
|
|
|
{ |
|
100
|
|
|
// If a file resource is read |
|
101
|
3 |
|
if ($this->reader instanceof FileReader) { |
|
102
|
|
|
// If a copy error occurs |
|
103
|
2 |
|
if (!@rename($this->reader->getFile(), $writer->getFile())) { |
|
104
|
1 |
|
throw new RuntimeException( |
|
105
|
|
|
sprintf( |
|
106
|
1 |
|
'Could not move / rename "%s" to "%s"', |
|
107
|
1 |
|
$this->reader->getFile(), |
|
108
|
1 |
|
$writer->getFile() |
|
109
|
|
|
), |
|
110
|
1 |
|
RuntimeException::COULD_NOT_MOVE_FILE_TO_FILE |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
return $writer; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// In-memory resource |
|
118
|
1 |
|
$writer->write($this->reader->read()); |
|
119
|
1 |
|
return $writer; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Move / rename the resource to a in-memory buffer |
|
124
|
|
|
* |
|
125
|
|
|
* @param InMemoryWriter $writer Target in-memory writer |
|
126
|
|
|
* @return InMemoryWriter In-memory writer instance |
|
127
|
|
|
*/ |
|
128
|
2 |
|
protected function moveToInMemory(InMemoryWriter $writer) |
|
129
|
|
|
{ |
|
130
|
2 |
|
$writer->write($this->reader->read()); |
|
131
|
2 |
|
return $writer; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|