Completed
Push — next ( ba018c...5105e3 )
by Mathias
10:34
created

Client::getResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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\Client;
12
13
use Jobs\Entity\JobInterface;
14
use Zend\Http\Client as ZendHttpClient;
15
use Zend\Http\Request;
16
use Zend\Log\LoggerAwareInterface;
17
use Zend\Log\LoggerAwareTrait;
18
19
/**
20
 * Client for the stackoverflow api.
21
 * 
22
 * @author Mathias Gelhausen <[email protected]>
23
 * @since 0.1.0
24
 */
25
class Client extends ZendHttpClient implements LoggerAwareInterface
26
{
27
    use LoggerAwareTrait;
28
29
    /**
30
     * The job data transformer
31
     *
32
     * @var DataTransformer
33
     */
34
    private $transformer;
35
36
    /**
37
     * Creates an instance.
38
     *
39
     * @param string $authCode The stackoverflow api authentication code.
40
     */
41
    public function __construct($authCode)
42
    {
43
        $uri = 'https://talent.stackoverflow.com/api/jobs?code=' . (string) $authCode;
44
45
        parent::__construct($uri);
46
    }
47
48
    /**
49
     * Set the data transformer
50
     *
51
     * @param DataTransformer $transformer
52
     *
53
     * @return self
54
     */
55
    public function setTransformer(DataTransformer $transformer)
56
    {
57
        $this->transformer = $transformer;
58
59
        return $this;
60
    }
61
62
    /**
63
     * Get the data transformer
64
     *
65
     * Create a new DataTransformer instance if none is set.
66
     *
67
     * @return DataTransformer
68
     */
69
    public function getTransformer()
70
    {
71
        if (!$this->transformer) {
72
            $this->setTransformer(new DataTransformer());
73
        }
74
75
        return $this->transformer;
76
    }
77
78
    /**
79
     * Send a job listing.
80
     *
81
     * @param JobInterface $job
82
     * @param array $data
83
     *
84
     * @return Response
85
     */
86
    public function sendJob($job, $data)
87
    {
88
        $this->reset();
89
90
        $xml = $this->getTransformer()->transform($job, $data);
91
        $this->getLogger()->debug($xml);
92
        $this->setRawBody($xml);
93
        $this->setMethod(Request::METHOD_POST);
94
95
        $response = $this->send();
96
97
        return $response;
98
    }
99
100
    /**
101
     * Delete a job listing.
102
     *
103
     * @param int $externalId
104
     *
105
     * @return Response
106
     */
107
    public function deleteJob($externalId)
108
    {
109
        $this->reset();
110
111
        $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><job></job>');
112
113
        $xml->addChild('action', 'delete');
114
        $xml->addChild('jobid', $externalId);
115
        $xml->addChild('test', 'true');
116
117
        $xmlStr = $xml->asXML();
118
119
        $this->setRawBody($xmlStr);
120
        $this->setMethod(Request::METHOD_POST);
121
122
        $response = $this->send();
123
124
        return $response;
125
    }
126
127
    /**
128
     * Get the response object.
129
     *
130
     * @return Response
131
     */
132
    public function getResponse()
133
    {
134
        if (empty($this->response)) {
135
            $this->response = new Response();
136
        }
137
        return $this->response;
138
    }
139
}