Completed
Push — develop ( afe0ec...7c3dfe )
by
unknown
13:30
created

Converter::toCoordinates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2015 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Geo\Form\GeoText;
12
13
use Geo\Entity\Geometry\Point;
14
use Jobs\Entity\Location;
15
use Zend\Http\Client;
16
use Zend\Json\Json;
17
18
/**
19
 * ${CARET}
20
 * 
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @todo write test 
23
 */
24
class Converter 
25
{
26
27
    public function toEntity($data, $type)
28
    {
29
        if ('photon' == $type) {
30
            $data = $this->normalizePhotonData($data);
31
        }
32
33
34
        if (empty($data)) {
35
            return new Location();
36
        }
37
        $entity = new Location();
38
        $entity->setCity($data['city'])
39
               ->setRegion($data['region'])
40
               ->setPostalcode($data['postalcode'])
41
               ->setCountry($data['country']);
42
        if (!empty($data['coordinates'])) {
43
               $entity->setCoordinates(new Point($data['coordinates']));
44
        }
45
46
        return $entity;
47
    }
48
49
    protected function normalizePhotonData($data)
50
    {
51
        if (empty($data)) {
52
            return [];
53
        }
54
55
        $data = Json::decode($data, Json::TYPE_ARRAY);
56
57
        $data = [
58
            'city' => isset($data['properties']['city']) ? $data['properties']['city'] : null,
59
            'region' => isset($data['properties']['state']) ? $data['properties']['state'] : null,
60
            'postalcode' => isset($data['properties']['postcode']) ? $data['properties']['postcode'] : null,
61
            'country' => isset($data['properties']['country']) ? $data['properties']['country'] : null,
62
            'coordinates' => isset($data['geometry']['coordinates']) ? $data['geometry']['coordinates'] : null,
63
        ];
64
65
        return $data;
66
    }
67
68
    public function toValue(Location $location, $type)
69
    {
70
        if ('photon' == $type) {
71
            $data = [
72
                "geometry" => [
73
                    "coordinates" => $location->getCoordinates()->getCoordinates(),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class GeoJson\GeoJson as the method getCoordinates() does only exist in the following sub-classes of GeoJson\GeoJson: GeoJson\Geometry\Geometry, GeoJson\Geometry\GeometryCollection, GeoJson\Geometry\LineString, GeoJson\Geometry\LinearRing, GeoJson\Geometry\MultiLineString, GeoJson\Geometry\MultiPoint, GeoJson\Geometry\MultiPolygon, GeoJson\Geometry\Point, GeoJson\Geometry\Polygon, Geo\Entity\Geometry\Point. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
74
                    "type" => $location->getCoordinates()->getType()
75
                ],
76
                "type" => "Feature",
77
                "properties" => [
78
                    "country" => $location->getCountry(),
79
                    "city" => $location->getCity(),
80
                    "state" => $location->getRegion(),
81
                    "postcode" => $location->getPostalcode()
82
                ]
83
            ];
84
        } else {
85
            $data = [];
86
        }
87
88
        return  $location->getCity() . '|' . Json::encode($data);
89
    }
90
91
    /**
92
     * used by the beo plugin only. We can hardcode the geoCoderUrl for the moment
93
     *
94
     * @param $input
95
     *
96
     * @return array|mixed|string
97
     */
98
    public function toCoordinates($input) {
99
        $client = new Client('http://api.cross-solution.de/geo');
100
        $client->setMethod('GET');
101
        $client->setParameterGet(array('q' => $input, 'country' => 'DE', 'coor' => 1, 'zoom' => 1 , 'strict' => 0));
102
        $response = $client->send();
103
        $result = $response->getBody();
104
        $result = json_decode($result);
105
        $result = (array) $result->result;
106
        return $result;
107
    }
108
}