Passed
Push — master ( a8fede...8b7367 )
by Cyril
02:06
created

ObjectToPreview::getCategories()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 13
cp 0
rs 8.8571
cc 6
eloc 7
nc 6
nop 1
crap 42
1
<?php
2
namespace ApaiIO\ResponseTransformer;
3
4
class ObjectToPreview extends ObjectToArray implements ResponseTransformerInterface
5
{
6
    /**
7
     *
8
     * @var array
9
     */
10
    protected $data = [];
11
12
    /**
13
     *
14
     * @var array
15
     */
16
    protected $items = [];
17
18
    /**
19
     *
20
     * @var array
21
     */
22
    protected $categories = [];
23
24
    /**
25
     *
26
     * @param string $response
27
     * @return array
28
     */
29
    public function transform($response)
30
    {
31
        $response = parent::transform($response);
32
33
        if (!$this->getItems($response)) {
34
            return [];
35
        }
36
37
        $this->getCategories($response);
38
39
        $c = count($this->items);
40
        for ($i = 0; $i < $c; $i++) {
41
            $this->set($i, 'asin', 'ASIN');
42
            $this->set($i, 'category', 'BrowseNodes', 'BrowseNode', 0, 'Name');
43
            $this->set($i, 'sales_rank', 'SalesRank');
44
            $this->set($i, 'title', 'ItemAttributes', 'Title');
45
            $this->set($i, 'url', 'DetailPageURL');
46
            $this->set($i, 'manufacturer', 'ItemAttributes', 'Manufacturer');
47
            $this->set($i, 'large_image', 'LargeImage', 'URL');
48
            $this->set($i, 'medium_image', 'MediumImage', 'URL');
49
            $this->set($i, 'small_image', 'SmallImage', 'URL');
50
        }
51
52
        return [
53
            'items' => $this->data,
54
            'categories' => $this->categories
55
        ];
56
    }
57
58
    /**
59
     *
60
     * @param type $response
61
     * @return mixed
62
     */
63 View Code Duplication
    protected function getItems($response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        if (isset($response['Items']['Item']) AND is_array($response['Items']['Item'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
66
            return $this->items = $response['Items']['Item'];
67
        } else {
68
            return false;
69
        }
70
    }
71
72
    /**
73
     *
74
     * @param array $response
75
     */
76
    protected function getCategories($response)
77
    {
78
        if (isset($response['Items']['SearchBinSets']['SearchBinSet']['Bin'])) {
79
            foreach ($response['Items']['SearchBinSets']['SearchBinSet']['Bin'] as $bin) {
80
                if (isset($bin['BinParameter']['Name']) && $bin['BinParameter']['Name'] == 'SearchIndex') {
81
                    $this->categories[] = $bin['BinParameter']['Value'];
82
                }
83
                if (count($this->categories) >= 10) {
84
                    return;
85
                }
86
            }
87
        }
88
    }
89
90
    /**
91
     *
92
     * @param int $i
93
     * @param string $data
94
     * @param string $key1
95
     * @param string $key2
96
     * @param string $key3
97
     * @param string $key4
98
     */
99
    protected function set($i, $data, $key1, $key2 = null, $key3 = null, $key4 = null)
100
    {
101
        if ($key4 !== null) {
102 View Code Duplication
            if (isset($this->items[$i][$key1][$key2][$key3][$key4])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
                $this->data[$i][$data] = $this->items[$i][$key1][$key2][$key3][$key4];
104
            }
105 View Code Duplication
        } elseif ($key3 !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            if (isset($this->items[$i][$key1][$key2][$key3])) {
107
                $this->data[$i][$data] = $this->items[$i][$key1][$key2][$key3];
108
            }
109
        } elseif ($key2 !== null) {
110
            if (isset($this->items[$i][$key1][$key2])) {
111
                $this->data[$i][$data] = $this->items[$i][$key1][$key2];
112
            }
113
        } else {
114
            if (isset($this->items[$i][$key1])) {
115
                $this->data[$i][$data] = $this->items[$i][$key1];
116
            }
117
        }
118
    }
119
}
120