PodcastScraper   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 151
Duplicated Lines 37.75 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 57
loc 151
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 16 16 3
A search() 13 13 2
A find() 15 15 3
A limit() 0 7 1
A original() 0 6 1
A read() 13 13 2
A getFeedFromUrl() 0 14 2
A failedResponse() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Tzsk\ScrapePod;
3
4
use Exception;
5
use SimpleXMLElement;
6
use Tzsk\ScrapePod\Contracts\VendorInterface;
7
use Tzsk\ScrapePod\Helpers\Request;
8
use Tzsk\ScrapePod\Helpers\Xml;
9
10
class PodcastScraper
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 View Code Duplication
    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 View Code Duplication
    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 View Code Duplication
    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 View Code Duplication
    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
}
161