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

Geo   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setupClient() 0 8 1
A preQuery() 0 6 2
B processResult() 0 29 4
A queryCoords() 0 14 2
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
15
/**
16
 * ${CARET}
17
 * 
18
 * @author Mathias Gelhausen <[email protected]>
19
 * @todo write test 
20
 */
21
class Geo extends AbstractClient
22
{
23
    protected function setupClient($uri)
24
    {
25
        $client = parent::setupClient($uri);
26
        $client->setMethod('GET');
27
        $client->setParameterGet(['country' => 'DE', 'zoom' => 1]);
28
29
        return $client;
30
    }
31
32
    protected function preQuery($term, array $params)
33
    {
34
        $query = $this->client->getRequest()->getQuery();
35
        $query->set('q', $term);
36
        $query->set('plz', 0 < (int) $term ? 1 : 0);
37
    }
38
39
    protected function processResult($result)
40
    {
41
        $result = json_decode($result, JSON_OBJECT_AS_ARRAY);
42
43
        $r=[];
44
        foreach ($result["result"] as $val) {
45
            $coords = $this->queryCoords($val);
46
            if (false !== strpos($val, ',')) {
47
                $parts = explode(',', $val, 2);
48
                $name = trim($parts[0]);
49
                $state = trim($parts[1]);
50
            } else {
51
                $name = $val;
52
                $state = null;
53
            }
54
55
            $r[] = [
56
                'name' => $name,
57
                'state' => $state,
58
                'coordinates' => $coords,
59
                'id' => $coords
60
                        ? 'c:' . $coords[0] . ':' . $coords[1]
61
                        : 'q:' . $val,
62
                'data' => json_encode($val),
63
            ];
64
        }
65
66
        return $r;
67
    }
68
69
    public function queryCoords($term)
70
    {
71
        $query = $this->client->getRequest()->getQuery();
72
        $query->set('coor', 1)->set('q', $term);
73
74
        $response = $this->client->send();
75
        $result = $response->getBody();
76
        $result = json_decode($result, JSON_OBJECT_AS_ARRAY);
77
        if (!isset($result["result"][0])) { return false; }
78
        $coord = $result["result"][0];
79
        $coord = explode(',', $coord);
80
81
        return [str_replace('.', ',', $coord[1]), str_replace('.',',',$coord[0])];
82
    }
83
}