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

AbstractVendor   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 130
c 0
b 0
f 0
wmc 17
lcom 1
cbo 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A buildFeed() 0 17 1
B getEpisodes() 0 18 5
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'          => isset($value->enclosure) ? (string) $value->enclosure->attributes()->url : null,
45
				'size'         => isset($value->enclosure) ? (int) $value->enclosure->attributes()->length : 0,
46
				'duration'     => $this->getEpisodeDuration($value),
47
				'description'  => (string) $value->description,
48
				'link'         => (string) $value->link,
49
				'image'        => $this->getEpisodeImage($value) ? : (string) $channel->image->url,
50
				'published_at' => $this->getPublishedDate($value),
51
			];
52
		}
53
54
		return $items;
55
	}
56
57
	/**
58
	 * @param SimpleXMLElement $item
59
	 *
60
	 * @return string
61
	 */
62
	protected function getPublishedDate($item)
63
	{
64
		$published_at = new DateTime();
65
		$published_at->setTimestamp(strtotime($item->pubDate));
66
67
		return $published_at->format('Y-m-d H:i:s');
68
	}
69
70
	/**
71
	 * @param SimpleXMLElement|mixed $item
72
	 * @param $path string
73
	 *
74
	 * @return string
75
	 */
76
	protected function getValueByPath($item, $path)
77
	{
78
		return empty($item->xpath("itunes:{$path}")) ? null :
79
			$item->xpath("itunes:{$path}")[0];
80
	}
81
82
	/**
83
	 * @param SimpleXMLElement|mixed $item
84
	 *
85
	 * @return int
86
	 */
87
	protected function getEpisodeDuration($item)
88
	{
89
		$duration = (string) $this->getValueByPath($item, "duration");
90
91
		$durationArray = explode(":", $duration);
92
		if (count($durationArray) > 1) {
93
			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...
94
95
			$duration = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
96
		}
97
98
		return (int) $duration;
99
	}
100
101
	/**
102
	 * @param SimpleXMLElement $item
103
	 *
104
	 * @return string
105
	 */
106
	protected function getEpisodeImage($item)
107
	{
108
		$xmlImage = $this->getValueByPath($item, "image");
109
110
		return $xmlImage ? (string) $xmlImage->attributes()->href : null;
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...
111
	}
112
113
	/**
114
	 * @param SimpleXMLElement|mixed $channel
115
	 *
116
	 * @return array
117
	 */
118
	protected function fetchCategories($channel)
119
	{
120
		$categories = $channel->xpath('itunes:category');
121
122
		$items = [];
123
		foreach ( $categories as $category ) {
124
			$item = ['title' => (string) $category->attributes()->text, 'children' => []];
125
126
			if (! empty($category->xpath('itunes:category'))) {
127
				$inner = $this->fetchCategories($category);
128
129
				$item['children'] = $inner;
130
			}
131
132
			$items[] = $item;
133
		}
134
135
		return $items;
136
	}
137
}