CheggPriceClient::getPricesForIsbns()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php namespace Packback\Prices\Clients;
2
3
use Packback\Prices\PriceClient;
4
5
class CheggPriceClient extends PriceClient
6
{
7
    const RETAILER = 'chegg';
8
9 14
    public function __construct($config = [])
10
    {
11 14
        parent::__construct();
12 14
        $this->baseUrl = 'http://api.chegg.com/rent.svc';
13 14
        $this->query['KEY'] = $config['access_key'];
14 14
        $this->query['PW'] = $config['secret_key'];
15 14
        $this->query['R'] = 'XML';
16 14
        $this->query['V'] = '2.0';
17 14
        $this->query['with_pids'] = '1';
18 14
    }
19
20 2
    public function getPricesForIsbns($isbns = [])
21
    {
22 2
        foreach ($isbns as $isbn) {
23 2
            if ($isbn) {
24 2
                $this->query['isbn'] = $isbn;
25 2
                $response = $this->send();
26 2
                $this->addPricesToCollection($response);
27 2
            }
28 2
        }
29 2
        return $this->collection;
30
    }
31
32 14
    public function addPricesToCollection($payload)
33
    {
34 14
        if ($this->isValidCheggResponse($payload)) {
35 12
            foreach ($payload['Items']['Item']['Terms']['Term'] as $term) {
36 12
                $price = $this->createNewPrice();
37 12
                $price->isbn13 = $payload['Items']['Item']['EAN'];
38 12
                $price->retailer = self::RETAILER;
39 12
                $price->price = $term['Price'];
40 12
                $price->shipping_price = $this->setCheggShippingPrice($payload['Items']['Item']);
41 12
                $base_url = "http://chggtrx.com/click.track?CID=267582&AFID=304350&ADID=1088031&SID=&isbn_ean=";
42 12
                $price->url = $base_url . $price->isbn13;
43 12
                $price->condition = parent::CONDITION_GOOD;
44 12
                if (isset($term)) {
45 12
                    $price->term = $this->getTermFromString($term['Term']);
46 12
                }
47 12
                if ($term['Term'] != 'SUMMER') {
48 12
                    $this->collection[] = $price;
49 12
                }
50 12
            }
51 12
        }
52 14
        return $this->collection;
53
    }
54
55 12
    public function setCheggShippingPrice($item)
56
    {
57 12
        if (isset($item['ShippingPrices'])
58 12
            && isset($item['ShippingPrices']['ShippingPrice'])) {
59 4
            foreach ($item['ShippingPrices']['ShippingPrice'] as $shipping_price) {
60 4
                $prices[] = $shipping_price['Cost_first'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$prices was never initialized. Although not strictly required by PHP, it is generally a good practice to add $prices = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
61 4
            }
62 4
            return min($prices);
0 ignored issues
show
Bug introduced by
The variable $prices does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
63
        }
64 8
        return null;
65
    }
66
67 14
    public function isValidCheggResponse($payload)
68
    {
69 14
        if (is_array($payload)
70 14
            && isset($payload['Items'])
71 14
            && isset($payload['Items']['Item'])
72 14
            && isset($payload['Items']['Item']['Terms'])
73 14
            && isset($payload['Items']['Item']['Terms']['Term'])) {
74 12
            return true;
75
        }
76 2
        return false;
77
    }
78
}
79