Completed
Push — master ( 18bfc7...82e0b5 )
by Jens
18:00 queued 05:19
created

GeoLocation::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Common;
7
8
/**
9
 * @package Commercetools\Core\Model\Common
10
 *
11
 * @method string getType()
12
 * @method GeoLocation setType(string $type = null)
13
 * @method array getCoordinates()
14
 * @method GeoLocation setCoordinates(array $coordinates = null)
15
 */
16 View Code Duplication
class GeoLocation extends JsonObject
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    const TYPE_NAME = 'Point';
19
20
    /**
21
     * @param array $data
22
     * @param Context|callable $context
23
     */
24 3
    public function __construct(array $data = [], $context = null)
25
    {
26 3
        parent::__construct($data, $context);
27 3
        $name = static::TYPE_NAME;
28 3
        if (!empty($name)) {
29 3
            $this->setType(static::TYPE_NAME);
30
        }
31 3
    }
32
33
    /**
34
     * @return array
35
     */
36 3
    public function fieldDefinitions()
37
    {
38
        return [
39 3
            'type' => [static::TYPE => 'string'],
40 3
            'coordinates' => [static::TYPE => 'array']
41
        ];
42
    }
43
44
    /**
45
     * @param array $data
46
     * @param Context|callable $context
47
     * @return static
48
     */
49 1
    public static function fromArray(array $data, $context = null)
50
    {
51 1
        if (isset($data['type'])) {
52 1
            $className = static::getGeoTypeByTypeName($data['type']);
53 1
            if (class_exists($className)) {
54 1
                return new $className($data, $context);
55
            }
56
        }
57
        return new static($data, $context);
58
    }
59
60 1
    protected static function getGeoTypeByTypeName($apiType)
61
    {
62 1
        $className = '\Commercetools\Core\Model\Common\\Geo' . ucfirst($apiType);
63 1
        return $className;
64
    }
65
}
66