Passed
Push — master ( 401ab6...755c0f )
by Kazi Mainuddin
01:43
created

PodcastScraper::failedResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
namespace Tzsk\ScrapePod;
3
4
use Exception;
5
use SimpleXMLElement;
6
use Tzsk\ScrapePod\Contracts\VendorInterface;
7
use Tzsk\ScrapePod\Helpers\Request;
8
use Tzsk\ScrapePod\Helpers\Xml;
9
10
class PodcastScraper
11
{
12
	/**
13
	 * @var VendorInterface
14
	 */
15
	private $vendor;
16
17
	/**
18
	 * @param VendorInterface $vendor
19
	 */
20
	public function __construct(VendorInterface $vendor)
21
	{
22
		$this->vendor = $vendor;
23
	}
24
25
	/**
26
	 * @param  string $value
27
	 * @return array
28
	 */
29 View Code Duplication
	public function get($value)
1 ignored issue
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...
30
	{
31
		if (strlen($value) < 1) {
32
			return $this->failedResponse("Search keyword cannot be empty");
33
		}
34
35
		try {
36
			$response = $this->search(new Request, $value);
37
			return [
38
				'status'      => true,
39
				'data'        => $this->vendor->build($response)
40
			];
41
		} catch (Exception $except) {
42
			return $this->failedResponse($except->getMessage());
43
		}
44
	}
45
46
	/**
47
	 * @param  Request $request
48
	 * @param  string  $value
49
	 * @return array
50
	 * @throws Exception
51
	 */
52 View Code Duplication
	private function search(Request $request, $value)
1 ignored issue
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...
53
	{
54
		$response = $request->create($this->vendor->generateUrl($value));
55
56
		if (is_null($response)) {
57
			throw new Exception("Request to Itunes API failed", $request->getStatusCode());
58
		}
59
60
		return [
61
			'search'      => $response,
62
			'status'      => true,
63
		];
64
	}
65
66
	/**
67
	 * @param  string $feedUrl
68
	 * @return array
69
	 */
70 View Code Duplication
	public function find($feedUrl)
1 ignored issue
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...
71
	{
72
		if (strlen($feedUrl) < 1) {
73
			return $this->failedResponse("Feed Url cannot be empty");
74
		}
75
76
		try {
77
			return [
78
				'status'      => true,
79
				'data'        => $this->vendor->buildFeed($this->getFeedFromUrl($feedUrl))
80
			];
81
		} catch (Exception $except) {
82
			return $this->failedResponse($except->getMessage());
83
		}
84
	}
85
86
	/**
87
	 * @param  int $limit
88
	 * @return PodcastScraper
89
	 */
90
	public function limit($limit)
91
	{
92
		$this->vendor->setLimit($limit);
93
		$this->vendor->setDefaultQuery();
94
95
		return $this;
96
	}
97
98
	/**
99
	 * @param  Request $request
100
	 * @param  string  $feedUrl
101
	 * @return array
102
	 * @throws Exception
103
	 */
104 View Code Duplication
	private function read(Request $request, $feedUrl)
1 ignored issue
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...
105
	{
106
		$output = $request->create($feedUrl);
107
108
		if (is_null($output)) {
109
			throw new Exception("Request to RSS failed", $request->getStatusCode());
110
		}
111
112
		return [
113
			'feed'        => $output,
114
			'status'      => true,
115
		];
116
	}
117
118
	/**
119
	 * @param string $feedUrl
120
	 *
121
	 * @return mixed
122
	 */
123
	protected function getFeedFromUrl($feedUrl)
124
	{
125
		$response = $this->read(new Request, $feedUrl);
126
127
		libxml_use_internal_errors( true );
128
129
		try {
130
			$feed = new SimpleXMLElement($response['feed'], LIBXML_NOCDATA, false);
131
		} catch ( Exception $except ) {
132
			$feed = new SimpleXMLElement(Xml::repair($response['feed']), LIBXML_NOCDATA, false );
133
		}
134
135
		return $feed;
136
	}
137
138
	/**
139
	 * @param string $message
140
	 *
141
	 * @return array
142
	 */
143
	protected function failedResponse($message)
144
	{
145
		return [
146
			'status'      => false,
147
			'message'     => $message
148
		];
149
	}
150
}