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

Repository::generateValidatedParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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