DigitalPodcast   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 106
c 0
b 0
f 0
wmc 8
lcom 2
cbo 1
rs 10

7 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
A original() 0 4 1
1
<?php
2
namespace Tzsk\ScrapePod\Vendors;
3
4
use SimpleXMLElement;
5
use Tzsk\ScrapePod\Contracts\VendorInterface;
6
7
class DigitalPodcast extends AbstractVendor implements VendorInterface
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
    }
112
}
113