1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zurbaev\PipelineTasks; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
6
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
7
|
|
|
use Illuminate\Pipeline\Pipeline; |
8
|
|
|
use Zurbaev\PipelineTasks\Events\PipelineTaskCompleted; |
9
|
|
|
use Zurbaev\PipelineTasks\Events\PipelineTaskFailed; |
10
|
|
|
use Zurbaev\PipelineTasks\Exceptions\PipelineTaskFailedException; |
11
|
|
|
|
12
|
|
|
class TaskManager |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Container |
16
|
|
|
*/ |
17
|
|
|
protected $container; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* TaskManager constructor. |
21
|
|
|
* |
22
|
|
|
* @param Container $container |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Container $container) |
25
|
|
|
{ |
26
|
|
|
$this->container = $container; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Start given task. |
31
|
|
|
* |
32
|
|
|
* @param Task $task |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function start(Task $task) |
37
|
|
|
{ |
38
|
|
|
try { |
39
|
|
|
return $this->pipeline() |
40
|
|
|
->send([$this->getContainer(), $task]) |
41
|
|
|
->through($task->pipes()) |
42
|
|
|
->via($task->pipeMethod()) |
43
|
|
|
->then(function (array $payload) { |
44
|
|
|
list(, $task) = $payload; |
45
|
|
|
|
46
|
|
|
return $this->taskCompleted($task); |
47
|
|
|
}); |
48
|
|
|
} catch (PipelineTaskFailedException $e) { |
49
|
|
|
return $this->taskFailed($task, $e); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Marks task as completed. |
55
|
|
|
* |
56
|
|
|
* @param Task $task |
57
|
|
|
* |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
protected function taskCompleted(Task $task) |
61
|
|
|
{ |
62
|
|
|
$this->fire(new PipelineTaskCompleted($task)); |
63
|
|
|
|
64
|
|
|
return $task->completed(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Marks task as failed. |
69
|
|
|
* |
70
|
|
|
* @param Task $task |
71
|
|
|
* @param PipelineTaskFailedException $e |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
protected function taskFailed(Task $task, PipelineTaskFailedException $e) |
76
|
|
|
{ |
77
|
|
|
$this->fire(new PipelineTaskFailed($task, $e)); |
78
|
|
|
|
79
|
|
|
return $task->failed($e); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get container instance. |
84
|
|
|
* |
85
|
|
|
* @return \Illuminate\Contracts\Container\Container |
86
|
|
|
*/ |
87
|
|
|
protected function getContainer() |
88
|
|
|
{ |
89
|
|
|
return $this->container; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Create new pipeline instance. |
94
|
|
|
* |
95
|
|
|
* @return Pipeline |
96
|
|
|
*/ |
97
|
|
|
protected function pipeline() |
98
|
|
|
{ |
99
|
|
|
return new Pipeline($this->getContainer()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Fire given event. |
104
|
|
|
* |
105
|
|
|
* @param mixed $event |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
protected function fire($event) |
110
|
|
|
{ |
111
|
|
|
if (!$this->hasEvents()) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->getEvents()->dispatch($event); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Determines if manager's container has events dispatcher bound. |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
protected function hasEvents() |
124
|
|
|
{ |
125
|
|
|
return $this->getContainer()->bound('events'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the events dispatcher instance. |
130
|
|
|
* |
131
|
|
|
* @return Dispatcher |
132
|
|
|
*/ |
133
|
|
|
protected function getEvents() |
134
|
|
|
{ |
135
|
|
|
return $this->getContainer()->make('events'); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|