|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* apparat-dev |
|
5
|
|
|
* |
|
6
|
|
|
* @category Apparat |
|
7
|
|
|
* @package Apparat\Dev |
|
8
|
|
|
* @subpackage Apparat\Dev\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\Dev\Infrastructure\Factory; |
|
38
|
|
|
|
|
39
|
|
|
use Apparat\Dev\Ports\Repository; |
|
40
|
|
|
use Apparat\Dev\Ports\RuntimeException; |
|
41
|
|
|
use Apparat\Kernel\Ports\Kernel; |
|
42
|
|
|
use Apparat\Object\Domain\Model\Path\RepositoryPath; |
|
43
|
|
|
use Apparat\Object\Domain\Model\Path\RepositoryPathInterface; |
|
44
|
|
|
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Shallow object factory |
|
48
|
|
|
* |
|
49
|
|
|
* @package Apparat\Dev |
|
50
|
|
|
* @subpackage Apparat\Dev\Infrastructure |
|
51
|
|
|
*/ |
|
52
|
|
|
class ShallowObjectFactory extends AbstractObjectFactory |
|
53
|
|
|
{ |
|
54
|
|
|
/** |
|
55
|
|
|
* Container factory |
|
56
|
|
|
* |
|
57
|
|
|
* @var ContainerFactory |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $containerFactory; |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Constructor |
|
64
|
|
|
* |
|
65
|
|
|
* @param ContainerFactory $containerFactory Container factory |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function __construct(ContainerFactory $containerFactory) |
|
68
|
|
|
{ |
|
69
|
3 |
|
$this->containerFactory = $containerFactory; |
|
70
|
3 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Create a repository object |
|
74
|
|
|
* |
|
75
|
|
|
* @param \DateTimeInterface $creationDate Creation date |
|
76
|
|
|
* @param int $objectId Object ID |
|
77
|
|
|
* @param array $config Object configuration |
|
78
|
|
|
* @throws RuntimeException If the resource container cannot be created |
|
79
|
|
|
* @throws RuntimeException If the shallow resource cannot be created |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function create(\DateTimeInterface $creationDate, $objectId, array $config) |
|
82
|
|
|
{ |
|
83
|
|
|
// Build the container path |
|
84
|
3 |
|
$hidden = ($config[Repository::HIDDEN] > 0) ? (rand(0, 100) < ($config[Repository::HIDDEN] * 100)) : false; |
|
85
|
|
|
$containerPath = '/'. |
|
86
|
3 |
|
$this->containerFactory->create( |
|
87
|
3 |
|
$creationDate, |
|
88
|
3 |
|
$hidden, |
|
89
|
3 |
|
$objectId, |
|
90
|
3 |
|
$config[Repository::TYPE] |
|
91
|
3 |
|
).'/'; |
|
92
|
|
|
|
|
93
|
3 |
|
$this->createObjectRevisions($objectId, $config, $containerPath); |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Create the object revisions |
|
98
|
|
|
* |
|
99
|
|
|
* @param int $objectId Object ID |
|
100
|
|
|
* @param array $config Object configuration |
|
101
|
|
|
* @param string $containerPath Repository relative container path |
|
102
|
|
|
*/ |
|
103
|
3 |
|
protected function createObjectRevisions($objectId, array $config, $containerPath) { |
|
104
|
|
|
// Run through all object paths and create (empty) files |
|
105
|
|
|
/** @var FileAdapterStrategy $adapterStrategy */ |
|
106
|
3 |
|
$adapterStrategy = $this->repository->getAdapterStrategy(); |
|
107
|
3 |
|
foreach ($this->revisionPaths($objectId, $config, $containerPath) as $revisionPathIndex => $revisionPath) { |
|
108
|
3 |
|
$this->createObjectResource($revisionPathIndex, $revisionPath, $adapterStrategy); |
|
109
|
1 |
|
} |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Create a single object resource |
|
114
|
|
|
* |
|
115
|
|
|
* @param int $revisionPathIndex Revision path index |
|
116
|
|
|
* @param string $revisionPath Revision repository path |
|
117
|
|
|
* @param FileAdapterStrategy $adapterStrategy |
|
118
|
|
|
* @throws RuntimeException If the shallow resource cannot be created |
|
119
|
|
|
*/ |
|
120
|
3 |
|
protected function createObjectResource($revisionPathIndex, $revisionPath, FileAdapterStrategy $adapterStrategy) |
|
121
|
|
|
{ |
|
122
|
|
|
/** @var RepositoryPathInterface $revRepositoryPath */ |
|
123
|
3 |
|
$revRepositoryPath = Kernel::create(RepositoryPath::class, [$this->repository, $revisionPath]); |
|
124
|
3 |
|
$absRevisionPath = $adapterStrategy->getAbsoluteResourcePath($revRepositoryPath); |
|
125
|
|
|
|
|
126
|
|
|
// For the first revision: Create the container directory |
|
127
|
3 |
|
if (!$revisionPathIndex) { |
|
128
|
3 |
|
$this->createObjectResourceContainer(dirname($absRevisionPath)); |
|
129
|
2 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
// If the shallow resource cannot be created |
|
132
|
2 |
|
if (!touch($absRevisionPath)) { |
|
133
|
1 |
|
throw new RuntimeException( |
|
134
|
1 |
|
sprintf('Cannot create shallow resource "%s"', $absRevisionPath), |
|
135
|
|
|
RuntimeException::CANNOT_CREATE_SHALLOW_RESOURCE |
|
136
|
1 |
|
); |
|
137
|
|
|
} |
|
138
|
1 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Create the object resource container |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $absContainerPath Absolute object resource container path |
|
144
|
|
|
* @throws RuntimeException If the resource container cannot be created |
|
145
|
|
|
*/ |
|
146
|
3 |
|
protected function createObjectResourceContainer($absContainerPath) |
|
147
|
|
|
{ |
|
148
|
|
|
// If the resource container cannot be created |
|
149
|
3 |
|
if (!is_dir($absContainerPath) && !mkdir($absContainerPath, 0777, true)) { |
|
150
|
1 |
|
throw new RuntimeException( |
|
151
|
1 |
|
sprintf('Cannot create container directory "%s"', $absContainerPath), |
|
152
|
|
|
RuntimeException::CANNOT_CREATE_CONTAINER_DIRECTORY |
|
153
|
1 |
|
); |
|
154
|
|
|
} |
|
155
|
2 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Generate the list of repository relative revision paths |
|
159
|
|
|
* |
|
160
|
|
|
* @param int $objectId Object ID |
|
161
|
|
|
* @param array $config Object configuration |
|
162
|
|
|
* @param string $containerPath Repository relative container path |
|
163
|
|
|
* @return array Repository relative revision paths |
|
164
|
|
|
*/ |
|
165
|
3 |
|
protected function revisionPaths($objectId, array $config, $containerPath) |
|
166
|
|
|
{ |
|
167
|
|
|
// Determine the amount of archive revisions to create |
|
168
|
3 |
|
$revisions = ($config[Repository::REVISIONS] < 0) ? |
|
169
|
3 |
|
rand(1, abs($config[Repository::REVISIONS]) + 1) : (1 + $config[Repository::REVISIONS]); |
|
170
|
|
|
|
|
171
|
|
|
// Determine the draft status |
|
172
|
3 |
|
$draft = (($config[Repository::DRAFTS] > 0) ? (rand(0, 100) < $config[Repository::DRAFTS] * 100) : 0); |
|
173
|
|
|
|
|
174
|
|
|
// Build the revision path list |
|
175
|
3 |
|
$revisionPaths = []; |
|
176
|
3 |
|
for ($revision = 1; $revision < $revisions - $draft; ++$revision) { |
|
177
|
1 |
|
$revisionPaths[] = $containerPath.$objectId.'-'.$revision.'.'.getenv('OBJECT_RESOURCE_EXTENSION'); |
|
178
|
1 |
|
} |
|
179
|
3 |
|
if ($revision <= $revisions - $draft) { |
|
180
|
2 |
|
$revisionPaths[] = $containerPath.$objectId.'.'.getenv('OBJECT_RESOURCE_EXTENSION'); |
|
181
|
2 |
|
} |
|
182
|
3 |
|
if ($draft) { |
|
183
|
2 |
|
$revisionPaths[] = $containerPath.($draft ? '.' : '').$objectId.'-'.$revisions. |
|
184
|
2 |
|
'.'.getenv('OBJECT_RESOURCE_EXTENSION'); |
|
185
|
2 |
|
} |
|
186
|
|
|
|
|
187
|
3 |
|
return $revisionPaths; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|