Completed
Push — develop ( 94ac5e...9b8817 )
by Jens
14:35
created

Price::getCurrentValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 * @created: 04.02.15, 16:43
5
 */
6
7
namespace Commercetools\Core\Model\Common;
8
9
use Commercetools\Core\Model\Channel\ChannelReference;
10
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
11
use Commercetools\Core\Model\CustomField\CustomFieldObject;
12
13
/**
14
 * @package Commercetools\Core\Model\Common
15
 * @link https://dev.commercetools.com/http-api-projects-products.html#price
16
 * @method Money getValue()
17
 * @method string getCountry()
18
 * @method CustomerGroupReference getCustomerGroup()
19
 * @method ChannelReference getChannel()
20
 * @method DiscountedPrice getDiscounted()
21
 * @method Price setValue(Money $value = null)
22
 * @method Price setCountry(string $country = null)
23
 * @method Price setCustomerGroup(CustomerGroupReference $customerGroup = null)
24
 * @method Price setChannel(ChannelReference $channel = null)
25
 * @method Price setDiscounted(DiscountedPrice $discounted = null)
26
 * @method string getId()
27
 * @method Price setId(string $id = null)
28
 * @method DateTimeDecorator getValidFrom()
29
 * @method Price setValidFrom(\DateTime $validFrom = null)
30
 * @method DateTimeDecorator getValidUntil()
31
 * @method Price setValidUntil(\DateTime $validUntil = null)
32
 * @method CustomFieldObject getCustom()
33
 * @method Price setCustom(CustomFieldObject $custom = null)
34
 */
35
class Price extends JsonObject
36
{
37
    const ID = 'id';
38
    const VALUE = 'value';
39
    const COUNTRY = 'country';
40
    const CUSTOMER_GROUP = 'customerGroup';
41
    const CHANNEL = 'channel';
42
    const VALID_FROM = 'validFrom';
43
    const VALID_UNTIL = 'validUntil';
44
    const DISCOUNTED = 'discounted';
45
    const CUSTOM = 'custom';
46
47 21
    public function fieldDefinitions()
48
    {
49
        return [
50 21
            static::ID => [static::TYPE => 'string'],
51 21
            static::VALUE => [self::TYPE => '\Commercetools\Core\Model\Common\Money'],
52 21
            static::COUNTRY => [self::TYPE => 'string'],
53 21
            static::CUSTOMER_GROUP => [self::TYPE => '\Commercetools\Core\Model\CustomerGroup\CustomerGroupReference'],
54 21
            static::CHANNEL => [self::TYPE => '\Commercetools\Core\Model\Channel\ChannelReference'],
55 21
            static::VALID_FROM => [
56 21
                self::TYPE => '\DateTime',
57 21
                self::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
58
            ],
59 21
            static::VALID_UNTIL => [
60 21
                self::TYPE => '\DateTime',
61 21
                self::DECORATOR => '\Commercetools\Core\Model\Common\DateTimeDecorator'
62
            ],
63 21
            static::DISCOUNTED => [self::TYPE => '\Commercetools\Core\Model\Common\DiscountedPrice'],
64 21
            static::CUSTOM => [static::TYPE => '\Commercetools\Core\Model\CustomField\CustomFieldObject'],
65
        ];
66
    }
67
68
    /**
69
     * @param Money $money
70
     * @param Context|callable $context
71
     * @return Price
72
     */
73 3
    public static function ofMoney(Money $money, $context = null)
74
    {
75 3
        $price = static::of($context);
76 3
        return $price->setValue($money);
77
    }
78
79 1
    public function __toString()
80
    {
81 1
        return $this->getValue()->__toString();
82
    }
83
84
    /**
85
     * @return Money
86
     */
87 2
    public function getCurrentValue()
88
    {
89 2
        if ($this->getDiscounted() instanceof DiscountedPrice) {
90 2
            return $this->getDiscounted()->getValue();
91
        }
92
93
        return $this->getValue();
94
    }
95
}
96