Repository   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 169
rs 10
c 0
b 0
f 0
ccs 40
cts 40
cp 1
wmc 13
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
B generate() 0 19 6
A sanitizeConfigArray() 0 20 3
A sanitizeObjectConfig() 0 21 2
A generateValidatedParams() 0 20 2
1
<?php
2
3
/**
4
 * apparat-dev
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Dev
8
 * @subpackage  Apparat\Dev\Ports
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\Ports;
38
39
use Apparat\Dev\Application\Service\RepositoryGeneratorService;
40
use Apparat\Kernel\Ports\Kernel;
41
use Apparat\Object\Domain\Repository\RepositoryInterface;
42
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy;
43
use Apparat\Object\Ports\Types\Object;
44
45
/**
46
 * Repository tools
47
 *
48
 * @package Apparat\Dev
49
 * @subpackage Apparat\Dev\Ports
50
 */
51
class Repository
52
{
53
    /**
54
     * Object count
55
     *
56
     * @var string
57
     */
58
    const COUNT = 'count';
59
    /**
60
     * Object drafts
61
     *
62
     * @var string
63
     */
64
    const DRAFTS = 'drafts';
65
    /**
66
     * Hidden objects
67
     *
68
     * @var string
69
     */
70
    const HIDDEN = 'hidden';
71
    /**
72
     * Max. string
73
     *
74
     * @var int
75
     */
76
    const REVISIONS = 'revisions';
77
    /**
78
     * Object type
79
     *
80
     * @var int
81
     */
82
    const TYPE = 'type';
83
    /**
84
     * Create empty files only
85
     *
86
     * @var int
87
     */
88
    const FLAG_EMPTY_FILES = 1;
89
    /**
90
     * Create root directory if non-existent
91
     *
92
     * @var int
93
     */
94
    const FLAG_CREATE_ROOT_DIRECTORY = 2;
95
    /**
96
     * Default object configuration
97
     *
98
     * @var array
99
     */
100
    protected static $defaultObjectConfig = [
101
        self::COUNT => 0,
102
        self::DRAFTS => 0,
103
        self::HIDDEN => 0,
104
        self::REVISIONS => 0,
105
    ];
106
107
    /**
108
     * Setup a dummy repository
109
     *
110
     * @param string $root Root directory
111
     * @param array $config Object configuration
112
     * @param int $flags Build flags
113
     * @return RepositoryInterface Repository
114
     */
115 8
    public static function generate($root, array $config, $flags = 0)
116
    {
117
        // If no valid root directory is given
118 8
        if (!is_string($root)
119 7
            || !strlen($root)
120 8
            || (!is_dir($root) && (($flags & self::FLAG_CREATE_ROOT_DIRECTORY) ? !mkdir($root, 0777, true) : true))
121
        ) {
122 1
            throw new InvalidArgumentsException(
123 1
                sprintf('Invalid root directory "%s"', $root),
124 1
                InvalidArgumentsException::INVALID_ROOT_DIRECTORY
125
            );
126
        }
127
128
        // Sanitize the object configuration
129 7
        $config = self::sanitizeConfigArray($config);
130
131
        // Build the repository
132 5
        return self::generateValidatedParams($root, $config, $flags);
133
    }
134
135
    /**
136
     * Validate and sanitize the object configuration
137
     *
138
     * @param array $config Object configuration
139
     * @return array Object configuration
140
     */
141 7
    protected static function sanitizeConfigArray(array $config)
142
    {
143
        // Strip all invalid object types
144 7
        $config = array_intersect_key($config, Object::getSupportedTypes());
145
146
        // If the configuration is empty
147 7
        if (!count($config)) {
148 1
            throw new InvalidArgumentsException(
149 1
                'Empty object configuration',
150 1
                InvalidArgumentsException::EMPTY_OBJECT_CONFIG
151
            );
152
        }
153
154
        // Run through all object configurations
155 6
        foreach ($config as $objectType => $objectConfig) {
156 6
            $config[$objectType] = self::sanitizeObjectConfig($objectType, $objectConfig);
157
        }
158
159 5
        return $config;
160
    }
161
162
    /**
163
     * Validate and sanitize a single object configuration
164
     *
165
     * @param string $type Object type
166
     * @param array $config Single object configuration
167
     * @return array Single object configuration
168
     */
169 6
    protected static function sanitizeObjectConfig($type, array $config)
170
    {
171 6
        $config = array_merge(self::$defaultObjectConfig, $config);
172 6
        $config[self::COUNT] = intval($config[self::COUNT]);
173
174
        // If the number of objects is zero
175 6
        if ($config[self::COUNT] <= 0) {
176 1
            throw new InvalidArgumentsException(
177 1
                sprintf('Invalid object count "%s"', $config[self::COUNT]),
178 1
                InvalidArgumentsException::INVALID_OBJECT_COUNT
179
            );
180
        }
181
182
        // Sanitize values
183 5
        $config[self::DRAFTS] = max(0, min(1, floatval($config[self::DRAFTS])));
184 5
        $config[self::HIDDEN] = max(0, min(1, floatval($config[self::HIDDEN])));
185 5
        $config[self::REVISIONS] = intval($config[self::REVISIONS]);
186 5
        $config[self::TYPE] = $type;
187
188 5
        return $config;
189
    }
190
191
    /**
192
     * Setup a dummy repository with validated parameters
193
     *
194
     * @param string $root Root directory
195
     * @param array $config Object configuration
196
     * @param int $flags Build flags
197
     * @return RepositoryInterface Repository
198
     */
199 5
    protected static function generateValidatedParams($root, array $config, $flags)
200
    {
201
        // Prepare the repository
202 5
        $repository = \Apparat\Object\Infrastructure\Repository\Repository::create(
203 5
            getenv('REPOSITORY_URL'),
204
            [
205 5
                'type' => FileAdapterStrategy::TYPE,
206 5
                'root' => $root,
207
            ]
208
        );
209
210
        // Instantiate and run the repository generator service
211 5
        $repoBuilderType = ($flags & self::FLAG_EMPTY_FILES) ?
212 5
            '$ShallowRepositoryGeneratorService' : '$RepositoryGeneratorService';
213
        /** @var RepositoryGeneratorService $repoBuilder */
214 5
        $repoBuilder = Kernel::create($repoBuilderType, [$repository, $config]);
215 5
        $repoBuilder->generate($config);
216
217 3
        return $repository;
218
    }
219
}
220