Passed
Push — master ( 46dc7a...7d8ee2 )
by Kazi Mainuddin
02:02
created
src/Contracts/VendorInterface.php 1 patch
Indentation   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -5,46 +5,46 @@
 block discarded – undo
5 5
 
6 6
 interface VendorInterface
7 7
 {
8
-    /**
9
-     * @param string $value
10
-     *
11
-     * @return string
12
-     */
13
-    public function generateUrl($value);
14
-
15
-    /**
16
-     * @param array $response
17
-     *
18
-     * @return array
19
-     */
20
-    public function build(array $response);
21
-
22
-    /**
23
-     * @return void
24
-     */
25
-    public function setDefaultQuery();
26
-
27
-    /**
28
-     * @return int
29
-     */
30
-    public function getLimit();
31
-
32
-    /**
33
-     * @param int $limit
34
-     *
35
-     * @return void
36
-     */
37
-    public function setLimit($limit);
38
-
39
-    /**
40
-     * @param SimpleXMLElement $feed
41
-     *
42
-     * @return array
43
-     */
44
-    public function buildFeed(SimpleXMLElement $feed);
45
-
46
-    /**
47
-    * @return void
48
-    */
49
-    public function original();
8
+	/**
9
+	 * @param string $value
10
+	 *
11
+	 * @return string
12
+	 */
13
+	public function generateUrl($value);
14
+
15
+	/**
16
+	 * @param array $response
17
+	 *
18
+	 * @return array
19
+	 */
20
+	public function build(array $response);
21
+
22
+	/**
23
+	 * @return void
24
+	 */
25
+	public function setDefaultQuery();
26
+
27
+	/**
28
+	 * @return int
29
+	 */
30
+	public function getLimit();
31
+
32
+	/**
33
+	 * @param int $limit
34
+	 *
35
+	 * @return void
36
+	 */
37
+	public function setLimit($limit);
38
+
39
+	/**
40
+	 * @param SimpleXMLElement $feed
41
+	 *
42
+	 * @return array
43
+	 */
44
+	public function buildFeed(SimpleXMLElement $feed);
45
+
46
+	/**
47
+	 * @return void
48
+	 */
49
+	public function original();
50 50
 }
Please login to merge, or discard this patch.
src/ScrapePodcast.php 1 patch
Indentation   +104 added lines, -104 removed lines patch added patch discarded remove patch
@@ -7,108 +7,108 @@
 block discarded – undo
7 7
 
8 8
 class ScrapePodcast
9 9
 {
10
-    /**
11
-     * @var VendorInterface
12
-     */
13
-    protected $vendor;
14
-
15
-    /**
16
-     * @var int
17
-     */
18
-    protected $count = 15;
19
-
20
-    /**
21
-    * @var bool
22
-    */
23
-    protected $isOrginal = false;
24
-
25
-    /**
26
-     * ScrapePodcast constructor.
27
-     */
28
-    public function __construct()
29
-    {
30
-        $this->vendor = new Itunes();
31
-    }
32
-
33
-    /**
34
-     * @return ScrapePodcast
35
-     */
36
-    public function itunes()
37
-    {
38
-        $this->vendor = new Itunes();
39
-
40
-        return $this;
41
-    }
42
-
43
-    /**
44
-     * @return ScrapePodcast
45
-     */
46
-    public function digitalPodcast()
47
-    {
48
-        $this->vendor = new DigitalPodcast();
49
-
50
-        return $this;
51
-    }
52
-
53
-    /**
54
-     * @param int $count
55
-     *
56
-     * @return ScrapePodcast
57
-     */
58
-    public function limit($count)
59
-    {
60
-        $this->count = $count;
61
-
62
-        return $this;
63
-    }
64
-
65
-    /**
66
-     * @param string $term
67
-     *
68
-     * @return array
69
-     */
70
-    public function search($term)
71
-    {
72
-        return $this->engine()->get($term);
73
-    }
74
-
75
-    /**
76
-     * @param $id
77
-     *
78
-     * @return array
79
-     */
80
-    public function find($id)
81
-    {
82
-        return $this->engine()->get((int) $id);
83
-    }
84
-
85
-    /**
86
-     * @param string $feed
87
-     *
88
-     * @return array
89
-     */
90
-    public function feed($feed)
91
-    {
92
-        return $this->engine()->find($feed);
93
-    }
94
-
95
-    /**
96
-     * @return ScrapePodcast
97
-     */
98
-    public function original()
99
-    {
100
-        $this->isOrginal = true;
101
-
102
-        return $this;
103
-    }
104
-
105
-    /**
106
-     * @return PodcastScraper
107
-     */
108
-    protected function engine()
109
-    {
110
-        $engine = (new PodcastScraper($this->vendor))->limit($this->count);
111
-
112
-        return $this->isOrginal ? $engine->original() : $engine;
113
-    }
10
+	/**
11
+	 * @var VendorInterface
12
+	 */
13
+	protected $vendor;
14
+
15
+	/**
16
+	 * @var int
17
+	 */
18
+	protected $count = 15;
19
+
20
+	/**
21
+	 * @var bool
22
+	 */
23
+	protected $isOrginal = false;
24
+
25
+	/**
26
+	 * ScrapePodcast constructor.
27
+	 */
28
+	public function __construct()
29
+	{
30
+		$this->vendor = new Itunes();
31
+	}
32
+
33
+	/**
34
+	 * @return ScrapePodcast
35
+	 */
36
+	public function itunes()
37
+	{
38
+		$this->vendor = new Itunes();
39
+
40
+		return $this;
41
+	}
42
+
43
+	/**
44
+	 * @return ScrapePodcast
45
+	 */
46
+	public function digitalPodcast()
47
+	{
48
+		$this->vendor = new DigitalPodcast();
49
+
50
+		return $this;
51
+	}
52
+
53
+	/**
54
+	 * @param int $count
55
+	 *
56
+	 * @return ScrapePodcast
57
+	 */
58
+	public function limit($count)
59
+	{
60
+		$this->count = $count;
61
+
62
+		return $this;
63
+	}
64
+
65
+	/**
66
+	 * @param string $term
67
+	 *
68
+	 * @return array
69
+	 */
70
+	public function search($term)
71
+	{
72
+		return $this->engine()->get($term);
73
+	}
74
+
75
+	/**
76
+	 * @param $id
77
+	 *
78
+	 * @return array
79
+	 */
80
+	public function find($id)
81
+	{
82
+		return $this->engine()->get((int) $id);
83
+	}
84
+
85
+	/**
86
+	 * @param string $feed
87
+	 *
88
+	 * @return array
89
+	 */
90
+	public function feed($feed)
91
+	{
92
+		return $this->engine()->find($feed);
93
+	}
94
+
95
+	/**
96
+	 * @return ScrapePodcast
97
+	 */
98
+	public function original()
99
+	{
100
+		$this->isOrginal = true;
101
+
102
+		return $this;
103
+	}
104
+
105
+	/**
106
+	 * @return PodcastScraper
107
+	 */
108
+	protected function engine()
109
+	{
110
+		$engine = (new PodcastScraper($this->vendor))->limit($this->count);
111
+
112
+		return $this->isOrginal ? $engine->original() : $engine;
113
+	}
114 114
 }
Please login to merge, or discard this patch.
src/Vendors/DigitalPodcast.php 1 patch
Indentation   +103 added lines, -103 removed lines patch added patch discarded remove patch
@@ -6,107 +6,107 @@
 block discarded – undo
6 6
 
7 7
 class DigitalPodcast extends AbstractVendor implements VendorInterface
8 8
 {
9
-    /**
10
-     * @var string
11
-     */
12
-    const APP_ID = "podcastsearchdemo";
13
-
14
-    /**
15
-     * @var string
16
-     */
17
-    const SEARCH_URL = "http://api.digitalpodcast.com/v2r/search";
18
-
19
-    /**
20
-     * @var string
21
-     */
22
-    const FORMAT = "rss";
23
-
24
-    /**
25
-     * @var int
26
-     */
27
-    private $limit = 15;
28
-
29
-    /**
30
-     * @var string
31
-     */
32
-    private $defaultQuery = null;
33
-
34
-    /**
35
-     * DigitalPodcast constructor.
36
-     */
37
-    public function __construct()
38
-    {
39
-        $this->setDefaultQuery();
40
-    }
41
-
42
-    /**
43
-     * @return int
44
-     */
45
-    public function getLimit()
46
-    {
47
-        return $this->limit;
48
-    }
49
-
50
-    /**
51
-     * @param int $limit
52
-     */
53
-    public function setLimit($limit)
54
-    {
55
-        // `-1` being used here because the API returns 3 items when `results=2`.
56
-        $this->limit = ((int) $limit - 1);
57
-    }
58
-
59
-    /**
60
-     * @return void
61
-     */
62
-    public function setDefaultQuery()
63
-    {
64
-        $this->defaultQuery = http_build_query([
65
-            'results' => $this->limit,
66
-            'appid'   => self::APP_ID,
67
-            'format'  => self::FORMAT
68
-        ]);
69
-    }
70
-
71
-    /**
72
-     * @param  string $value
73
-     * @return string
74
-     */
75
-    public function generateUrl($value)
76
-    {
77
-        $value = urlencode($value);
78
-        $url   = self::SEARCH_URL . "?keywords={$value}";
79
-
80
-        return $url . '&' . $this->defaultQuery;
81
-    }
82
-
83
-    /**
84
-     * @param  array $response
85
-     * @return array
86
-     */
87
-    public function build(array $response)
88
-    {
89
-        $xml = new SimpleXMLElement($response['search']);
90
-        $xml = $xml->channel;
91
-
92
-        $output['result_count'] = count($xml->item);
93
-
94
-        foreach ($xml->item as $value) {
95
-            $output['results'][] = [
96
-                'title' => (string) $value->title,
97
-                'rss'   => (string) $value->source,
98
-                'link'  => (string) $value->link,
99
-            ];
100
-        }
101
-
102
-        return $output;
103
-    }
104
-
105
-    /**
106
-    * @return void
107
-    */
108
-    public function original()
109
-    {
110
-        $this->isOrginal = true;
111
-    }
9
+	/**
10
+	 * @var string
11
+	 */
12
+	const APP_ID = "podcastsearchdemo";
13
+
14
+	/**
15
+	 * @var string
16
+	 */
17
+	const SEARCH_URL = "http://api.digitalpodcast.com/v2r/search";
18
+
19
+	/**
20
+	 * @var string
21
+	 */
22
+	const FORMAT = "rss";
23
+
24
+	/**
25
+	 * @var int
26
+	 */
27
+	private $limit = 15;
28
+
29
+	/**
30
+	 * @var string
31
+	 */
32
+	private $defaultQuery = null;
33
+
34
+	/**
35
+	 * DigitalPodcast constructor.
36
+	 */
37
+	public function __construct()
38
+	{
39
+		$this->setDefaultQuery();
40
+	}
41
+
42
+	/**
43
+	 * @return int
44
+	 */
45
+	public function getLimit()
46
+	{
47
+		return $this->limit;
48
+	}
49
+
50
+	/**
51
+	 * @param int $limit
52
+	 */
53
+	public function setLimit($limit)
54
+	{
55
+		// `-1` being used here because the API returns 3 items when `results=2`.
56
+		$this->limit = ((int) $limit - 1);
57
+	}
58
+
59
+	/**
60
+	 * @return void
61
+	 */
62
+	public function setDefaultQuery()
63
+	{
64
+		$this->defaultQuery = http_build_query([
65
+			'results' => $this->limit,
66
+			'appid'   => self::APP_ID,
67
+			'format'  => self::FORMAT
68
+		]);
69
+	}
70
+
71
+	/**
72
+	 * @param  string $value
73
+	 * @return string
74
+	 */
75
+	public function generateUrl($value)
76
+	{
77
+		$value = urlencode($value);
78
+		$url   = self::SEARCH_URL . "?keywords={$value}";
79
+
80
+		return $url . '&' . $this->defaultQuery;
81
+	}
82
+
83
+	/**
84
+	 * @param  array $response
85
+	 * @return array
86
+	 */
87
+	public function build(array $response)
88
+	{
89
+		$xml = new SimpleXMLElement($response['search']);
90
+		$xml = $xml->channel;
91
+
92
+		$output['result_count'] = count($xml->item);
93
+
94
+		foreach ($xml->item as $value) {
95
+			$output['results'][] = [
96
+				'title' => (string) $value->title,
97
+				'rss'   => (string) $value->source,
98
+				'link'  => (string) $value->link,
99
+			];
100
+		}
101
+
102
+		return $output;
103
+	}
104
+
105
+	/**
106
+	 * @return void
107
+	 */
108
+	public function original()
109
+	{
110
+		$this->isOrginal = true;
111
+	}
112 112
 }
Please login to merge, or discard this patch.
src/Vendors/Itunes.php 1 patch
Indentation   +113 added lines, -113 removed lines patch added patch discarded remove patch
@@ -5,117 +5,117 @@
 block discarded – undo
5 5
 
6 6
 class Itunes extends AbstractVendor implements VendorInterface
7 7
 {
8
-    /**
9
-     * @var string
10
-     */
11
-    const SEARCH_URL = "https://itunes.apple.com/search";
12
-
13
-    /**
14
-     * @var string
15
-     */
16
-    const LOOKUP_URL = "https://itunes.apple.com/lookup";
17
-
18
-    /**
19
-     * @var string
20
-     */
21
-    const ENTITY = "podcast";
22
-
23
-    /**
24
-     * @var string
25
-     */
26
-    const MEDIA = "podcast";
27
-
28
-    /**
29
-     * @var int
30
-     */
31
-    private $limit = 15;
32
-
33
-    /**
34
-     * @var string
35
-     */
36
-    private $defaultQuery = null;
37
-
38
-    /**
39
-     * Itunes constructor.
40
-     */
41
-    public function __construct()
42
-    {
43
-        $this->setDefaultQuery();
44
-    }
45
-
46
-    /**
47
-     * @return int
48
-     */
49
-    public function getLimit()
50
-    {
51
-        return $this->limit;
52
-    }
53
-
54
-    /**
55
-     * @param int $limit
56
-     */
57
-    public function setLimit($limit)
58
-    {
59
-        $this->limit = (int) $limit;
60
-    }
61
-
62
-    /**
63
-     * @return void
64
-     */
65
-    public function setDefaultQuery()
66
-    {
67
-        $this->defaultQuery = http_build_query([
68
-            'limit'     => $this->limit,
69
-            'entity'    => self::ENTITY,
70
-            'media'     => self::MEDIA
71
-        ]);
72
-    }
73
-
74
-    /**
75
-     * @param  string $value
76
-     * @return string
77
-     */
78
-    public function generateUrl($value)
79
-    {
80
-        $value = is_string($value) ? urlencode($value) : $value;
81
-        $url   = is_int($value) ? self::LOOKUP_URL . "?id={$value}" : self::SEARCH_URL . "?term={$value}";
82
-
83
-        return $url . '&' . $this->defaultQuery;
84
-    }
85
-
86
-    /**
87
-     * @param  array  $response
88
-     * @return array
89
-     */
90
-    public function build(array $response)
91
-    {
92
-        $response = json_decode($response['search']);
93
-        if ($this->isOrginal) {
94
-            return $response;
95
-        }
96
-        $output['result_count'] = $response->resultCount;
97
-
98
-        foreach ($response->results as $value) {
99
-            $output['results'][] = [
100
-                'itunes_id' => $value->collectionId,
101
-                'author'    => $value->artistName,
102
-                'title'     => $value->collectionName,
103
-                'episodes'  => $value->trackCount,
104
-                'image'     => $value->artworkUrl600,
105
-                'rss'       => $value->feedUrl,
106
-                'itunes'    => $value->collectionViewUrl,
107
-                'genre'     => $value->primaryGenreName,
108
-            ];
109
-        }
110
-
111
-        return $output;
112
-    }
113
-
114
-    /**
115
-    * @return void
116
-    */
117
-    public function original()
118
-    {
119
-        $this->isOrginal = true;
120
-    }
8
+	/**
9
+	 * @var string
10
+	 */
11
+	const SEARCH_URL = "https://itunes.apple.com/search";
12
+
13
+	/**
14
+	 * @var string
15
+	 */
16
+	const LOOKUP_URL = "https://itunes.apple.com/lookup";
17
+
18
+	/**
19
+	 * @var string
20
+	 */
21
+	const ENTITY = "podcast";
22
+
23
+	/**
24
+	 * @var string
25
+	 */
26
+	const MEDIA = "podcast";
27
+
28
+	/**
29
+	 * @var int
30
+	 */
31
+	private $limit = 15;
32
+
33
+	/**
34
+	 * @var string
35
+	 */
36
+	private $defaultQuery = null;
37
+
38
+	/**
39
+	 * Itunes constructor.
40
+	 */
41
+	public function __construct()
42
+	{
43
+		$this->setDefaultQuery();
44
+	}
45
+
46
+	/**
47
+	 * @return int
48
+	 */
49
+	public function getLimit()
50
+	{
51
+		return $this->limit;
52
+	}
53
+
54
+	/**
55
+	 * @param int $limit
56
+	 */
57
+	public function setLimit($limit)
58
+	{
59
+		$this->limit = (int) $limit;
60
+	}
61
+
62
+	/**
63
+	 * @return void
64
+	 */
65
+	public function setDefaultQuery()
66
+	{
67
+		$this->defaultQuery = http_build_query([
68
+			'limit'     => $this->limit,
69
+			'entity'    => self::ENTITY,
70
+			'media'     => self::MEDIA
71
+		]);
72
+	}
73
+
74
+	/**
75
+	 * @param  string $value
76
+	 * @return string
77
+	 */
78
+	public function generateUrl($value)
79
+	{
80
+		$value = is_string($value) ? urlencode($value) : $value;
81
+		$url   = is_int($value) ? self::LOOKUP_URL . "?id={$value}" : self::SEARCH_URL . "?term={$value}";
82
+
83
+		return $url . '&' . $this->defaultQuery;
84
+	}
85
+
86
+	/**
87
+	 * @param  array  $response
88
+	 * @return array
89
+	 */
90
+	public function build(array $response)
91
+	{
92
+		$response = json_decode($response['search']);
93
+		if ($this->isOrginal) {
94
+			return $response;
95
+		}
96
+		$output['result_count'] = $response->resultCount;
97
+
98
+		foreach ($response->results as $value) {
99
+			$output['results'][] = [
100
+				'itunes_id' => $value->collectionId,
101
+				'author'    => $value->artistName,
102
+				'title'     => $value->collectionName,
103
+				'episodes'  => $value->trackCount,
104
+				'image'     => $value->artworkUrl600,
105
+				'rss'       => $value->feedUrl,
106
+				'itunes'    => $value->collectionViewUrl,
107
+				'genre'     => $value->primaryGenreName,
108
+			];
109
+		}
110
+
111
+		return $output;
112
+	}
113
+
114
+	/**
115
+	 * @return void
116
+	 */
117
+	public function original()
118
+	{
119
+		$this->isOrginal = true;
120
+	}
121 121
 }
Please login to merge, or discard this patch.
src/Vendors/AbstractVendor.php 1 patch
Indentation   +163 added lines, -163 removed lines patch added patch discarded remove patch
@@ -7,167 +7,167 @@
 block discarded – undo
7 7
 
8 8
 abstract class AbstractVendor
9 9
 {
10
-    /**
11
-    * @var bool
12
-    */
13
-    protected $isOrginal = false;
14
-
15
-    /**
16
-     * @param SimpleXMLElement $feed
17
-     *
18
-     * @return array
19
-     */
20
-    public function buildFeed(SimpleXMLElement $feed)
21
-    {
22
-        $output = [
23
-            'title'          => (string) $feed->channel->title,
24
-            'description'    => (string) $feed->channel->description,
25
-            'summary'        => (string) $this->getValueByPath($feed->channel, "summary"),
26
-            'image'          => (string) $feed->channel->image->url,
27
-            'site'           => (string) $feed->channel->link,
28
-            'author'         => (string) $this->getValueByPath($feed->channel, "author"),
29
-            'language'       => (string) $feed->channel->language,
30
-            'categories'     => $this->fetchCategories($feed->channel),
31
-            'episode_count'  => (int) count($feed->channel->item),
32
-            'episodes'       => $this->getEpisodes($feed->channel)
33
-        ];
34
-
35
-        return $output;
36
-    }
37
-
38
-    /**
39
-     * @param SimpleXMLElement|mixed $channel
40
-     *
41
-     * @return array
42
-     */
43
-    protected function getEpisodes($channel)
44
-    {
45
-        $items = [];
46
-        foreach ($channel->item as $value) {
47
-            $items[] = [
48
-                'title'        => (string) $value->title,
49
-                'mp3'          => $this->getAudioUrl($value),
50
-                'size'         => $this->getEpisodeSize($value),
51
-                'duration'     => $this->getEpisodeDuration($value),
52
-                'description'  => (string) $value->description,
53
-                'keywords'     => $this->getKeywords($value),
54
-                'link'         => (string) $value->link,
55
-                'image'        => $this->getEpisodeImage($value, $channel),
56
-                'published_at' => $this->getPublishedDate($value),
57
-            ];
58
-        }
59
-
60
-        return $items;
61
-    }
62
-
63
-    /**
64
-    * @return array
65
-    */
66
-    protected function getKeywords($item)
67
-    {
68
-        return array_map('trim', explode(",", $this->getValueByPath($item, 'keywords')));
69
-    }
70
-
71
-    /**
72
-     * @param SimpleXMLElement $value
73
-     *
74
-     * @return null|string
75
-     */
76
-    protected function getAudioUrl($value)
77
-    {
78
-        return isset($value->enclosure) ? (string) $value->enclosure->attributes()->url : null;
79
-    }
80
-
81
-    /**
82
-     * @param SimpleXMLElement $value
83
-     *
84
-     * @return int
85
-     */
86
-    protected function getEpisodeSize($value)
87
-    {
88
-        return isset($value->enclosure) ? (int) $value->enclosure->attributes()->length : 0;
89
-    }
90
-
91
-    /**
92
-     * @param SimpleXMLElement $item
93
-     *
94
-     * @return string
95
-     */
96
-    protected function getPublishedDate($item)
97
-    {
98
-        $published_at = new DateTime();
99
-        $published_at->setTimestamp(strtotime($item->pubDate));
100
-
101
-        return $published_at->format('Y-m-d H:i:s');
102
-    }
103
-
104
-    /**
105
-     * @param SimpleXMLElement|mixed $item
106
-     * @param string $path
107
-     *
108
-     * @return SimpleXMLElement
109
-     */
110
-    protected function getValueByPath($item, $path)
111
-    {
112
-        return empty($item->xpath("itunes:{$path}")) ? null :
113
-            $item->xpath("itunes:{$path}")[0];
114
-    }
115
-
116
-    /**
117
-     * @param SimpleXMLElement|mixed $item
118
-     *
119
-     * @return int
120
-     */
121
-    protected function getEpisodeDuration($item)
122
-    {
123
-        $duration = (string) $this->getValueByPath($item, "duration");
124
-
125
-        $durationArray = explode(":", $duration);
126
-        if (count($durationArray) > 1) {
127
-            sscanf($duration, "%d:%d:%d", $hours, $minutes, $seconds);
128
-
129
-            $duration = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
130
-        }
131
-
132
-        return (int) $duration;
133
-    }
134
-
135
-    /**
136
-     * @param SimpleXMLElement $item
137
-     *
138
-     * @param SimpleXMLElement $channel
139
-     *
140
-     * @return string
141
-     */
142
-    protected function getEpisodeImage($item, $channel)
143
-    {
144
-        $xmlImage = $this->getValueByPath($item, "image");
145
-
146
-        return $xmlImage ? (string) $xmlImage->attributes()->href : (string) $channel->image->url;
147
-    }
148
-
149
-    /**
150
-     * @param SimpleXMLElement|mixed $channel
151
-     *
152
-     * @return array
153
-     */
154
-    protected function fetchCategories($channel)
155
-    {
156
-        $categories = $channel->xpath('itunes:category');
157
-
158
-        $items = [];
159
-        foreach ($categories as $category) {
160
-            $item = ['title' => (string) $category->attributes()->text, 'children' => []];
161
-
162
-            if (! empty($category->xpath('itunes:category'))) {
163
-                $inner = $this->fetchCategories($category);
164
-
165
-                $item['children'] = $inner;
166
-            }
167
-
168
-            $items[] = $item;
169
-        }
170
-
171
-        return $items;
172
-    }
10
+	/**
11
+	 * @var bool
12
+	 */
13
+	protected $isOrginal = false;
14
+
15
+	/**
16
+	 * @param SimpleXMLElement $feed
17
+	 *
18
+	 * @return array
19
+	 */
20
+	public function buildFeed(SimpleXMLElement $feed)
21
+	{
22
+		$output = [
23
+			'title'          => (string) $feed->channel->title,
24
+			'description'    => (string) $feed->channel->description,
25
+			'summary'        => (string) $this->getValueByPath($feed->channel, "summary"),
26
+			'image'          => (string) $feed->channel->image->url,
27
+			'site'           => (string) $feed->channel->link,
28
+			'author'         => (string) $this->getValueByPath($feed->channel, "author"),
29
+			'language'       => (string) $feed->channel->language,
30
+			'categories'     => $this->fetchCategories($feed->channel),
31
+			'episode_count'  => (int) count($feed->channel->item),
32
+			'episodes'       => $this->getEpisodes($feed->channel)
33
+		];
34
+
35
+		return $output;
36
+	}
37
+
38
+	/**
39
+	 * @param SimpleXMLElement|mixed $channel
40
+	 *
41
+	 * @return array
42
+	 */
43
+	protected function getEpisodes($channel)
44
+	{
45
+		$items = [];
46
+		foreach ($channel->item as $value) {
47
+			$items[] = [
48
+				'title'        => (string) $value->title,
49
+				'mp3'          => $this->getAudioUrl($value),
50
+				'size'         => $this->getEpisodeSize($value),
51
+				'duration'     => $this->getEpisodeDuration($value),
52
+				'description'  => (string) $value->description,
53
+				'keywords'     => $this->getKeywords($value),
54
+				'link'         => (string) $value->link,
55
+				'image'        => $this->getEpisodeImage($value, $channel),
56
+				'published_at' => $this->getPublishedDate($value),
57
+			];
58
+		}
59
+
60
+		return $items;
61
+	}
62
+
63
+	/**
64
+	 * @return array
65
+	 */
66
+	protected function getKeywords($item)
67
+	{
68
+		return array_map('trim', explode(",", $this->getValueByPath($item, 'keywords')));
69
+	}
70
+
71
+	/**
72
+	 * @param SimpleXMLElement $value
73
+	 *
74
+	 * @return null|string
75
+	 */
76
+	protected function getAudioUrl($value)
77
+	{
78
+		return isset($value->enclosure) ? (string) $value->enclosure->attributes()->url : null;
79
+	}
80
+
81
+	/**
82
+	 * @param SimpleXMLElement $value
83
+	 *
84
+	 * @return int
85
+	 */
86
+	protected function getEpisodeSize($value)
87
+	{
88
+		return isset($value->enclosure) ? (int) $value->enclosure->attributes()->length : 0;
89
+	}
90
+
91
+	/**
92
+	 * @param SimpleXMLElement $item
93
+	 *
94
+	 * @return string
95
+	 */
96
+	protected function getPublishedDate($item)
97
+	{
98
+		$published_at = new DateTime();
99
+		$published_at->setTimestamp(strtotime($item->pubDate));
100
+
101
+		return $published_at->format('Y-m-d H:i:s');
102
+	}
103
+
104
+	/**
105
+	 * @param SimpleXMLElement|mixed $item
106
+	 * @param string $path
107
+	 *
108
+	 * @return SimpleXMLElement
109
+	 */
110
+	protected function getValueByPath($item, $path)
111
+	{
112
+		return empty($item->xpath("itunes:{$path}")) ? null :
113
+			$item->xpath("itunes:{$path}")[0];
114
+	}
115
+
116
+	/**
117
+	 * @param SimpleXMLElement|mixed $item
118
+	 *
119
+	 * @return int
120
+	 */
121
+	protected function getEpisodeDuration($item)
122
+	{
123
+		$duration = (string) $this->getValueByPath($item, "duration");
124
+
125
+		$durationArray = explode(":", $duration);
126
+		if (count($durationArray) > 1) {
127
+			sscanf($duration, "%d:%d:%d", $hours, $minutes, $seconds);
128
+
129
+			$duration = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
130
+		}
131
+
132
+		return (int) $duration;
133
+	}
134
+
135
+	/**
136
+	 * @param SimpleXMLElement $item
137
+	 *
138
+	 * @param SimpleXMLElement $channel
139
+	 *
140
+	 * @return string
141
+	 */
142
+	protected function getEpisodeImage($item, $channel)
143
+	{
144
+		$xmlImage = $this->getValueByPath($item, "image");
145
+
146
+		return $xmlImage ? (string) $xmlImage->attributes()->href : (string) $channel->image->url;
147
+	}
148
+
149
+	/**
150
+	 * @param SimpleXMLElement|mixed $channel
151
+	 *
152
+	 * @return array
153
+	 */
154
+	protected function fetchCategories($channel)
155
+	{
156
+		$categories = $channel->xpath('itunes:category');
157
+
158
+		$items = [];
159
+		foreach ($categories as $category) {
160
+			$item = ['title' => (string) $category->attributes()->text, 'children' => []];
161
+
162
+			if (! empty($category->xpath('itunes:category'))) {
163
+				$inner = $this->fetchCategories($category);
164
+
165
+				$item['children'] = $inner;
166
+			}
167
+
168
+			$items[] = $item;
169
+		}
170
+
171
+		return $items;
172
+	}
173 173
 }
Please login to merge, or discard this patch.
src/PodcastScraper.php 1 patch
Indentation   +148 added lines, -148 removed lines patch added patch discarded remove patch
@@ -9,152 +9,152 @@
 block discarded – undo
9 9
 
10 10
 class PodcastScraper
11 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
-    public function get($value)
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
-    private function search(Request $request, $value)
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
-    public function find($feedUrl)
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
-    * @return PodcastScraper
100
-    */
101
-    public function original()
102
-    {
103
-        $this->vendor->original();
104
-
105
-        return $this;
106
-    }
107
-
108
-    /**
109
-     * @param  Request $request
110
-     * @param  string  $feedUrl
111
-     * @return array
112
-     * @throws Exception
113
-     */
114
-    private function read(Request $request, $feedUrl)
115
-    {
116
-        $output = $request->create($feedUrl);
117
-
118
-        if (is_null($output)) {
119
-            throw new Exception("Request to RSS failed", $request->getStatusCode());
120
-        }
121
-
122
-        return [
123
-            'feed'        => $output,
124
-            'status'      => true,
125
-        ];
126
-    }
127
-
128
-    /**
129
-     * @param string $feedUrl
130
-     *
131
-     * @return mixed
132
-     */
133
-    protected function getFeedFromUrl($feedUrl)
134
-    {
135
-        $response = $this->read(new Request, $feedUrl);
136
-
137
-        libxml_use_internal_errors(true);
138
-
139
-        try {
140
-            $feed = new SimpleXMLElement($response['feed'], LIBXML_NOCDATA, false);
141
-        } catch (Exception $except) {
142
-            $feed = new SimpleXMLElement(Xml::repair($response['feed']), LIBXML_NOCDATA, false);
143
-        }
144
-
145
-        return $feed;
146
-    }
147
-
148
-    /**
149
-     * @param string $message
150
-     *
151
-     * @return array
152
-     */
153
-    protected function failedResponse($message)
154
-    {
155
-        return [
156
-            'status'      => false,
157
-            'message'     => $message
158
-        ];
159
-    }
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
+	public function get($value)
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
+	private function search(Request $request, $value)
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
+	public function find($feedUrl)
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
+	 * @return PodcastScraper
100
+	 */
101
+	public function original()
102
+	{
103
+		$this->vendor->original();
104
+
105
+		return $this;
106
+	}
107
+
108
+	/**
109
+	 * @param  Request $request
110
+	 * @param  string  $feedUrl
111
+	 * @return array
112
+	 * @throws Exception
113
+	 */
114
+	private function read(Request $request, $feedUrl)
115
+	{
116
+		$output = $request->create($feedUrl);
117
+
118
+		if (is_null($output)) {
119
+			throw new Exception("Request to RSS failed", $request->getStatusCode());
120
+		}
121
+
122
+		return [
123
+			'feed'        => $output,
124
+			'status'      => true,
125
+		];
126
+	}
127
+
128
+	/**
129
+	 * @param string $feedUrl
130
+	 *
131
+	 * @return mixed
132
+	 */
133
+	protected function getFeedFromUrl($feedUrl)
134
+	{
135
+		$response = $this->read(new Request, $feedUrl);
136
+
137
+		libxml_use_internal_errors(true);
138
+
139
+		try {
140
+			$feed = new SimpleXMLElement($response['feed'], LIBXML_NOCDATA, false);
141
+		} catch (Exception $except) {
142
+			$feed = new SimpleXMLElement(Xml::repair($response['feed']), LIBXML_NOCDATA, false);
143
+		}
144
+
145
+		return $feed;
146
+	}
147
+
148
+	/**
149
+	 * @param string $message
150
+	 *
151
+	 * @return array
152
+	 */
153
+	protected function failedResponse($message)
154
+	{
155
+		return [
156
+			'status'      => false,
157
+			'message'     => $message
158
+		];
159
+	}
160 160
 }
Please login to merge, or discard this patch.