AmazonPriceClient   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 98.68%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 2
cbo 5
dl 0
loc 120
ccs 75
cts 76
cp 0.9868
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B addPricesToCollection() 0 21 8
B populatePriceData() 0 24 4
A addParam() 0 5 1
A setConfiguration() 0 9 1
B getPricesForIsbns() 0 34 4
A send() 0 5 1
1
<?php namespace Packback\Prices\Clients;
2
3
use Packback\Prices\PriceClient;
4
use ApaiIO\Configuration\GenericConfiguration;
5
use ApaiIO\Operations\Search;
6
use ApaiIO\ApaiIO;
7
8
class AmazonPriceClient extends PriceClient
9
{
10
    const RETAILER = 'amazon';
11
12
    public $conf;
13
    public $container;
14
    public $search;
15
16 16
    public function __construct($config = [])
17
    {
18 16
        $this->conf = new GenericConfiguration();
19 16
        $this->setConfiguration($config);
20 16
        $this->container = new ApaiIO($this->conf);
21 16
        $this->search = new Search();
22 16
    }
23
24 16
    public function setConfiguration($config = [])
25
    {
26 16
        $this->conf
27 16
            ->setCountry('com')
28 16
            ->setAccessKey($config['access_key'])
29 16
            ->setSecretKey($config['secret_key'])
30 16
            ->setAssociateTag($config['associate_tag']);
31 16
        $this->conf->setResponseTransformer('\ApaiIO\ResponseTransformer\XmlToSimpleXmlObject');
32 16
    }
33
34 6
    public function getPricesForIsbns($isbns = [])
35
    {
36 6
        $isbn_groups = [];
37
38 6
        if (count($isbns) > 10) {
39 2
            $isbn_groups = array_chunk($isbns, 10);
40 2
        } else {
41 4
            array_push($isbn_groups, $isbns);
42
        }
43 6
        foreach ($isbn_groups as $isbn_group) {
44 6
            $response = $this->addParam('Condition', 'New')
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45 6
                ->addParam('IdType', 'ISBN')
46 6
                ->addParam('ItemId', $isbn_group)
47 6
                ->addParam('Condition', 'All')
48 6
                ->addParam('SearchIndex', 'Books')
49 6
                ->addParam('Operation', 'ItemLookup')
50 6
                ->addParam('Version', '2011-08-01')
51 6
                ->addParam('ResponseGroup', ['Large'])
52 6
                ->addParam('Service', 'AWSECommerceService');
53
54
            // Get response for Marketplace books
55 6
            $response = $this->send();
56
            // If there is an error with the API key, throw an exception
57 6
            if (isset($response->Error)) {
58
                throw new \Exception($response->Error->Code.': '.$response->Error->Message);
59
            }
60 6
            $this->addPricesToCollection($response);
61
            // Get response for only Amazon books
62 6
            $response = $this->addParam('MerchantId', 'Amazon')->send();
63 6
            $this->addPricesToCollection($response);
64 6
        }
65
66 6
        return $this->collection;
67
    }
68
69 6
    public function addPricesToCollection($response)
70
    {
71 6
        if (isset($response->Items->Request->IsValid) && $response->Items->Request->IsValid) {
72 6
            if (isset($response->Items->Item)) {
73 6
                $items = $response->Items->Item;
74 6
                foreach ($items as $key => $item) {
75
                    // Create price objects for each offer
76 6
                    if (isset($item->Offers->Offer)) {
77 6
                        if (is_array($item->Offers->Offer)) {
78 2
                            foreach ($item->Offers->Offer as $offer) {
79 2
                                $this->collection[] = $this->populatePriceData($offer, $item, self::RETAILER);
80 2
                            }
81 2
                        } else {
82 4
                            $this->collection[] = $this->populatePriceData($item->Offers->Offer, $item, self::RETAILER);
83
                        }
84 6
                    }
85 6
                }
86 6
            }
87 6
        }
88 6
        return $this->collection;
89
    }
90
91 10
    public function populatePriceData($offer, $item, $merchant_id)
92
    {
93
        // New price model object
94 8
        $price = $this->createNewPrice();
95
        // Set retailer
96 8
        $price->retailer = $merchant_id;
97
        // Populate ISBN 13
98 8
        if (isset($item->ItemAttributes->EAN)) {
99 6
            $price->isbn13 = $item->ItemAttributes->EAN;
100 8
        } elseif (isset($item->ItemAttributes->EISBN)) {
101 2
            $price->isbn13 = $item->ItemAttributes->EISBN;
102 2
        }
103
        // Populate URL
104 10
        if (isset($item->DetailPageURL)) {
105 8
            $price->url = $item->DetailPageURL;
106 8
        }
107
        // Populate condition
108 8
        $price->condition = parent::getConditionFromString($offer->OfferAttributes->Condition);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getConditionFromString() instead of populatePriceData()). Are you sure this is correct? If so, you might want to change this to $this->getConditionFromString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
109
        // Populate term
110 8
        $price->term = parent::TERM_PERPETUAL;
111
        // Populate Price
112 8
        $price->price = ((float)$offer->OfferListing->Price->Amount)/100;
0 ignored issues
show
Documentation Bug introduced by
The property $price was declared of type string, but (double) $offer->OfferLi...ng->Price->Amount / 100 is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
113 8
        return $price;
114
    }
115
116 6
    public function addParam($key, $value)
117
    {
118 6
        $this->search->{'set'.$key}($value);
119 6
        return $this;
120
    }
121
122 10
    public function send()
123
    {
124 10
        $response = $this->container->runOperation($this->search);
125 8
        return json_decode(json_encode($response));
126
    }
127
}
128