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
|
1 |
|
$creationClosure = function (Id $uid) use ($repository, $type, $payload, $propertyData) { |
72
|
|
|
/** @var Revision $revision */ |
73
|
1 |
|
$revision = Kernel::create(Revision::class, [Revision::DRAFT]); |
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
|
1 |
|
}; |
84
|
|
|
|
85
|
|
|
// Wrap the object creation in an ID allocation transaction |
86
|
1 |
|
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
|
16 |
|
public function loadObject(RepositoryPathInterface $path) |
96
|
|
|
{ |
97
|
|
|
// Load the current object resource |
98
|
16 |
|
$objectResource = $this->loadObjectResource( |
99
|
16 |
|
$path->setRevision(Kernel::create(Revision::class, [Revision::CURRENT])) |
|
|
|
|
100
|
16 |
|
); |
101
|
|
|
|
102
|
|
|
// Instantiate the object |
103
|
15 |
|
$object = ObjectFactory::createFromResource($path, $objectResource); |
104
|
|
|
|
105
|
|
|
// Use and return the requested object revision |
106
|
14 |
|
return $object; |
107
|
|
|
|
108
|
|
|
// TODO: Implement revision loading |
109
|
|
|
// return $object->useRevision($path->getRevision()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Load and return an object resource |
114
|
|
|
* |
115
|
|
|
* @param RepositoryPathInterface $path |
116
|
|
|
* @return ResourceInterface Object resource |
117
|
|
|
*/ |
118
|
16 |
|
public function loadObjectResource(RepositoryPathInterface $path) |
119
|
|
|
{ |
120
|
16 |
|
return $path->getRepository()->getAdapterStrategy()->getObjectResource( |
121
|
16 |
|
$path->withExtension(getenv('OBJECT_RESOURCE_EXTENSION')) |
122
|
16 |
|
); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.