1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\Workflow; |
4
|
|
|
|
5
|
|
|
use Cerbero\Workflow\Inflectors\InflectorInterface; |
6
|
|
|
use Cerbero\Workflow\Repositories\PipelineRepositoryInterface; |
7
|
|
|
use Cerbero\Workflow\Wrappers\DispatcherInterface; |
8
|
|
|
use Illuminate\Contracts\Container\Container; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Hub of the pipelines. |
12
|
|
|
* |
13
|
|
|
* @author Andrea Marco Sartori |
14
|
|
|
*/ |
15
|
|
|
class Workflow |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @author Andrea Marco Sartori |
19
|
|
|
* |
20
|
|
|
* @var Cerbero\Workflow\Repositories\PipelineRepositoryInterface $pipelines Pipelines repository. |
21
|
|
|
*/ |
22
|
|
|
protected $pipelines; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Andrea Marco Sartori |
26
|
|
|
* |
27
|
|
|
* @var Cerbero\Workflow\Inflectors\InflectorInterface $inflector Inflector. |
28
|
|
|
*/ |
29
|
|
|
protected $inflector; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @author Andrea Marco Sartori |
33
|
|
|
* |
34
|
|
|
* @var Illuminate\Contracts\Container\Container $container Service container. |
35
|
|
|
*/ |
36
|
|
|
protected $container; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @author Andrea Marco Sartori |
40
|
|
|
* |
41
|
|
|
* @var Cerbero\Workflow\Wrappers\DispatcherInterface $dispatcher Bus dispatcher. |
42
|
|
|
*/ |
43
|
|
|
protected $dispatcher; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set the dependencies. |
47
|
|
|
* |
48
|
|
|
* @author Andrea Marco Sartori |
49
|
|
|
* |
50
|
|
|
* @param Cerbero\Workflow\Repositories\PipelineRepositoryInterface $pipelines |
51
|
|
|
* @param Cerbero\Workflow\Wrappers\DispatcherInterface $dispatcher |
52
|
|
|
* @param Cerbero\Workflow\Inflectors\InflectorInterface $inflector |
53
|
|
|
* @param Illuminate\Contracts\Container\Container $container |
54
|
|
|
* |
55
|
|
|
* @return void |
|
|
|
|
56
|
|
|
*/ |
57
|
12 |
|
public function __construct( |
58
|
|
|
PipelineRepositoryInterface $pipelines, |
59
|
|
|
InflectorInterface $inflector, |
60
|
|
|
Container $container, |
61
|
|
|
DispatcherInterface $dispatcher |
62
|
|
|
) { |
63
|
12 |
|
$this->pipelines = $pipelines; |
|
|
|
|
64
|
12 |
|
$this->inflector = $inflector; |
|
|
|
|
65
|
12 |
|
$this->container = $container; |
|
|
|
|
66
|
12 |
|
$this->dispatcher = $dispatcher; |
|
|
|
|
67
|
12 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Dynamically call pipelines. |
71
|
|
|
* |
72
|
|
|
* @author Andrea Marco Sartori |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @param array $arguments |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
9 |
|
public function __call($name, $arguments) |
80
|
|
|
{ |
81
|
9 |
|
$this->failIfNotExists($name); |
82
|
|
|
|
83
|
6 |
|
$this->inflector->of($name); |
84
|
|
|
|
85
|
6 |
|
return $this->dispatchWorkflow($name); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Throw an exception if the given workflow does not exist. |
90
|
|
|
* |
91
|
|
|
* @author Andrea Marco Sartori |
92
|
|
|
* |
93
|
|
|
* @param string $workflow |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
9 |
|
protected function failIfNotExists($workflow) |
98
|
|
|
{ |
99
|
9 |
|
if (!$this->pipelines->exists($workflow)) { |
100
|
3 |
|
$error = "The workflow [$workflow] does not exist."; |
101
|
|
|
|
102
|
3 |
|
throw new \BadFunctionCallException($error); |
103
|
|
|
} |
104
|
6 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Dispatch the given workflow. |
108
|
|
|
* |
109
|
|
|
* @author Andrea Marco Sartori |
110
|
|
|
* |
111
|
|
|
* @param string $workflow |
112
|
|
|
* |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
6 |
|
protected function dispatchWorkflow($workflow) |
116
|
|
|
{ |
117
|
6 |
|
$job = $this->inflector->getJob(); |
118
|
|
|
|
119
|
6 |
|
$request = $this->resolveRequest(); |
120
|
|
|
|
121
|
6 |
|
$pipes = $this->pipelines->getPipesByPipeline($workflow); |
122
|
|
|
|
123
|
6 |
|
$parameters = $this->container->make('router')->current()->parameters(); |
124
|
|
|
|
125
|
6 |
|
return $this->dispatcher->pipeThrough($pipes)->dispatchFrom($job, $request, $parameters); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Resolve the apter request. |
130
|
|
|
* |
131
|
|
|
* @author Andrea Marco Sartori |
132
|
|
|
* |
133
|
|
|
* @return Illuminate\Http\Request |
134
|
|
|
*/ |
135
|
6 |
|
protected function resolveRequest() |
136
|
|
|
{ |
137
|
6 |
|
if (class_exists($request = $this->inflector->getRequest())) { |
138
|
3 |
|
return $this->container->make($request); |
139
|
|
|
} |
140
|
|
|
|
141
|
3 |
|
return $this->container->make('Illuminate\Http\Request'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.