Completed
Push — master ( 2ed5a0...f30234 )
by Joschi
02:36
created

ShallowObjectFactory::createObjectResource()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 19
rs 9.4285
ccs 10
cts 10
cp 1
crap 3
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] ? (rand(1, 5) == 5) : false;
85 3
        $containerPath = '/'.$this->containerFactory->create($creationDate, $hidden, $objectId,
86 3
                $config[Repository::TYPE]).'/';
87
88
        // Run through all object paths and create (empty) files
89
        /** @var FileAdapterStrategy $adapterStrategy */
90 3
        $adapterStrategy = $this->repository->getAdapterStrategy();
91 3
        foreach ($this->revisionPaths($objectId, $config, $containerPath) as $revisionPathIndex => $revisionPath) {
92 3
            $this->createObjectResource($revisionPathIndex, $revisionPath, $adapterStrategy);
93
        }
94 1
    }
95
96
    /**
97
     * Create a single object resource
98
     *
99
     * @param int $revisionPathIndex Revision path index
100
     * @param string $revisionPath Revision repository path
101
     * @param FileAdapterStrategy $adapterStrategy
102
     * @throws RuntimeException If the shallow resource cannot be created
103
     */
104 3
    protected function createObjectResource($revisionPathIndex, $revisionPath, FileAdapterStrategy $adapterStrategy)
105
    {
106
        /** @var RepositoryPathInterface $revRepositoryPath */
107 3
        $revRepositoryPath = Kernel::create(RepositoryPath::class, [$this->repository, $revisionPath]);
108 3
        $absRevisionPath = $adapterStrategy->getAbsoluteResourcePath($revRepositoryPath);
109
110
        // For the first revision: Create the container directory
111 3
        if (!$revisionPathIndex) {
112 3
            $this->createObjectResourceContainer(dirname($absRevisionPath));
113
        }
114
115
        // If the shallow resource cannot be created
116 2
        if (!touch($absRevisionPath)) {
117 1
            throw new RuntimeException(
118 1
                sprintf('Cannot create shallow resource "%s"', $absRevisionPath),
119 1
                RuntimeException::CANNOT_CREATE_SHALLOW_RESOURCE
120
            );
121
        }
122 1
    }
123
124
    /**
125
     * Create the object resource container
126
     *
127
     * @param string $absContainerPath Absolute object resource container path
128
     * @throws RuntimeException If the resource container cannot be created
129
     */
130 3
    protected function createObjectResourceContainer($absContainerPath)
131
    {
132
        // If the resource container cannot be created
133 3
        if (!is_dir($absContainerPath) && !mkdir($absContainerPath, 0777, true)) {
134 1
            throw new RuntimeException(
135 1
                sprintf('Cannot create container directory "%s"', $absContainerPath),
136 1
                RuntimeException::CANNOT_CREATE_CONTAINER_DIRECTORY
137
            );
138
        }
139 2
    }
140
141
    /**
142
     * Generate the list of repository relative revision paths
143
     *
144
     * @param int $objectId Object ID
145
     * @param array $config Object configuration
146
     * @param string $containerPath Repository relative container path
147
     * @return array Repository relative revision paths
148
     */
149 3
    protected function revisionPaths($objectId, array $config, $containerPath)
150
    {
151
        // Determine the amount of archive revisions to create
152 3
        $revisions = ($config[Repository::REVISIONS] < 0) ?
153 3
            rand(1, abs($config[Repository::REVISIONS]) + 1) : (1 + $config[Repository::REVISIONS]);
154
155
        // Determine the draft status
156 3
        $draft = intval($config[Repository::DRAFTS] ? (rand(1, 5) == 5) : 0);
157
158
        // Build the revision path list
159 3
        $revisionPaths = [];
160 3
        for ($revision = 1; $revision < $revisions - $draft; ++$revision) {
161 2
            $revisionPaths[] = $containerPath.$objectId.'-'.$revision.'.'.getenv('OBJECT_RESOURCE_EXTENSION');
162
        }
163 3
        if ($revision <= $revisions - $draft) {
164 3
            $revisionPaths[] = $containerPath.$objectId.'.'.getenv('OBJECT_RESOURCE_EXTENSION');
165
        }
166 3
        if ($draft) {
167 1
            $revisionPaths[] = $containerPath.($draft ? '.' : '').$objectId.'-'.$revisions.
168 1
                '.'.getenv('OBJECT_RESOURCE_EXTENSION');
169
        }
170
171 3
        return $revisionPaths;
172
    }
173
}