This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
/
AbstractActionManager.php
170 lines (145 loc) · 4.93 KB
/
AbstractActionManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Spy\Timeline\Driver;
use Spy\Timeline\Model\ActionInterface;
use Spy\Timeline\Model\ComponentInterface;
use Spy\Timeline\ResolveComponent\ComponentDataResolverInterface;
use Spy\Timeline\ResolveComponent\ValueObject\ResolveComponentModelIdentifier;
use Spy\Timeline\ResolveComponent\ValueObject\ResolvedComponentData;
use Spy\Timeline\Spread\DeployerInterface;
abstract class AbstractActionManager implements ActionManagerInterface
{
/**
* @var DeployerInterface
*/
protected $deployer;
/**
* @var string FQCN af the action class
*/
protected $actionClass;
/**
* @var string FQCN of the action component class
*/
protected $componentClass;
/**
* @var string FQCN of the component class
*/
protected $actionComponentClass;
/**
* @var ComponentDataResolverInterface
*/
private $componentDataResolver;
/**
* @param string $actionClass FQCN of the action class
* @param string $componentClass FQCN of the component class
* @param string $actionComponentClass FQCN of the action component class
*/
public function __construct($actionClass, $componentClass, $actionComponentClass)
{
$this->actionClass = $actionClass;
$this->componentClass = $componentClass;
$this->actionComponentClass = $actionComponentClass;
}
/**
* {@inheritdoc}
*/
public function create($subject, $verb, array $components = array())
{
/** @var $action ActionInterface */
$action = new $this->actionClass();
$action->setVerb($verb);
if (!$subject instanceof ComponentInterface and !is_object($subject)) {
throw new \Exception('Subject must be a ComponentInterface or an object');
}
$components['subject'] = $subject;
foreach ($components as $type => $component) {
$this->addComponent($action, $type, $component);
}
return $action;
}
/**
* @param ActionInterface $action action
* @param string $type type
* @param mixed $component component
* @throws \Exception
*/
public function addComponent($action, $type, $component)
{
if (!$component instanceof ComponentInterface && !is_scalar($component)) {
$component = $this->findOrCreateComponent($component);
if (null === $component) {
throw new \Exception(sprintf('Impossible to create component from %s.', $type));
}
}
$action->addComponent($type, $component, $this->actionComponentClass);
}
/**
* @param ActionInterface $action action
*
* @return void
*/
protected function deployActionDependOnDelivery(ActionInterface $action)
{
if ($this->deployer && $this->deployer->isDeliveryImmediate()) {
$this->deployer->deploy($action, $this);
}
}
/**
* @param DeployerInterface $deployer deployer
*/
public function setDeployer(DeployerInterface $deployer)
{
$this->deployer = $deployer;
}
/**
* Sets the component data resolver.
*
* @param ComponentDataResolverInterface $componentDataResolver
*/
public function setComponentDataResolver(ComponentDataResolverInterface $componentDataResolver)
{
$this->componentDataResolver = $componentDataResolver;
}
/**
* Gets the component data resolver.
*
* @return ComponentDataResolverInterface
*
* @throws \Exception When no component data resolver has been set
*/
public function getComponentDataResolver()
{
if (empty($this->componentDataResolver) || !$this->componentDataResolver instanceof ComponentDataResolverInterface ) {
throw new \Exception('Component data resolver not set');
}
return $this->componentDataResolver;
}
/**
* Resolves the model and identifier.
*
* @param string|object $model
* @param null|string|array $identifier
*
* @return ResolvedComponentData
*/
protected function resolveModelAndIdentifier($model, $identifier)
{
$resolve = new ResolveComponentModelIdentifier($model, $identifier);
return $this->getComponentDataResolver()->resolveComponentData($resolve);
}
/**
* Creates a new component object from the resolved data.
*
* @param ResolvedComponentData $resolved The resolved component data
*
* @return ComponentInterface The newly created and populated component
*/
protected function getComponentFromResolvedComponentData(ResolvedComponentData $resolved)
{
/** @var $component ComponentInterface */
$component = new $this->componentClass();
$component->setModel($resolved->getModel());
$component->setData($resolved->getData());
$component->setIdentifier($resolved->getIdentifier());
return $component;
}
}