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

DigitalPodcast   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 98
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLimit() 0 4 1
A setLimit() 0 5 1
A setDefaultQuery() 0 8 1
A generateUrl() 0 7 1
A build() 0 17 2
1
<?php
2
namespace Tzsk\ScrapePod\Vendors;
3
4
5
use SimpleXMLElement;
6
use Tzsk\ScrapePod\Contracts\VendorInterface;
7
8
class DigitalPodcast extends AbstractVendor implements VendorInterface
9
{
10
	/**
11
	 * @var string
12
	 */
13
	const APP_ID = "podcastsearchdemo";
14
15
	/**
16
	 * @var string
17
	 */
18
	const SEARCH_URL = "http://api.digitalpodcast.com/v2r/search";
19
20
	/**
21
	 * @var string
22
	 */
23
	const FORMAT = "rss";
24
25
	/**
26
	 * @var int
27
	 */
28
	private $limit = 15;
29
30
	/**
31
	 * @var string
32
	 */
33
	private $defaultQuery = null;
34
35
	/**
36
	 * DigitalPodcast constructor.
37
	 */
38
	public function __construct()
39
	{
40
		$this->setDefaultQuery();
41
	}
42
43
	/**
44
	 * @return int
45
	 */
46
	public function getLimit()
47
	{
48
		return $this->limit;
49
	}
50
51
	/**
52
	 * @param int $limit
53
	 */
54
	public function setLimit($limit)
55
	{
56
		// `-1` being used here because the API returns 3 items when `results=2`.
57
		$this->limit = ((int) $limit - 1);
58
	}
59
60
	/**
61
	 * @return void
62
	 */
63
	public function setDefaultQuery()
64
	{
65
		$this->defaultQuery = http_build_query([
66
			'results' => $this->limit,
67
			'appid'   => self::APP_ID,
68
			'format'  => self::FORMAT
69
		]);
70
	}
71
72
	/**
73
	 * @param  string $value
74
	 * @return string
75
	 */
76
	public function generateUrl($value)
77
	{
78
		$value = urlencode($value);
79
		$url   = self::SEARCH_URL . "?keywords={$value}";
80
81
		return $url . '&' . $this->defaultQuery;
82
	}
83
84
	/**
85
	 * @param  array $response
86
	 * @return array
87
	 */
88
	public function build(array $response)
89
	{
90
		$xml = new SimpleXMLElement($response['search']);
91
		$xml = $xml->channel;
1 ignored issue
show
Bug introduced by
The property channel does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
92
93
		$output['result_count'] = count($xml->item);
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...
94
95
		foreach($xml->item as $value) {
96
			$output['results'][] = [
97
				'title' => (string) $value->title,
98
				'rss'   => (string) $value->source,
99
				'link'  => (string) $value->link,
100
			];
101
		}
102
103
		return $output;
104
	}
105
}