OrderAddress   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 11
c 4
b 1
f 1
lcom 0
cbo 3
dl 0
loc 101
ccs 52
cts 52
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormattedPostCodeWithHyphen() 0 11 4
A assign() 0 17 1
A map() 0 23 2
A mapShipmentItem() 0 9 1
A getFormattedPhone() 0 15 3
1
<?php
2
3
namespace Iris\Mapping;
4
5
class OrderAddress extends Base
6
{
7
    /**
8
     * {@inheritdoc}
9
     */
10 2
    public function assign(array $externalData)
11
    {
12 2
        $salesOrderAddress = new \Iris\Transfer\Sales\Order\Address(array(
13 2
            'firstName'                 => $externalData['first_name'],
14 2
            'lastName'                  => $externalData['last_name'],
15 2
            'address1'                  => $externalData['shipping_address']['street'],
16 2
            'streetNumber'              => $externalData['shipping_address']['number'],
17 2
            'complement'                => $externalData['shipping_address']['complement'],
18 2
            'city'                      => $externalData['shipping_address']['city'],
19 2
            'postcode'                  => $externalData['shipping_address']['postal_code'],
20 2
            'state'                     => $externalData['shipping_address']['state'],
21 2
            'neighborhood'              => $externalData['shipping_address']['neighborhood'],
22 2
            'phone'                     => $this->getFormattedPhone($externalData['phones'][0])
23 2
        ));
24
25 2
        return $salesOrderAddress;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $salesOrderAddress; (Iris\Transfer\Sales\Order\Address) is incompatible with the return type declared by the abstract method Iris\Mapping\Base::assign of type Iris\Transfer\Catalog\ConfigCollection.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function map($internalData)
32
    {
33 2
        $salesOrderAddress = $internalData->getAddress();
34
35
        $externalData = array(
36
            'address' => array(
37 2
                'receiverName'  => $salesOrderAddress->getFirstName() . ' ' . $salesOrderAddress->getLastName(),
38 2
                'postalCode'    => $this->getFormattedPostCodeWithHyphen($salesOrderAddress->getPostcode()),
39 2
                'city'          => $salesOrderAddress->getCity(),
40 2
                'state'         => $salesOrderAddress->getState(),
41 2
                'street'        => $salesOrderAddress->getAddress1() . ' ' . $salesOrderAddress->getAddress2(),
42 2
                'number'        => $salesOrderAddress->getStreetNumber(),
43 2
                'neighborhood'  => $salesOrderAddress->getNeighborhood(),
44 2
                'complement'    => $salesOrderAddress->getComplement()
45 2
            )
46 2
        );
47
        
48 2
        foreach ($internalData->getItemCollection() as $salesOrderItem) {
49 2
            $externalData['logisticsInfo'][] = $this->mapShipmentItem($salesOrderItem);
50 2
        }
51
52 2
        return $externalData;
53
    }
54
55
    /**
56
     * @param \Iris\Transfer\Sales\Order\Item $salesOrderItem
57
     * @return array
58
     */
59 2
    private function mapShipmentItem(\Iris\Transfer\Sales\Order\Item $salesOrderItem)
60
    {
61
        return array(
62 2
            'item'              => $salesOrderItem->getSku(),
63 2
            'shippingLockTTL'   => $salesOrderItem->getShipmentTotalDeliveryTime(),
64 2
            'shippingEstimate'  => $salesOrderItem->getShipmentTotalDeliveryTime(),
65 2
            'shipping_amount'   => $salesOrderItem->getShippingAmount()
66 2
        );
67
    }
68
69
    /**
70
     * @param $postCode
71
     * @return string
72
     */
73 2
    private function getFormattedPostCodeWithHyphen($postCode)
74
    {
75 2
        if (empty($postCode) ||
76 2
            strlen($postCode) !== 8 ||
77 1
            strpos($postCode, '-') !== false
78 2
        ) {
79 1
            return $postCode;
80
        }
81
82 1
        return substr_replace($postCode, '-', 5, 0);
83
    }
84
85
    /**
86
     * todo move method to a better place
87
     * @param $phone
88
     * @return string
89
     */
90 2
    private function getFormattedPhone($phone)
91
    {
92 2
        $number = trim($phone);
93 2
        $formattedNumber = $number;
94
95 2
        if (in_array(strlen($number), array(10, 11))) {
96 1
            $formattedNumber = preg_replace('#^([0-9]{2})([0-9]{4,5})([0-9]{4})$#', '(\1) \2-\3', $number);
97
98 2
        } elseif (8 === strlen($number)) {
99 1
            $formattedNumber = preg_replace('#^([0-9]{4})([0-9]{4})$#', '(xx) \1-\2', $number);
100
101 1
        }
102
103 2
        return $formattedNumber;
104
    }
105
}
106