AbeBooksPriceClient::getPricesForIsbns()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php namespace Packback\Prices\Clients;
2
3
use Packback\Prices\PriceClient;
4
5
class AbeBooksPriceClient extends PriceClient
6
{
7
    const RETAILER = 'abebooks';
8
9 10
    public function __construct($config = [])
10
    {
11 10
        parent::__construct();
12 10
        $this->baseUrl = 'http://search2.abebooks.com/search';
13 10
        $this->query['clientkey'] = $config['access_key'];
14 10
    }
15
16 2
    public function getPricesForIsbns($isbns = [])
17
    {
18 2
        foreach ($isbns as $isbn) {
19 2
            $response = $this->addParam('isbn', $isbn)->send();
20 2
            $this->addPricesToCollection($response);
21 2
        }
22
23 2
        return $this->collection;
24
    }
25
26 10
    public function addPricesToCollection($response)
27
    {
28
        // Check response, populate price object, send collection back
29 10
        if (isset($response['Book'])) {
30 8
            foreach ($response['Book'] as $offer) {
31 8
                $offer = (object) $offer;
32 8
                $price = $this->createNewPrice();
33 8
                if (isset($offer->listingPrice)) {
34 8
                    if (isset($offer->listingCondition) && strtolower($offer->listingCondition) == 'new book') {
35 2
                        $price->condition = parent::CONDITION_NEW;
36 8
                    } elseif (isset($offer->itemCondition)) {
37 4
                        $price->condition = $this->getConditionFromString($offer->itemCondition);
38 4
                    }
39 8
                    if (empty($price->condition)) {
40 2
                        $price->condition = parent::CONDITION_GOOD;
41 2
                    }
42 8
                    $price->isbn13 = $offer->isbn13;
43 8
                    $price->price = $offer->listingPrice;
44 8
                    $price->shipping_price = $offer->firstBookShipCost;
45 8
                    $price->url = 'http://affiliates.abebooks.com/c/74871/77797/2029?u='.urlencode($offer->listingUrl);
46 8
                    $price->retailer = self::RETAILER;
47 8
                    $price->term = parent::TERM_PERPETUAL;
48 8
                }
49 8
                $this->addPriceToCollection($price);
50 8
            }
51 8
        }
52 10
        return $this;
53
    }
54
}
55