CommissionJunctionPriceClient   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 64
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getPricesForIsbns() 0 9 2
A addPricesToCollection() 0 13 4
A makePriceFromProduct() 0 9 1
B responseHasProducts() 0 10 6
A send() 0 9 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