ValoreBooksPriceClient   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
lcom 1
cbo 2
dl 0
loc 122
ccs 88
cts 88
cp 1
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getPricesForIsbns() 0 9 2
A addPricesToCollection() 0 10 3
B processRentalPrices() 0 33 3
A processSalePrices() 0 20 1
A getConditionFromOffer() 0 8 2
A getLinkFromOffer() 0 4 2
B getLowestShippingPriceFromOffer() 0 15 6
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