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
|
10 |
|
public function __construct(Client $client) |
44
|
|
|
{ |
45
|
10 |
|
$this->client = $client; |
46
|
10 |
|
} |
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
|
6 |
|
public function send(JobInterface $job, $data) |
59
|
|
|
{ |
60
|
|
|
|
61
|
|
|
/* @var \StackoverflowApi\Entity\JobData $jobData */ |
62
|
|
|
|
63
|
6 |
|
$log = $this->getLogger(); |
64
|
6 |
|
$log->info('Send Job: ' . $job->getId()); |
65
|
6 |
|
$status = true; |
66
|
|
|
|
67
|
6 |
|
$jobData = $job->hasAttachedEntity('stackoverflow') ? $job->getAttachedEntity('stackoverflow') : $job->createAttachedEntity(JobData::class, ['jobId' => $job->getId()], 'stackoverflow'); |
68
|
|
|
|
69
|
6 |
|
if ($jobData->isOnline()) { |
70
|
1 |
|
$data['action'] = 'put'; |
71
|
1 |
|
$data['externalId'] = $jobData->getExternalId(); |
72
|
1 |
|
$log->debug('--> Job is already online: External id ' . $jobData->getExternalId() . ': update'); |
73
|
1 |
|
} else { |
74
|
5 |
|
$data['action'] = 'post'; |
75
|
5 |
|
$log->debug('--> Job is not online: insert'); |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
$log->debug('--> data:' , $data); |
79
|
6 |
|
$response = $this->client->sendJob($job, $data); |
80
|
|
|
|
81
|
6 |
|
$apiResponse = new ApiResponse($response); |
|
|
|
|
82
|
|
|
|
83
|
6 |
|
$result = $response->getXml(); |
84
|
6 |
|
if ($result) { |
85
|
|
|
//$result = $result->response; |
86
|
2 |
|
if ('success' == $result->result) { |
87
|
1 |
|
$jobData->setExternalId($result->jobid) |
88
|
1 |
|
->setExternalUrl($result->joburl) |
89
|
1 |
|
->setIsOnline(true); |
90
|
1 |
|
$log->info('==> Successfully send ' . $job->getId(), ['id' => $result->jobid, 'url' => $result->joburl]); |
91
|
|
|
|
92
|
1 |
|
} else { |
93
|
1 |
|
$status = false; |
94
|
1 |
|
$log->err('==> Sending job ' . $job->getId() . ' failed.'); |
95
|
1 |
|
$errors = (array) $result->errors->error; |
96
|
1 |
|
$log->debug($response->getStatusCode() . ': ' . $response->getReasonPhrase(), ['errors' => $errors, 'body' => $response->getBody()]); |
97
|
|
|
} |
98
|
2 |
|
} else { |
99
|
4 |
|
$status = false; |
100
|
4 |
|
$log->err('==> Unexpected error: ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase()); |
101
|
4 |
|
$log->debug($response->getBody()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/* temporarely disabled due to encondig issues. */ |
105
|
|
|
/*$jobData->addResponse($apiResponse);*/ |
106
|
|
|
|
107
|
6 |
|
return $status; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Delete a job from stackoverflow |
112
|
|
|
* |
113
|
|
|
* @param JobInterface $job |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
3 |
|
public function remove(JobInterface $job) |
118
|
|
|
{ |
119
|
|
|
/* @var \StackoverflowApi\Entity\JobData $jobData */ |
120
|
3 |
|
$jobData = $job->getAttachedEntity('stackoverflow'); |
121
|
|
|
|
122
|
3 |
|
if ($jobData->isOnline()) { |
123
|
2 |
|
$response = $this->client->deleteJob($jobData->getExternalId()); |
124
|
|
|
|
125
|
2 |
|
$apiResponse = new ApiResponse($response); |
126
|
|
|
|
127
|
2 |
|
$jobData->addResponse($apiResponse); |
128
|
|
|
|
129
|
2 |
|
$result = $response->getXml(); |
130
|
2 |
|
if ($result && 'success' == $result->result) { |
131
|
1 |
|
$jobData->setIsOnline(false); |
132
|
1 |
|
return true; |
133
|
|
|
} |
134
|
1 |
|
} |
135
|
|
|
|
136
|
2 |
|
return false; |
137
|
|
|
} |
138
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.