Client   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 115
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setTransformer() 0 6 1
A getTransformer() 0 8 2
A sendJob() 0 13 1
A deleteJob() 0 19 1
A getResponse() 0 7 2
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 4
    public function __construct($authCode)
42
    {
43 4
        $uri = 'https://talent.stackoverflow.com/api/jobs?code=' . (string) $authCode;
44
45 4
        parent::__construct($uri);
46 4
    }
47
48
    /**
49
     * Set the data transformer
50
     *
51
     * @param DataTransformer $transformer
52
     *
53
     * @return self
54
     */
55 2
    public function setTransformer(DataTransformer $transformer)
56
    {
57 2
        $this->transformer = $transformer;
58
59 2
        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 2
    public function getTransformer()
70
    {
71 2
        if (!$this->transformer) {
72 1
            $this->setTransformer(new DataTransformer());
73 1
        }
74
75 2
        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 1
    public function sendJob($job, $data)
87
    {
88 1
        $this->reset();
89
90 1
        $xml = $this->getTransformer()->transform($job, $data);
91 1
        $this->getLogger()->debug($xml);
92 1
        $this->setRawBody($xml);
93 1
        $this->setMethod(Request::METHOD_POST);
94
95 1
        $response = $this->send();
96
97 1
        return $response;
98
    }
99
100
    /**
101
     * Delete a job listing.
102
     *
103
     * @param int $externalId
104
     *
105
     * @return Response
106
     */
107 1
    public function deleteJob($externalId)
108
    {
109 1
        $this->reset();
110
111 1
        $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><job></job>');
112
113 1
        $xml->addChild('action', 'delete');
114 1
        $xml->addChild('jobid', $externalId);
115 1
        $xml->addChild('test', 'true');
116
117 1
        $xmlStr = $xml->asXML();
118
119 1
        $this->setRawBody($xmlStr);
120 1
        $this->setMethod(Request::METHOD_POST);
121
122 1
        $response = $this->send();
123
124 1
        return $response;
125
    }
126
127
    /**
128
     * Get the response object.
129
     *
130
     * @return Response
131
     */
132 2
    public function getResponse()
133
    {
134 2
        if (empty($this->response)) {
135 2
            $this->response = new Response();
136 2
        }
137 2
        return $this->response;
138
    }
139
}