Passed
Push — develop ( 02676e...d2b63d )
by Jens
08:11
created

PriceFinder::findPriceFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 5
crap 1
1
<?php
2
/**
3
 * @author @ct-jensschulze <[email protected]>
4
 */
5
6
namespace Commercetools\Commons\Helper;
7
8
use Commercetools\Core\Model\Channel\ChannelReference;
9
use Commercetools\Core\Model\Common\Price;
10
use Commercetools\Core\Model\Common\PriceCollection;
11
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
12
13
/**
14
 * @deprecated Please use the price selection functionality of the platform
15
 * @link http://dev.commercetools.com/http-api-projects-products.html#price-selection
16
 */
17
class PriceFinder
18
{
19
    private $currency;
20
    private $country;
21
    private $customerGroup;
22
    private $channel;
23
24 26
    public function __construct(
25
        $currency,
26
        $country = null,
27
        CustomerGroupReference $customerGroup = null,
28
        ChannelReference $channel = null
29
    ) {
30 26
        $this->currency = $currency;
31 26
        $this->country = $country;
32 26
        $this->customerGroup = $customerGroup;
33 26
        $this->channel = $channel;
34 26
    }
35
36
    /**
37
     * @param PriceCollection $prices
38
     * @return Price
39
     */
40 26
    public function findPrice(PriceCollection $prices)
41
    {
42 26
        $currency = $this->currency;
43 26
        $country = $this->country;
44 26
        $customerGroup = $this->customerGroup;
45 26
        $channel = $this->channel;
46
47 26
        $prices = new \CallbackFilterIterator(
48 26
            $prices,
49 26
            function ($price) use ($currency, $country, $customerGroup, $channel) {
50 26
                if (!$this->priceHasCurrency($price, $currency)) {
51 21
                    return false;
52
                }
53 26
                if (!$this->priceHasNoDate($price) && !$this->priceHasValidDate($price, new \DateTime())) {
54
                    return false;
55
                }
56 26 View Code Duplication
                if (is_null($country)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
57 6
                    if ($this->priceHas($price, 'country')) {
58 6
                        return false;
59
                    }
60 20
                } elseif (!$this->priceHasCountry($price, $country)) {
61 18
                    return false;
62
                }
63 26
                if (is_null($customerGroup)) {
64 12
                    if ($this->priceHas($price, 'customerGroup')) {
65 12
                        return false;
66
                    }
67 14
                } elseif (!$this->priceHasCustomerGroup($price, $customerGroup)) {
68 12
                    return false;
69
                }
70 26 View Code Duplication
                if (is_null($channel)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
71 12
                    if ($this->priceHas($price, 'channel')) {
72 12
                        return false;
73
                    }
74 14
                } elseif (!$this->priceHasChannel($price, $channel)) {
75 13
                    return false;
76
                }
77 26
                return true;
78 26
            }
79
        );
80
81 26
        foreach ($prices as $price) {
82 26
            return $price;
83
        }
84
        return null;
85
    }
86
87
    /**
88
     * @param $prices
89
     * @param $currency
90
     * @param string $country
91
     * @param CustomerGroupReference $customerGroup
92
     * @param ChannelReference $channel
93
     * @return Price|null
94
     */
95 26
    public static function findPriceFor(
96
        $prices,
97
        $currency,
98
        $country = null,
99
        CustomerGroupReference $customerGroup = null,
100
        ChannelReference $channel = null
101
    ) {
102 26
        $priceFinder = new static($currency, $country, $customerGroup, $channel);
0 ignored issues
show
Deprecated Code introduced by
The class Commercetools\Commons\Helper\PriceFinder has been deprecated with message: Please use the price selection functionality of the platform

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...
103
104 26
        return $priceFinder->findPrice($prices);
105
    }
106
107 26
    private function priceHasCurrency(Price $price, $currency)
108
    {
109 26
        return !is_null($price->getValue()) && $price->getValue()->getCurrencyCode() == $currency;
110
    }
111
112 20
    private function priceHasCountry(Price $price, $country)
113
    {
114 20
        return !is_null($price->getCountry()) && $price->getCountry() == $country;
115
    }
116
117 14
    private function priceHasCustomerGroup(Price $price, CustomerGroupReference $customerGroup)
118
    {
119 14
        return !is_null($price->getCustomerGroup()) && $price->getCustomerGroup()->getId() == $customerGroup->getId();
120
    }
121
122 14
    private function priceHasChannel(Price $price, ChannelReference $channel)
123
    {
124 14
        return !is_null($price->getChannel()) && $price->getChannel()->getId() == $channel->getId();
125
    }
126
127
    private function priceHasValidDate(Price $price, \DateTime $date)
128
    {
129
        $tooEarly = !is_null($price->getValidFrom()) && $price->getValidFrom()->getDateTime() > $date;
130
        $tooLate = !is_null($price->getValidUntil()) && $price->getValidUntil()->getDateTime() < $date;
131
        return !$tooEarly && !$tooLate;
132
    }
133
134 26
    private function priceHasNoDate(Price $price)
135
    {
136 26
        return is_null($price->getValidFrom()) && is_null($price->getValidUntil());
137
    }
138
139 18
    private function priceHas(Price $price, $field)
140
    {
141 18
        return !is_null($price->get($field));
142
    }
143
}
144