CommissionJunctionPriceClient::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 CROSCON\CommissionJunction\Client;
4
use Packback\Prices\PriceClient;
5
6
class CommissionJunctionPriceClient extends PriceClient
7
{
8 20
    public function __construct($config = [])
9
    {
10 20
        $this->client = new Client($config['key']);
11 20
        $this->query['website-id'] = $config['website'];
12 20
        $this->query['advertiser-ids'] = $config['cj_advertiser_id'];
13 20
    }
14
15 4
    public function getPricesForIsbns($isbns = [])
16
    {
17 4
        foreach ($isbns as $isbn) {
18 4
            $response = $this->addParam('isbn', $isbn)->send();
19 4
            $this->addPricesToCollection($response);
20 4
        }
21
22 4
        return $this->collection;
23
    }
24
25 16
    public function addPricesToCollection($response)
26
    {
27 16
        if ($this->responseHasProducts($response)) {
28 12
            if (is_array($response->products->product)) {
29 2
                foreach ($response->products->product as $product) {
30 2
                    $this->collection[] = $this->makePriceFromProduct($product);
31 2
                }
32 2
            } else {
33 10
                $this->collection[] = $this->makePriceFromProduct($response->products->product);
34
            }
35 12
        }
36 16
        return $this->collection;
37
    }
38
39 12
    public function makePriceFromProduct($product)
40
    {
41 12
        $price = $this->createNewPrice();
42 12
        $price->isbn13 = $product->isbn;
43 12
        $price->price = $product->price;
44 12
        $price->url = $product->{'buy-url'};
45 12
        $price->shipping_price = null;
46 12
        return $price;
47
    }
48
49 20
    public function responseHasProducts($response)
50
    {
51 20
        if (is_object($response) && isset($response->products)
52 20
            && isset($response->products->{'@attributes'})
53 20
            && isset($response->products->{'@attributes'}->{'records-returned'})
54 20
            && $response->products->{'@attributes'}->{'records-returned'} > 0) {
55 14
                return true;
56
        }
57 6
        return false;
58
    }
59
60 10
    public function send()
61
    {
62
        try {
63 10
            return json_decode(json_encode($this->client->productSearch($this->query)), false);
0 ignored issues
show
Bug introduced by
The method productSearch does only exist in CROSCON\CommissionJunction\Client, but not in GuzzleHttp\Client.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
64 2
        } catch (\Exception $e) {
65
            // Return error messaging
66 2
            return $e->getMessage();
67
        }
68
    }
69
}
70