Completed
Push — master ( 51b910...e719dd )
by Joschi
03:23
created

FileAdapterStrategy::findObjectPaths()   F

Complexity

Conditions 13
Paths 832

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 13

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 57
ccs 44
cts 44
cp 1
rs 3.5087
cc 13
eloc 34
nc 832
nop 2
crap 13

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\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\Object\Infrastructure\Repository;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Application\Repository\AbstractAdapterStrategy;
41
use Apparat\Object\Domain\Model\Object\Id;
42
use Apparat\Object\Domain\Model\Object\ObjectInterface;
43
use Apparat\Object\Domain\Model\Object\ResourceInterface;
44
use Apparat\Object\Domain\Model\Object\Revision;
45
use Apparat\Object\Domain\Model\Path\PathInterface;
46
use Apparat\Object\Domain\Model\Path\RepositoryPath;
47
use Apparat\Object\Domain\Model\Path\RepositoryPathInterface;
48
use Apparat\Object\Domain\Repository\AdapterStrategyInterface;
49
use Apparat\Object\Domain\Repository\RepositoryInterface;
50
use Apparat\Object\Domain\Repository\RuntimeException;
51
use Apparat\Object\Domain\Repository\Selector;
52
use Apparat\Object\Domain\Repository\SelectorInterface;
53
use Apparat\Object\Infrastructure\Factory\ResourceFactory;
54
use Apparat\Resource\Infrastructure\Io\File\AbstractFileReaderWriter;
55
use Apparat\Resource\Infrastructure\Io\File\Writer;
56
57
/**
58
 * File adapter strategy
59
 *
60
 * @package Apparat\Object
61
 * @subpackage Apparat\Object\Infrastructure
62
 */
63
class FileAdapterStrategy extends AbstractAdapterStrategy
64
{
65
    /**
66
     * Adapter strategy type
67
     *
68
     * @var string
69
     */
70
    const TYPE = 'file';
71
    /**
72
     * Configuration
73
     *
74
     * @var array
75
     */
76
    protected $config = null;
77
    /**
78
     * Root directory (without trailing directory separator)
79
     *
80
     * @var string
81
     */
82
    protected $root = null;
83
    /**
84
     * Configuration directory (including trailing directory separator)
85
     *
86
     * @var string
87
     */
88
    protected $configDir = null;
89
90
    /**
91
     * Adapter strategy constructor
92
     *
93
     * @param array $config Adapter strategy configuration
94
     * @throws InvalidArgumentException If the root directory configuration is empty
95
     * @throws InvalidArgumentException If the root directory configuration is invalid
96
     */
97 17
    public function __construct(array $config)
98
    {
99 17
        parent::__construct($config, ['root']);
100
101
        // If the root directory configuration is empty
102 16
        if (empty($this->config['root'])) {
103 1
            throw new InvalidArgumentException(
104 1
                'Empty file adapter strategy root',
105
                InvalidArgumentException::EMTPY_FILE_STRATEGY_ROOT
106 1
            );
107
        }
108
109
        // Get the real path of the root directory
110 15
        $this->root = realpath($this->config['root']);
111
112
        // If the repository should be initialized
113 15
        if (!empty($this->config['init'])
114 15
            && (boolean)$this->config['init']
115 15
            && $this->initializeRepository()
116 13
        ) {
117 3
            $this->root = realpath($this->config['root']);
118 3
        }
119
120
        // If the root directory configuration is still invalid
121 13
        if (empty($this->root) || !@is_dir($this->root)) {
122 1
            throw new InvalidArgumentException(
123 1
                sprintf(
124 1
                    'Invalid file adapter strategy root "%s"',
125 1
                    $this->config['root']
126 1
                ),
127
                InvalidArgumentException::INVALID_FILE_STRATEGY_ROOT
128 1
            );
129
        }
130
131 12
        $this->configDir = $this->root.DIRECTORY_SEPARATOR.'.repo'.DIRECTORY_SEPARATOR;
132 12
    }
133
134
    /**
135
     * Initialize the repository
136
     *
137
     * @return boolean Success
138
     * @throws RuntimeException If the repository cannot be initialized
139
     * @throws RuntimeException If the repository size descriptor can not be created
140
     */
141 5
    public function initializeRepository()
142
    {
143
        // Successively create the repository directories
144 5
        $repoDirectories = [$this->config['root'], $this->config['root'].DIRECTORY_SEPARATOR.'.repo'];
145 5
        foreach ($repoDirectories as $repoDirectory) {
146
            // If the repository cannot be initialized
147 5
            if (file_exists($repoDirectory) ? !is_dir($repoDirectory) : !mkdir($repoDirectory, 0777, true)) {
148 1
                throw new RuntimeException('Could not initialize repository', RuntimeException::REPO_NOT_INITIALIZED);
149
            }
150 4
        }
151
152
        // If the repository size descriptor can not be created
153 4
        $configDir = $this->config['root'].DIRECTORY_SEPARATOR.'.repo'.DIRECTORY_SEPARATOR;
154 4
        if ((file_exists($configDir.'size.txt') && !is_file($configDir.'size.txt'))
155 3
            || !file_put_contents($configDir.'size.txt', '0')
156 4
        ) {
157 1
            throw new RuntimeException(
158 1
                'Could not create repository size descriptor',
159
                RuntimeException::REPO_SIZE_DESCRIPTOR_NOT_CREATED
160 1
            );
161
        }
162
163 3
        return true;
164
    }
165
166
    /**
167
     * Find objects by selector
168
     *
169
     * @param Selector|SelectorInterface $selector Object selector
170
     * @param RepositoryInterface $repository Object repository
171
     * @return PathInterface[] Object paths
172
     */
173 7
    public function findObjectPaths(SelectorInterface $selector, RepositoryInterface $repository)
174
    {
175 7
        chdir($this->root);
176
177
        // Build a glob string from the selector
178 7
        $glob = '';
179 7
        $globFlags = GLOB_ONLYDIR | GLOB_NOSORT;
180
181 7
        $year = $selector->getYear();
182 7
        if ($year !== null) {
183 7
            $glob .= '/'.$year;
184 7
        }
185
186 7
        $month = $selector->getMonth();
187 7
        if ($month !== null) {
188 7
            $glob .= '/'.$month;
189 7
        }
190
191 7
        $day = $selector->getDay();
192 7
        if ($day !== null) {
193 7
            $glob .= '/'.$day;
194 7
        }
195
196 7
        $hour = $selector->getHour();
197 7
        if ($hour !== null) {
198 2
            $glob .= '/'.$hour;
199 2
        }
200
201 7
        $minute = $selector->getMinute();
202 7
        if ($minute !== null) {
203 2
            $glob .= '/'.$minute;
204 2
        }
205
206 7
        $second = $selector->getSecond();
207 7
        if ($second !== null) {
208 2
            $glob .= '/'.$second;
209 2
        }
210
211 7
        $uid = $selector->getId();
212 7
        $type = $selector->getType();
213 7
        if (($uid !== null) || ($type !== null)) {
214 7
            $glob .= '/'.($uid ?: Selector::WILDCARD).'.'.($type ?: Selector::WILDCARD);
215
216 7
            $revision = $selector->getRevision();
217 7
            if ($revision !== null) {
218 1
                $glob .= '/'.($uid ?: Selector::WILDCARD).'-'.$revision;
219 1
                $globFlags &= ~GLOB_ONLYDIR;
220 1
            }
221 7
        }
222
223 7
        return array_map(
224 7
            function ($objectPath) use ($repository) {
225 7
                return Kernel::create(RepositoryPath::class, [$repository, '/'.$objectPath]);
226 7
            },
227 7
            glob(ltrim($glob, '/'), $globFlags)
228 7
        );
229
    }
230
231
    /**
232
     * Find and return an object resource
233
     *
234
     * @param string $resourcePath Repository relative resource path
235
     * @return ResourceInterface Object resource
236
     */
237 24
    public function getObjectResource($resourcePath)
238
    {
239 24
        return ResourceFactory::createFromSource(AbstractFileReaderWriter::WRAPPER.$this->root.$resourcePath);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Apparat\Object\I...>root . $resourcePath); (Apparat\Resource\Domain\...source\AbstractResource) is incompatible with the return type declared by the interface Apparat\Object\Applicati...face::getObjectResource of type Apparat\Object\Domain\Mo...bject\ResourceInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
240
    }
241
242
    /**
243
     * Allocate an object ID and create an object resource
244
     *
245
     * @param \Closure $creator Object creation closure
246
     * @return ObjectInterface Object
247
     * @throws RuntimeException If no object could be created
248
     * @throws \Exception If another error occurs
249
     */
250 2
    public function createObjectResource(\Closure $creator)
251
    {
252 2
        $sizeDescriptor = null;
253
254
        try {
255
            // Open the size descriptor
256 2
            $sizeDescriptor = fopen($this->configDir.'size.txt', 'r+');
257
258
            // If a lock of the size descriptor can be acquired
259 2
            if (flock($sizeDescriptor, LOCK_EX)) {
260
                // Determine the current repository size
261 1
                $repositorySize = '';
262 1
                while (!feof($sizeDescriptor)) {
263 1
                    $repositorySize .= fread($sizeDescriptor, 8);
264 1
                }
265 1
                $repositorySize = intval(trim($repositorySize));
266
267
                // Instantiate the next consecutive object ID
268 1
                $nextObjectId = Kernel::create(Id::class, [++$repositorySize]);
269
270
                // Create & persist the object (bypassing the repository)
271 1
                $object = $creator($nextObjectId);
272 1
                $this->persistObject($object);
273
274
                // Dump the new repository size, unlock the size descriptor
275 1
                ftruncate($sizeDescriptor, 0);
276 1
                fwrite($sizeDescriptor, $repositorySize);
277 1
                fflush($sizeDescriptor);
278 1
                flock($sizeDescriptor, LOCK_UN);
279
280
                // Return the newly created object
281 1
                return $object;
282
            }
283
284
            // If no object could be created
285 1
            throw new RuntimeException(
286 1
                'The repository size descriptor is unlockable',
287
                RuntimeException::REPO_SIZE_DESCRIPTOR_UNLOCKABLE
288 1
            );
289
290
            // If any exception is thrown
291 1
        } catch (\Exception $e) {
292
            // Release the size descriptor lock
293 1
            if (is_resource($sizeDescriptor)) {
294 1
                flock($sizeDescriptor, LOCK_UN);
295 1
            }
296
297
            // Forward the thrown exception
298 1
            throw $e;
299
        }
300
    }
301
302
    /**
303
     * Persist an object in the repository
304
     *
305
     * @param ObjectInterface $object Object
306
     * @return AdapterStrategyInterface Self reference
307
     */
308 1
    public function persistObject(ObjectInterface $object)
309
    {
310
        // If the object has just been deleted
311 1
        if ($object->hasBeenDeleted()) {
312
            return $this->deleteObject($object);
313
314
            // Elseif the object has just been undeleted
315 1
        } elseif ($object->hasBeenUndeleted()) {
316
            return $this->undeleteObject($object);
317
318
            // If the object has just been published
319 1
        } elseif ($object->hasBeenPublished()) {
320 1
            $this->publishObject($object);
321 1
        }
322
323
        // Persist the object resource
324 1
        return $this->persistObjectResource($object);
325
    }
326
327
    /**
328
     * Publish an object in the repository
329
     *
330
     * @param ObjectInterface $object
331
     */
332 1
    protected function publishObject(ObjectInterface $object)
333
    {
334 1
        $objectRepositoryPath = $object->getRepositoryPath();
335
336
        // If the object had been persisted as a draft: Remove the draft resource
337 1
        $objectDraftResPath = $this->absoluteResourcePath($objectRepositoryPath->setDraft(true));
338 1
        if (@file_exists($objectDraftResPath)) {
339 1
            unlink($objectDraftResPath);
340 1
        }
341
342
        // If it's not the first object revision: Rotate the previous revision resource
343 1
        $objectRevisionNumber = $object->getRevision()->getRevision();
344 1
        if ($objectRevisionNumber > 1) {
345
            // Build the "current" object repository path
346 1
            $currentRevision = Revision::current();
347
            $curObjectResPath =
348 1
                $this->absoluteResourcePath($objectRepositoryPath->setRevision($currentRevision));
349
350
            // Build the previous object repository path
351
            /** @var Revision $previousRevision */
352 1
            $previousRevision = Kernel::create(Revision::class, [$objectRevisionNumber - 1]);
353
            $prevObjectResPath
354 1
                = $this->absoluteResourcePath($objectRepositoryPath->setRevision($previousRevision));
355
356
            // Rotate the previous revision's resource path
357 1
            if (file_exists($curObjectResPath)) {
358 1
                rename($curObjectResPath, $prevObjectResPath);
359 1
            }
360 1
        }
361 1
    }
362
363
    /**
364
     * Build an absolute repository resource path
365
     *
366
     * @param RepositoryPathInterface $repositoryPath Repository path
367
     * @return string Absolute repository resource path
368
     */
369 1
    protected function absoluteResourcePath(RepositoryPathInterface $repositoryPath)
370
    {
371 1
        return $this->root.str_replace(
372 1
            '/',
373 1
            DIRECTORY_SEPARATOR,
374 1
            $repositoryPath->withExtension(getenv('OBJECT_RESOURCE_EXTENSION'))
375 1
        );
376
    }
377
378
    /**
379
     * Persist an object resource in the repository
380
     *
381
     * @param ObjectInterface $object Object
382
     * @return AdapterStrategyInterface Self reference
383
     */
384 1
    protected function persistObjectResource(ObjectInterface $object)
385
    {
386
        /** @var \Apparat\Object\Infrastructure\Model\Object\Resource $objectResource */
387 1
        $objectResource = ResourceFactory::createFromObject($object);
388
389
        // Create the absolute object resource path
390 1
        $objectResourcePath = $this->absoluteResourcePath($object->getRepositoryPath());
391
392
        /** @var Writer $fileWriter */
393 1
        $fileWriter = Kernel::create(
394 1
            Writer::class,
395 1
            [$objectResourcePath, Writer::FILE_CREATE | Writer::FILE_CREATE_DIRS | Writer::FILE_OVERWRITE]
396 1
        );
397 1
        $objectResource->dump($fileWriter);
398
399 1
        return $this;
400
    }
401
402
    /**
403
     * Return the repository size (number of objects in the repository)
404
     *
405
     * @return int Repository size
406
     */
407 2
    public function getRepositorySize()
408
    {
409 2
        $sizeDescriptorFile = $this->configDir.'size.txt';
410 2
        $repositorySize = 0;
411 2
        if (is_file($sizeDescriptorFile) && is_readable($sizeDescriptorFile)) {
412 2
            $repositorySize = intval(file_get_contents($this->configDir.'size.txt'));
413 2
        }
414 2
        return $repositorySize;
415
    }
416
417
    /**
418
     * Delete all revisions of an object
419
     *
420
     * @param ObjectInterface $object Object
421
     * @return ObjectInterface Object
422
     */
423
    protected function deleteObject(ObjectInterface $object)
424
    {
425
        // TODO Implement object deletion
426
427
        // Persist the object resource
428
        return $this->persistObjectResource($object);
429
    }
430
431
    /**
432
     * Undelete all revisions of an object
433
     *
434
     * @param ObjectInterface $object Object
435
     * @return ObjectInterface Object
436
     */
437
    protected function undeleteObject(ObjectInterface $object)
438
    {
439
        // TODO Implement object undeletion
440
441
        // Persist the object resource
442
        return $this->persistObjectResource($object);
443
    }
444
}
445