ValoreBooksPriceClient::addPricesToCollection()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
crap 3
1
<?php namespace Packback\Prices\Clients;
2
3
use Packback\Prices\PriceClient;
4
5
class ValoreBooksPriceClient extends PriceClient
6
{
7
    const RETAILER = 'valorebooks';
8
9 16
    public function __construct($config = [])
10
    {
11 16
        if (!isset($config['site_id'])) {
12 2
            throw new \InvalidArgumentException(
13
                'A "site_id" key must be included in the $config array argument'
14 2
            );
15
        }
16 16
        parent::__construct();
17 16
        $this->baseUrl = 'http://prices.valorebooks.com/lookup-multiple-categories';
18 16
        $this->query['SiteID'] = $config['site_id'];
19 16
    }
20
21 2
    public function getPricesForIsbns($isbns = [])
22
    {
23 2
        foreach ($isbns as $isbn) {
24 2
            $this->query['ProductCode'] = $isbn;
25 2
            $response = $this->send();
26 2
            $this->addPricesToCollection($response);
27 2
        }
28 2
        return $this->collection;
29
    }
30
31 10
    public function addPricesToCollection($payload)
32
    {
33 10
        if (isset($payload['rental-offer'])) {
34 4
            $this->processRentalPrices($payload);
35 4
        }
36 10
        if (isset($payload['sale-offer'])) {
37 4
            $this->processSalePrices($payload);
38 4
        }
39 10
        return $this->collection;
40
    }
41
42 6
    public function processRentalPrices($payload)
43
    {
44 6
        $rental = $payload['rental-offer'];
45 6
        $isbn = $payload['product-code'];
46 6
        $retailer = self::RETAILER;
47 6
        $condition = $this->getConditionFromOffer($rental);
48 6
        $shipping_price = $this->getLowestShippingPriceFromOffer($rental);
49 6
        $link = $this->getLinkFromOffer($rental);
50
51 6
        if (isset($rental['ninty-day-price'])) {
52 6
            $price = $this->createNewPrice();
53 6
            $price->isbn13 = $isbn;
54 6
            $price->retailer = $retailer;
55 6
            $price->price = $rental['ninty-day-price'];
56 6
            $price->term = parent::TERM_QUARTER;
57 6
            $price->condition = $condition;
58 6
            $price->shipping_price = $shipping_price;
59 6
            $price->url = $link;
60 6
            $this->collection[] = $price;
61 6
        }
62 6
        if (isset($rental['semester-price'])) {
63 6
            $price = $this->createNewPrice();
64 6
            $price->isbn13 = $isbn;
65 6
            $price->retailer = $retailer;
66 6
            $price->price = $rental['semester-price'];
67 6
            $price->term = parent::TERM_SEMESTER;
68 6
            $price->condition = $condition;
69 6
            $price->shipping_price = $shipping_price;
70 6
            $price->url = $link;
71 6
            $this->collection[] = $price;
72 6
        }
73 6
        return $this;
74
    }
75
76 6
    public function processSalePrices($payload)
77
    {
78 6
        $sale = $payload['sale-offer'];
79 6
        $isbn = $payload['product-code'];
80 6
        $retailer = self::RETAILER;
81 6
        $condition = $this->getConditionFromOffer($sale);
82 6
        $shipping_price = $this->getLowestShippingPriceFromOffer($sale);
83 6
        $link = $this->getLinkFromOffer($sale);
84
85 6
        $price = $this->createNewPrice();
86 6
        $price->isbn13 = $isbn;
87 6
        $price->retailer = $retailer;
88 6
        $price->price = $sale['price'];
89 6
        $price->term = parent::TERM_PERPETUAL;
90 6
        $price->condition = $condition;
91 6
        $price->shipping_price = $shipping_price;
92 6
        $price->url = $link;
93 6
        $this->collection[] = $price;
94 6
        return $this;
95
    }
96
97 8
    private function getConditionFromOffer($offer = null)
98
    {
99 8
        if (isset($offer['condition'])) {
100 6
            return $this->getConditionFromString($offer['condition']);
101
        } else {
102 6
            return parent::CONDITION_GOOD;
103
        }
104
    }
105
106 8
    private function getLinkFromOffer($offer = null)
107
    {
108 8
        return isset($offer['link']) ? $offer['link'] : null;
109
    }
110
111 8
    private function getLowestShippingPriceFromOffer($offer = null)
112
    {
113 8
        if (isset($offer['shipping-options'])) {
114 6
            if (isset($offer['shipping-options']['shipping']['price-first'])) {
115 6
                return $offer['shipping-options']['shipping']['price-first'];
116 4
            } elseif (isset($offer['shipping-options']['shipping'][0]['price-first'])) {
117 4
                foreach ($offer['shipping-options']['shipping'] as $option) {
118 4
                    if ($option['method'] == 'Standard') {
119 2
                        return $option['price-first'];
120
                    }
121 2
                }
122 2
            }
123 2
        }
124 4
        return null;
125
    }
126
}
127