Passed
Push — master ( 46dc7a...7d8ee2 )
by Kazi Mainuddin
02:02
created

Itunes::original()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Tzsk\ScrapePod\Vendors;
3
4
use Tzsk\ScrapePod\Contracts\VendorInterface;
5
6
class Itunes extends AbstractVendor implements VendorInterface
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
    }
121
}
122