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\Service; |
12
|
|
|
|
13
|
|
|
use Doctrine\ODM\MongoDB\DocumentRepository; |
14
|
|
|
use Jobs\Entity\JobInterface; |
15
|
|
|
use StackoverflowApi\Client\Client; |
16
|
|
|
use StackoverflowApi\Entity\ApiResponse; |
17
|
|
|
use StackoverflowApi\Entity\JobData; |
18
|
|
|
use Zend\Log\LoggerAwareInterface; |
19
|
|
|
use Zend\Log\LoggerAwareTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Manager for job listings on stackoverflow. |
23
|
|
|
* |
24
|
|
|
* @author Mathias Gelhausen <[email protected]> |
25
|
|
|
* @since 0.1.0 |
26
|
|
|
*/ |
27
|
|
|
class JobsManager implements LoggerAwareInterface |
28
|
|
|
{ |
29
|
|
|
use LoggerAwareTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Stackoverflow client. |
33
|
|
|
* |
34
|
|
|
* @var \StackoverflowApi\Client\Client |
35
|
|
|
*/ |
36
|
|
|
private $client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates a job manager instance. |
40
|
|
|
* |
41
|
|
|
* @param Client $client |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Client $client) |
44
|
|
|
{ |
45
|
|
|
$this->client = $client; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Send a job to stackoverflow. |
50
|
|
|
* |
51
|
|
|
* Determines automatically, wether the job needs to be posted or updated. |
52
|
|
|
* |
53
|
|
|
* @param JobInterface $job |
54
|
|
|
* @param array $data |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function send(JobInterface $job, $data) |
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
/* @var \StackoverflowApi\Entity\JobData $jobData */ |
62
|
|
|
|
63
|
|
|
$log = $this->getLogger(); |
64
|
|
|
$log->info('Send Job: ' . $job->getId()); |
65
|
|
|
$status = true; |
66
|
|
|
|
67
|
|
|
$jobData = $job->hasAttachedEntity('stackoverflow') ? $job->getAttachedEntity('stackoverflow') : $job->createAttachedEntity(JobData::class, ['jobId' => $job->getId()], 'stackoverflow'); |
68
|
|
|
|
69
|
|
|
if ($jobData->isOnline()) { |
70
|
|
|
$data['action'] = 'put'; |
71
|
|
|
$data['externalId'] = $jobData->getExternalId(); |
72
|
|
|
$log->debug('--> Job is already online: External id ' . $jobData->getExternalId() . ': update'); |
73
|
|
|
} else { |
74
|
|
|
$data['action'] = 'post'; |
75
|
|
|
$log->debug('--> Job is not online: insert'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$log->debug('--> data:' , $data); |
79
|
|
|
$response = $this->client->sendJob($job, $data); |
80
|
|
|
|
81
|
|
|
$apiResponse = new ApiResponse($response); |
82
|
|
|
|
83
|
|
|
$result = $response->getXml(); |
84
|
|
|
if ($result) { |
85
|
|
|
//$result = $result->response; |
|
|
|
|
86
|
|
|
if ('success' == $result->result) { |
87
|
|
|
$jobData->setExternalId($result->jobid) |
88
|
|
|
->setExternalUrl($result->joburl) |
89
|
|
|
->setIsOnline(true); |
90
|
|
|
$log->info('==> Successfully send ' . $job->getId(), ['id' => $result->jobid, 'url' => $result->joburl]); |
91
|
|
|
|
92
|
|
|
} else { |
93
|
|
|
$status = false; |
94
|
|
|
$log->err('==> Sending job ' . $job->getId() . ' failed.'); |
95
|
|
|
$errors = (array) $result->errors->error; |
96
|
|
|
$log->debug($response->getStatusCode() . ': ' . $response->getReasonPhrase(), ['errors' => $errors, 'body' => $response->getBody()]); |
97
|
|
|
} |
98
|
|
|
} else { |
99
|
|
|
$status = false; |
100
|
|
|
$log->err('==> Unexpected error: ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase()); |
101
|
|
|
$log->debug($response->getBody()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$jobData->addResponse($apiResponse); |
105
|
|
|
|
106
|
|
|
return $status; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Delete a job from stackoverflow |
111
|
|
|
* |
112
|
|
|
* @param JobInterface $job |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function remove(JobInterface $job) |
117
|
|
|
{ |
118
|
|
|
/* @var \StackoverflowApi\Entity\JobData $jobData */ |
119
|
|
|
$jobData = $job->getAttachedEntity('stackoverflow'); |
120
|
|
|
|
121
|
|
|
if ($jobData->isOnline()) { |
122
|
|
|
$response = $this->client->deleteJob($jobData->getExternalId()); |
123
|
|
|
|
124
|
|
|
$apiResponse = new ApiResponse($response); |
125
|
|
|
|
126
|
|
|
$jobData->addResponse($apiResponse); |
127
|
|
|
|
128
|
|
|
$result = $response->getXml(); |
129
|
|
|
if ($result && 'success' == $result->result) { |
130
|
|
|
$jobData->setIsOnline(false); |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.