1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK Stackoverflow API |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2016 -2017 Cross Solution <http://cross-solution.de> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** */ |
11
|
|
|
namespace StackoverflowApi\Listener; |
12
|
|
|
|
13
|
|
|
use Jobs\Entity\StatusInterface; |
14
|
|
|
use Jobs\Listener\Events\JobEvent; |
15
|
|
|
use Jobs\Listener\Response\JobResponse; |
16
|
|
|
use StackoverflowApi\Service\JobsManager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Jobs events listener to post, update or remove job listing to / from the stackoverflow api. |
20
|
|
|
* |
21
|
|
|
* @author Mathias Gelhausen <[email protected]> |
22
|
|
|
* @since 0.1.0 |
23
|
|
|
*/ |
24
|
|
|
class JobsListener |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The manager. |
28
|
|
|
* |
29
|
|
|
* @var \StackoverflowApi\Service\JobsManager |
30
|
|
|
*/ |
31
|
|
|
private $manager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create an instance. |
35
|
|
|
* |
36
|
|
|
* @param JobsManager $manager |
37
|
|
|
*/ |
38
|
|
|
public function __construct(JobsManager $manager) |
39
|
|
|
{ |
40
|
|
|
$this->manager = $manager; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Callback for "JobAccepted" event. |
45
|
|
|
* |
46
|
|
|
* @param JobEvent $event |
47
|
|
|
* |
48
|
|
|
* @return JobResponse |
49
|
|
|
*/ |
50
|
|
|
public function onJobAccepted(JobEvent $event) |
51
|
|
|
{ |
52
|
|
|
if (!$event->hasPortal('stackoverflow')) { |
53
|
|
|
return $this->createResponse('Portal "stackoverflow" not activated for this job.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$job = $event->getJobEntity(); |
57
|
|
|
$options = $event->getParam('extraData'); |
58
|
|
|
$options = isset($options[ 'channels' ][ 'stackoverflow' ]) ? $options[ 'channels' ][ 'stackoverflow' ] : []; |
59
|
|
|
|
60
|
|
|
return $this->createResponse($this->manager->send($job, $options)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Callback for "StatusChanged" event. |
65
|
|
|
* |
66
|
|
|
* @param JobEvent $event |
67
|
|
|
* |
68
|
|
|
* @return JobResponse |
69
|
|
|
*/ |
70
|
|
|
public function onJobStatusChanged(JobEvent $event) |
71
|
|
|
{ |
72
|
|
|
$job = $event->getJobEntity(); |
73
|
|
|
|
74
|
|
|
if (!$job->hasAttachedEntity('stackoverflow') || StatusInterface::INACTIVE != $job->getStatus()->getName()) { |
75
|
|
|
return $this->createResponse('Job (' . $job->getId() . ') was not exported or is not inactive'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->createResponse($this->manager->remove($job)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create a JobResponse. |
83
|
|
|
* |
84
|
|
|
* @param string|bool $success |
85
|
|
|
* @param string $message |
86
|
|
|
* |
87
|
|
|
* @return JobResponse |
88
|
|
|
*/ |
89
|
|
|
private function createResponse($success, $message = '') |
90
|
|
|
{ |
91
|
|
|
if (is_string($success)) { |
92
|
|
|
return new JobResponse('stackoverflow', JobResponse::RESPONSE_DENIED, $success); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($success) { |
96
|
|
|
return new JobResponse('stackoverflow', JobResponse::RESPONSE_OK, $message); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return new JobResponse('stackoverflow', JobResponse::RESPONSE_FAIL, $message); |
100
|
|
|
} |
101
|
|
|
} |