Completed
Push — master ( 300f7c...ac8da7 )
by Joschi
06:40
created

ShallowObjectFactory   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 85
wmc 14
lcom 1
cbo 5
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C create() 0 55 13
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
    public function __construct(ContainerFactory $containerFactory)
68
    {
69
        $this->containerFactory = $containerFactory;
70
    }
71
72
    /**
73
     * Create a repository object
74
     *
75
     * @param \DateTime $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
    public function create(\DateTime $creationDate, $objectId, array $config)
82
    {
83
        /** @var FileAdapterStrategy $adapterStrategy */
84
        $adapterStrategy = $this->repository->getAdapterStrategy();
85
86
        // Build the container path
87
        $hidden = $config[Repository::HIDDEN] ? (rand(1, 5) == 5) : false;
88
        $containerPath = '/'.$this->containerFactory->create($creationDate, $hidden, $objectId,
89
                $config[Repository::TYPE]).'/';
90
91
        // Determine the amount of archive revisions to create
92
        $revisions = ($config[Repository::REVISIONS] < 0) ?
93
            rand(1, abs($config[Repository::REVISIONS]) + 1) : (1 + $config[Repository::REVISIONS]);
94
        $draft = intval($config[Repository::DRAFTS] ? (rand(1, 5) == 5) : 0);
95
        $revisionPaths = [];
96
        for ($revision = 1; $revision < $revisions - $draft; ++$revision) {
97
            $revisionPaths[] = $containerPath.$objectId.'-'.$revision.'.'.getenv('OBJECT_RESOURCE_EXTENSION');
98
        }
99
        if ($revision <= $revisions - $draft) {
100
            $revisionPaths[] = $containerPath.$objectId.'.'.getenv('OBJECT_RESOURCE_EXTENSION');
101
        }
102
        if ($draft) {
103
            $revisionPaths[] = $containerPath.($draft ? '.' : '').$objectId.'-'.$revisions.
104
                '.'.getenv('OBJECT_RESOURCE_EXTENSION');
105
        }
106
107
        // Run through all object paths and create (empty) files
108
        foreach ($revisionPaths as $revisionPathIndex => $revisionPath) {
109
            /** @var RepositoryPathInterface $revRepositoryPath */
110
            $revRepositoryPath = Kernel::create(RepositoryPath::class, [$this->repository, $revisionPath]);
111
            $absRevisionPath = $adapterStrategy->getAbsoluteResourcePath($revRepositoryPath);
0 ignored issues
show
Bug introduced by
The method getAbsoluteResourcePath() does not exist on Apparat\Object\Infrastru...ory\FileAdapterStrategy. Did you maybe mean absoluteResourcePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
112
113
            // For the first revision
114
            if (!$revisionPathIndex) {
115
                // Create the container directory
116
                $absContainerPath = dirname($absRevisionPath);
117
118
                // If the resource container cannot be created
119
                if (!is_dir($absContainerPath) && !mkdir($absContainerPath, 0777, true)) {
120
                    throw new RuntimeException(
121
                        sprintf('Cannot create container directory "%s"', $absContainerPath),
122
                        RuntimeException::CANNOT_CREATE_CONTAINER_DIRECTORY
123
                    );
124
                }
125
            }
126
127
            // If the shallow resource cannot be created
128
            if (!touch($absRevisionPath)) {
129
                throw new RuntimeException(
130
                    sprintf('Cannot create shallow resource "%s"', $absRevisionPath),
131
                    RuntimeException::CANNOT_CREATE_SHALLOW_RESOURCE
132
                );
133
            }
134
        }
135
    }
136
}