Passed
Branch master (2530fd)
by Kazi Mainuddin
03:16
created

Itunes::getLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Tzsk\ScrapePod\Vendors;
3
4
5
use Tzsk\ScrapePod\Contracts\VendorInterface;
6
7
class Itunes extends AbstractVendor implements VendorInterface
8
{
9
	/**
10
	 * @var string
11
	 */
12
	const SEARCH_URL = "https://itunes.apple.com/search";
13
14
	/**
15
	 * @var string
16
	 */
17
	const LOOKUP_URL = "https://itunes.apple.com/lookup";
18
19
	/**
20
	 * @var string
21
	 */
22
	const ENTITY = "podcast";
23
24
	/**
25
	 * @var string
26
	 */
27
	const MEDIA = "podcast";
28
29
	/**
30
	 * @var int
31
	 */
32
	private $limit = 15;
33
34
	/**
35
	 * @var string
36
	 */
37
	private $defaultQuery = null;
38
39
	/**
40
	 * Itunes constructor.
41
	 */
42
	public function __construct()
43
	{
44
		$this->setDefaultQuery();
45
	}
46
47
	/**
48
	 * @return int
49
	 */
50
	public function getLimit()
51
	{
52
		return $this->limit;
53
	}
54
55
	/**
56
	 * @param int $limit
57
	 */
58
	public function setLimit($limit)
59
	{
60
		$this->limit = (int) $limit;
61
	}
62
63
	/**
64
	 * @return void
65
	 */
66
	public function setDefaultQuery()
67
	{
68
		$this->defaultQuery = http_build_query([
69
			'limit'     => $this->limit,
70
			'entity'    => self::ENTITY,
71
			'media'     => self::MEDIA
72
		]);
73
	}
74
75
	/**
76
	 * @param  string $value
77
	 * @return string
78
	 */
79
	public function generateUrl($value)
80
	{
81
		$value = is_string($value) ? urlencode($value) : $value;
82
		$url   = is_int($value) ? self::LOOKUP_URL . "?id={$value}" : self::SEARCH_URL . "?term={$value}";
83
84
		return $url . '&' . $this->defaultQuery;
85
	}
86
87
	/**
88
	 * @param  array  $response
89
	 * @return array
90
	 */
91
	public function build(array $response)
92
	{
93
		$response = json_decode($response['search']);
94
		$output['result_count'] = $response->resultCount;
1 ignored issue
show
Coding Style Comprehensibility introduced by
$output was never initialized. Although not strictly required by PHP, it is generally a good practice to add $output = 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...
95
96
		foreach($response->results as $value) {
97
			$output['results'][] = [
98
				'itunes_id' => $value->collectionId,
99
				'author'    => $value->artistName,
100
				'title'     => $value->collectionName,
101
				'episodes'  => $value->trackCount,
102
				'image'     => $value->artworkUrl100,
103
				'rss'       => $value->feedUrl,
104
				'itunes'    => $value->collectionViewUrl,
105
				'genre'     => $value->primaryGenreName,
106
			];
107
		}
108
109
		return $output;
110
	}
111
}