Completed
Push — master ( 2d5356...e3df84 )
by Joschi
03:05
created

Manager::objectResourceExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Application
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\Application\Model\Object;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Application\Factory\ObjectFactory;
41
use Apparat\Object\Domain\Model\Object\Id;
42
use Apparat\Object\Domain\Model\Object\ManagerInterface;
43
use Apparat\Object\Domain\Model\Object\ObjectInterface;
44
use Apparat\Object\Domain\Model\Object\ResourceInterface;
45
use Apparat\Object\Domain\Model\Object\Revision;
46
use Apparat\Object\Domain\Model\Object\Type;
47
use Apparat\Object\Domain\Model\Path\RepositoryPath;
48
use Apparat\Object\Domain\Model\Path\RepositoryPathInterface;
49
use Apparat\Object\Domain\Repository\RepositoryInterface;
50
51
/**
52
 * Object manager
53
 *
54
 * @package Apparat\Object
55
 * @subpackage Apparat\Object\Infrastructure
56
 */
57
class Manager implements ManagerInterface
58
{
59
    /**
60
     * Create and return a new object
61
     *
62
     * @param RepositoryInterface $repository Repository to create the object in
63
     * @param Type $type Object type
64
     * @param string $payload Object payload
65
     * @param array $propertyData Object property data
66
     * @return ObjectInterface Object
67
     */
68
    public function createObject(RepositoryInterface $repository, Type $type, $payload = '', array $propertyData = [])
69
    {
70
        // Construct a creation closure
71 2
        $creationClosure = function (Id $uid) use ($repository, $type, $payload, $propertyData) {
72
            /** @var Revision $revision */
73 1
            $revision = Kernel::create(Revision::class, [1, true]);
74
75
            /** @var RepositoryPath $repositoryPath */
76 1
            $repositoryPath = Kernel::create(RepositoryPath::class, [$repository]);
77 1
            $repositoryPath = $repositoryPath->setId($uid);
78 1
            $repositoryPath = $repositoryPath->setRevision($revision);
79 1
            $repositoryPath = $repositoryPath->setType($type);
80 1
            $repositoryPath = $repositoryPath->setCreationDate(new \DateTimeImmutable());
81
82 1
            return ObjectFactory::createFromParams($repositoryPath, $payload, $propertyData);
83 2
        };
84
85
        // Wrap the object creation in an ID allocation transaction
86 2
        return $repository->getAdapterStrategy()->createObjectResource($creationClosure);
87
    }
88
89
    /**
90
     * Load an object from a repository
91
     *
92
     * @param RepositoryPathInterface $path Repository object path
93
     * @return ObjectInterface Object
94
     */
95 24
    public function loadObject(RepositoryPathInterface $path)
96
    {
97
        // Create the current revision path
98
        /** @var RepositoryPathInterface $currentPath */
99 24
        $currentPath = $path->setRevision(Revision::current());
100
101
        // Load the current object resource
102 24
        $objectResource = $this->loadObjectResource($currentPath);
103
104
        // Instantiate the object
105 23
        $object = ObjectFactory::createFromResource($path, $objectResource);
106
107
        // Use and return the requested object revision
108 23
        return $object->useRevision($path->getRevision());
109
    }
110
111
    /**
112
     * Load and return an object resource
113
     *
114
     * @param RepositoryPathInterface $path
115
     * @return ResourceInterface Object resource
116
     */
117 24
    public function loadObjectResource(RepositoryPathInterface $path)
118
    {
119 24
        return $path->getRepository()->getAdapterStrategy()->getObjectResource(
120 24
            $path->withExtension(getenv('OBJECT_RESOURCE_EXTENSION'))
121 24
        );
122
    }
123
124
    /**
125
     * Test whether an object resource exists
126
     *
127
     * @param RepositoryPathInterface $path
128
     * @return boolean Object resource exists
129
     */
130 24
    public function objectResourceExists(RepositoryPathInterface $path)
131
    {
132
        // TODO: Implement objectResourceExists() method.
133 24
    }
134
}
135