Completed
Push — develop ( 4bf430...c74260 )
by
unknown
18:30
created

AbstractClient   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 77
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setupClient() 0 4 1
A preQuery() 0 5 1
B query() 0 24 5
A queryOne() 0 7 2
A processResult() 0 4 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Geo\Service;
12
13
use Zend\Http\Client;
14
use Zend\Json\Json;
15
16
/**
17
 * ${CARET}
18
 * 
19
 * @author Mathias Gelhausen <[email protected]>
20
 * @todo write test 
21
 */
22
class AbstractClient
23
{
24
    /**
25
     *
26
     *
27
     * @var \Zend\Http\Client
28
     */
29
    protected $client;
30
31
    /**
32
     *
33
     *
34
     * @var \Zend\Cache\Storage\Adapter\AbstractAdapter
35
     */
36
    protected $cache;
37
38
39
    public function __construct($uri, $cache = false)
40
    {
41
        $this->client = $this->setupClient($uri);
42
        $this->cache = $cache;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cache of type boolean is incompatible with the declared type object<Zend\Cache\Storag...dapter\AbstractAdapter> of property $cache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
44
//        $namespace = explode('\\', get_class($this));
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
45
//        $namespace = array_pop($namespace);
46
//        //$this->cache->setOptions(['namespace' => $namespace]);
47
    }
48
49
    protected function setupClient($uri)
50
    {
51
        return new Client($uri);
52
    }
53
54
    protected function preQuery($term, array $params)
55
    {
56
        $params['q'] = $term;
57
        $this->client->setParameterGet($params);
58
    }
59
60
    public function query($term, array $params = [])
61
    {
62
        $cacheId = md5($term);
63
64
        if ($this->cache && ($result = $this->cache->getItem($cacheId))) {
65
            return $result;
66
        }
67
68
        $this->preQuery($term, $params);
69
70
        $response = $this->client->send();
71
        if ($response->getStatusCode() !== 200) {
72
            throw new \RuntimeException('Query failed, because ' . $response->getReasonPhrase());
73
        }
74
75
        $result = $response->getBody();
76
        $result = $this->processResult($result);
77
78
        if ($this->cache) {
79
            $this->cache->setItem($cacheId, $result);
80
        }
81
82
        return $result;
83
    }
84
85
    public function queryOne($term)
86
    {
87
        $result = $this->query($term);
88
        $result = Json::decode($result, Json::TYPE_ARRAY);
89
90
        return isset($result[0]) ? $result[0] : false;
91
    }
92
93
    protected function processResult($result)
94
    {
95
        return $result;
96
    }
97
98
}