Passed
Push — develop ( 02676e...d2b63d )
by Jens
08:11
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.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
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
use DateTime;
13
14
/**
15
 * @package Commercetools\Core\Model\Common
16
 * @link https://dev.commercetools.com/http-api-projects-products.html#price
17
 * @method Money getValue()
18
 * @method string getCountry()
19
 * @method CustomerGroupReference getCustomerGroup()
20
 * @method ChannelReference getChannel()
21
 * @method DiscountedPrice getDiscounted()
22
 * @method Price setValue(Money $value = null)
23
 * @method Price setCountry(string $country = null)
24
 * @method Price setCustomerGroup(CustomerGroupReference $customerGroup = null)
25
 * @method Price setChannel(ChannelReference $channel = null)
26
 * @method Price setDiscounted(DiscountedPrice $discounted = null)
27
 * @method string getId()
28
 * @method Price setId(string $id = null)
29
 * @method DateTimeDecorator getValidFrom()
30
 * @method Price setValidFrom(DateTime $validFrom = null)
31
 * @method DateTimeDecorator getValidUntil()
32
 * @method Price setValidUntil(DateTime $validUntil = null)
33
 * @method CustomFieldObject getCustom()
34
 * @method Price setCustom(CustomFieldObject $custom = null)
35
 * @method PriceTierCollection getTiers()
36
 * @method Price setTiers(PriceTierCollection $tiers = null)
37
 */
38
class Price extends JsonObject
39
{
40
    const ID = 'id';
41
    const VALUE = 'value';
42
    const COUNTRY = 'country';
43
    const CUSTOMER_GROUP = 'customerGroup';
44
    const CHANNEL = 'channel';
45
    const VALID_FROM = 'validFrom';
46
    const VALID_UNTIL = 'validUntil';
47
    const DISCOUNTED = 'discounted';
48
    const CUSTOM = 'custom';
49
    const TIERS = 'tiers';
50
51 48 View Code Duplication
    public function fieldDefinitions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        return [
54 48
            static::ID => [static::TYPE => 'string'],
55 48
            static::VALUE => [self::TYPE => Money::class],
56 48
            static::COUNTRY => [self::TYPE => 'string'],
57 48
            static::CUSTOMER_GROUP => [self::TYPE => CustomerGroupReference::class],
58 48
            static::CHANNEL => [self::TYPE => ChannelReference::class],
59 48
            static::VALID_FROM => [
60 48
                self::TYPE => DateTime::class,
61 48
                self::DECORATOR => DateTimeDecorator::class
62
            ],
63 48
            static::VALID_UNTIL => [
64 48
                self::TYPE => DateTime::class,
65 48
                self::DECORATOR => DateTimeDecorator::class
66
            ],
67 48
            static::DISCOUNTED => [self::TYPE => DiscountedPrice::class],
68 48
            static::CUSTOM => [static::TYPE => CustomFieldObject::class],
69 48
            static::TIERS => [static::TYPE => PriceTierCollection::class]
70
        ];
71
    }
72
73
    /**
74
     * @param Money $money
75
     * @param Context|callable $context
76
     * @return Price
77
     */
78 3
    public static function ofMoney(Money $money, $context = null)
79
    {
80 3
        $price = static::of($context);
81 3
        return $price->setValue($money);
82
    }
83
84 1
    public function __toString()
85
    {
86 1
        return $this->getValue()->__toString();
87
    }
88
89
    /**
90
     * @return Money
91
     */
92 2
    public function getCurrentValue()
93
    {
94 2
        if ($this->getDiscounted() instanceof DiscountedPrice) {
95 2
            return $this->getDiscounted()->getValue();
96
        }
97
98
        return $this->getValue();
99
    }
100
}
101