OrderItem::assign()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
1
<?php
2
3
namespace Iris\Mapping;
4
5
class OrderItem extends Base
6
{
7
    /**
8
     * {@inheritdoc}
9
     */
10 2
    public function assign(array $externalData)
11
    {
12 2
        if (!$externalData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $externalData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
13 1
            throw new \Exception('No order items sent.');
14
        }
15
16 1
        $itemCollection = new \Iris\Transfer\Sales\Order\ItemCollection();
17
18 1
        foreach ($externalData['order_items'] as $orderItem) {
19 1
            $itemCollection[] = $this->createSalesOrderItem($orderItem, $externalData);
20 1
        }
21
22 1
        return $itemCollection;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $itemCollection; (Iris\Transfer\Sales\Order\ItemCollection) 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...
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 1
    public function map($internalData)
29
    {
30
        $externalData = array(
31 1
            'id'                => $internalData->getIdSalesOrderItem(),
32 1
            'sku'               => $internalData->getSku(),
33 1
            'sku_supplier'      => $internalData->getSkuSupplierSimple(),
34 1
            'quantity'          => $internalData->getQuantity(),
35 1
            'price'             => $internalData->getPaidPrice(),
36 1
            'taxable_price'     => $internalData->getTaxablePrice()
37 1
        );
38
39 1
        return $externalData;
40
    }
41
42 1
    private function createSalesOrderItem($orderItem, $externalData)
43
    {
44 1
        $shippingAmount = ($externalData['freight_amount'] > 0)
45 1
            ? ($externalData['freight_amount'] / count($externalData['order_items']))
46 1
            : $externalData['freight_amount'];
47
48
        $data = array(
49 1
            'sku'                                   => $orderItem['sku'],
50 1
            'unit_price'                            => $orderItem['price'],
51 1
            'paid_price'                            => $orderItem['price'],
52 1
            'carrier_id'                            => $orderItem['carrier_id'],
53 1
            'shipment_carrier_delivery_time'        => $orderItem['carrier_delivery_time'],
54 1
            'shipment_warehouse_processing_time'    => $orderItem['warehouse_time'],
55 1
            'shipment_total_delivery_time'          => $orderItem['freight_time'],
56
            'shipping_amount'                       => $shippingAmount
57 1
        );
58
59 1
        return new \Iris\Transfer\Sales\Order\Item($data);
60
    }
61
}
62