Completed
Push — develop ( 3b8197...211573 )
by Jens
19:17
created

CartAddLineItemAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 52
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fieldDefinitions() 0 16 1
A ofProductIdVariantIdAndQuantity() 0 4 1
A ofSkuAndQuantity() 0 4 1
A __construct() 0 5 1
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Request\Carts\Command;
7
8
use Commercetools\Core\Model\Cart\ExternalLineItemTotalPrice;
9
use Commercetools\Core\Model\Common\Context;
10
use Commercetools\Core\Model\Common\Money;
11
use Commercetools\Core\Request\AbstractAction;
12
use Commercetools\Core\Model\Channel\ChannelReference;
13
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
14
use Commercetools\Core\Model\TaxCategory\ExternalTaxRateDraft;
15
16
/**
17
 * @package Commercetools\Core\Request\Carts\Command
18
 * @link https://dev.commercetools.com/http-api-projects-carts.html#add-lineitem
19
 * @method string getAction()
20
 * @method CartAddLineItemAction setAction(string $action = null)
21
 * @method string getProductId()
22
 * @method CartAddLineItemAction setProductId(string $productId = null)
23
 * @method int getVariantId()
24
 * @method CartAddLineItemAction setVariantId(int $variantId = null)
25
 * @method int getQuantity()
26
 * @method CartAddLineItemAction setQuantity(int $quantity = null)
27
 * @method ChannelReference getSupplyChannel()
28
 * @method CartAddLineItemAction setSupplyChannel(ChannelReference $supplyChannel = null)
29
 * @method ChannelReference getDistributionChannel()
30
 * @method CartAddLineItemAction setDistributionChannel(ChannelReference $distributionChannel = null)
31
 * @method CustomFieldObjectDraft getCustom()
32
 * @method CartAddLineItemAction setCustom(CustomFieldObjectDraft $custom = null)
33
 * @method ExternalTaxRateDraft getExternalTaxRate()
34
 * @method CartAddLineItemAction setExternalTaxRate(ExternalTaxRateDraft $externalTaxRate = null)
35
 * @method Money getExternalPrice()
36
 * @method CartAddLineItemAction setExternalPrice(Money $externalPrice = null)
37
 * @method ExternalLineItemTotalPrice getExternalTotalPrice()
38
 * @method CartAddLineItemAction setExternalTotalPrice(ExternalLineItemTotalPrice $externalTotalPrice = null)
39
 * @method string getSku()
40
 * @method CartAddLineItemAction setSku(string $sku = null)
41
 */
42
class CartAddLineItemAction extends AbstractAction
43
{
44 26
    public function fieldDefinitions()
45
    {
46
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('action' =>...temTotalPrice::class)); (array<string,array>) is incompatible with the return type of the parent method Commercetools\Core\Reque...ction::fieldDefinitions of type array<string,array<string,string>>.

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...
47 26
            'action' => [static::TYPE => 'string'],
48 26
            'productId' => [static::TYPE => 'string'],
49 26
            'variantId' => [static::TYPE => 'int'],
50 26
            'sku' => [static::TYPE => 'string'],
51 26
            'quantity' => [static::TYPE => 'int'],
52 26
            'supplyChannel' => [static::TYPE => ChannelReference::class],
53 26
            'distributionChannel' => [static::TYPE => ChannelReference::class],
54 26
            'custom' => [static::TYPE => CustomFieldObjectDraft::class],
55 26
            'externalTaxRate' => [static::TYPE => ExternalTaxRateDraft::class],
56 26
            'externalPrice' => [static::TYPE => Money::class],
57 26
            'externalTotalPrice' => [static::TYPE => ExternalLineItemTotalPrice::class],
58
        ];
59
    }
60
61
    /**
62
     * @param string $productId
63
     * @param int $variantId
64
     * @param Context|callable $context
65
     * @param int $quantity
66
     * @return CartAddLineItemAction
67
     */
68 23
    public static function ofProductIdVariantIdAndQuantity($productId, $variantId, $quantity, $context = null)
69
    {
70 23
        return static::of($context)->setProductId($productId)->setVariantId($variantId)->setQuantity($quantity);
71
    }
72
73
    /**
74
     * @param string $sku
75
     * @param Context|callable $context
76
     * @param int $quantity
77
     * @return CartAddLineItemAction
78
     */
79 1
    public static function ofSkuAndQuantity($sku, $quantity, $context = null)
80
    {
81 1
        return static::of($context)->setSku($sku)->setQuantity($quantity);
82
    }
83
84
    /**
85
     * @param array $data
86
     * @param Context|callable $context
87
     */
88 24
    public function __construct(array $data = [], $context = null)
89
    {
90 24
        parent::__construct($data, $context);
91 24
        $this->setAction('addLineItem');
92 24
    }
93
}
94