Passed
Push — master ( e4966f...264912 )
by Kazi Mainuddin
01:50
created

AbstractVendor   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 152
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A buildFeed() 0 17 1
A getEpisodes() 0 18 2
A getAudioUrl() 0 4 2
A getEpisodeSize() 0 4 2
A getPublishedDate() 0 7 1
A getValueByPath() 0 5 2
A getEpisodeDuration() 0 13 3
A getEpisodeImage() 0 6 2
A fetchCategories() 0 19 3
1
<?php
2
namespace Tzsk\ScrapePod\Vendors;
3
4
use DateTime;
5
use function GuzzleHttp\Psr7\str;
6
use SimpleXMLElement;
7
8
abstract class AbstractVendor
9
{
10
	/**
11
	 * @param SimpleXMLElement $feed
12
	 *
13
	 * @return array
14
	 */
15
	public function buildFeed(SimpleXMLElement $feed)
16
	{
17
		$output = [
18
			'title'          => (string) $feed->channel->title,
19
			'description'    => (string) $feed->channel->description,
20
			'summary'        => (string) $this->getValueByPath($feed->channel, "summary"),
21
			'image'          => (string) $feed->channel->image->url,
22
			'site'           => (string) $feed->channel->link,
23
			'author'         => (string) $this->getValueByPath($feed->channel, "author"),
24
			'language'       => (string) $feed->channel->language,
25
			'categories'     => $this->fetchCategories($feed->channel),
26
			'episode_count'  => (int) count($feed->channel->item),
27
			'episodes'       => $this->getEpisodes($feed->channel)
28
		];
29
30
		return $output;
31
	}
32
33
	/**
34
	 * @param SimpleXMLElement|mixed $channel
35
	 *
36
	 * @return array
37
	 */
38
	protected function getEpisodes($channel)
39
	{
40
		$items = [];
41
		foreach($channel->item as $value) {
42
			$items[] = [
43
				'title'        => (string) $value->title,
44
				'mp3'          => $this->getAudioUrl($value),
45
				'size'         => $this->getEpisodeSize($value),
46
				'duration'     => $this->getEpisodeDuration($value),
47
				'description'  => (string) $value->description,
48
				'link'         => (string) $value->link,
49
				'image'        => $this->getEpisodeImage($value, $channel),
50
				'published_at' => $this->getPublishedDate($value),
51
			];
52
		}
53
54
		return $items;
55
	}
56
57
	/**
58
	 * @param SimpleXMLElement $value
59
	 *
60
	 * @return null|string
61
	 */
62
	protected function getAudioUrl($value)
63
	{
64
		return isset($value->enclosure) ? (string) $value->enclosure->attributes()->url : null;
65
	}
66
67
	/**
68
	 * @param SimpleXMLElement $value
69
	 *
70
	 * @return int
71
	 */
72
	protected function getEpisodeSize($value)
73
	{
74
		return isset($value->enclosure) ? (int) $value->enclosure->attributes()->length : 0;
75
	}
76
77
	/**
78
	 * @param SimpleXMLElement $item
79
	 *
80
	 * @return string
81
	 */
82
	protected function getPublishedDate($item)
83
	{
84
		$published_at = new DateTime();
85
		$published_at->setTimestamp(strtotime($item->pubDate));
86
87
		return $published_at->format('Y-m-d H:i:s');
88
	}
89
90
	/**
91
	 * @param SimpleXMLElement|mixed $item
92
	 * @param string $path
93
	 *
94
	 * @return string
95
	 */
96
	protected function getValueByPath($item, $path)
97
	{
98
		return empty($item->xpath("itunes:{$path}")) ? null :
99
			$item->xpath("itunes:{$path}")[0];
100
	}
101
102
	/**
103
	 * @param SimpleXMLElement|mixed $item
104
	 *
105
	 * @return int
106
	 */
107
	protected function getEpisodeDuration($item)
108
	{
109
		$duration = (string) $this->getValueByPath($item, "duration");
110
111
		$durationArray = explode(":", $duration);
112
		if (count($durationArray) > 1) {
113
			sscanf($duration, "%d:%d:%d", $hours, $minutes, $seconds);
2 ignored issues
show
Bug introduced by
The variable $minutes does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $seconds does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
114
115
			$duration = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
116
		}
117
118
		return (int) $duration;
119
	}
120
121
	/**
122
	 * @param SimpleXMLElement $item
123
	 *
124
	 * @param SimpleXMLElement $channel
125
	 *
126
	 * @return string
127
	 */
128
	protected function getEpisodeImage($item, $channel)
129
	{
130
		$xmlImage = $this->getValueByPath($item, "image");
131
132
		return $xmlImage ? (string) $xmlImage->attributes()->href : (string) $channel->image->url;
0 ignored issues
show
Bug introduced by
The method attributes cannot be called on $xmlImage (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
133
	}
134
135
	/**
136
	 * @param SimpleXMLElement|mixed $channel
137
	 *
138
	 * @return array
139
	 */
140
	protected function fetchCategories($channel)
141
	{
142
		$categories = $channel->xpath('itunes:category');
143
144
		$items = [];
145
		foreach ( $categories as $category ) {
146
			$item = ['title' => (string) $category->attributes()->text, 'children' => []];
147
148
			if (! empty($category->xpath('itunes:category'))) {
149
				$inner = $this->fetchCategories($category);
150
151
				$item['children'] = $inner;
152
			}
153
154
			$items[] = $item;
155
		}
156
157
		return $items;
158
	}
159
}