Completed
Push — develop ( c1c65f...ad5811 )
by Jens
08:43
created

CartDiscountValue::fromArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 3.0416
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\CartDiscount;
7
8
use Commercetools\Core\Model\Common\Context;
9
use Commercetools\Core\Model\Common\DiscountValue;
10
use Commercetools\Core\Model\Common\MoneyCollection;
11
12
/**
13
 * @package Commercetools\Core\Model\CartDiscount
14
 * @link https://dev.commercetools.com/http-api-projects-cartDiscounts.html#cartdiscountvalue
15
 * @deprecated use RelativeCartDiscountValue, AbsoluteCartDiscountValue or GiftLineItemCartDiscountValue instead.
16
 * @method string getType()
17
 * @method CartDiscountValue setType(string $type = null)
18
 * @method int getPermyriad()
19
 * @method CartDiscountValue setPermyriad(int $permyriad = null)
20
 * @method MoneyCollection getMoney()
21
 * @method CartDiscountValue setMoney(MoneyCollection $money = null)
22
 */
23
class CartDiscountValue extends DiscountValue
24
{
25
    const TYPE_RELATIVE = 'relative';
26
    const TYPE_ABSOLUTE = 'absolute';
27
    const TYPE_GIFT_LINE_ITEM = 'giftLineItem';
28
29
    /**
30
     * @param array $data
31
     * @param Context|callable $context
32
     * @return static
33
     */
34 2
    public static function fromArray(array $data, $context = null)
35
    {
36 2
        if (isset($data['type'])) {
37 2
            $className = static::getClassByType($data['type']);
38 2
            if (class_exists($className)) {
39 2
                return new $className($data, $context);
40
            }
41
        }
42
        return new static($data, $context);
0 ignored issues
show
Deprecated Code introduced by
The class Commercetools\Core\Model...count\CartDiscountValue has been deprecated with message: use RelativeCartDiscountValue, AbsoluteCartDiscountValue or GiftLineItemCartDiscountValue instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
43
    }
44
45 2
    protected static function getClassByType($type)
46
    {
47 2
        $className = '\Commercetools\Core\Model\CartDiscount\\' . ucfirst($type) . 'CartDiscountValue';
48 2
        return $className;
49
    }
50
}
51