GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Crawler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BilginPro\Agency\Iha;
4
5
use Carbon\Carbon;
6
use GuzzleHttp;
7
8
/**
9
 * Class Crawler
10
 * @package BilginPro\Ajans\Iha
11
 */
12
class Crawler
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $user_code = '';
18
19
    /**
20
     * @var string
21
     */
22
    protected $user_name = '';
23
24
    /**
25
     * @var string
26
     */
27
    protected $password = '';
28
29
    /**
30
     * @var int
31
     */
32
    protected $summary_length = 150;
33
34
    /**
35
     * @var array
36
     */
37
    protected $attributes = [
38
        'limit' => '5',
39
    ];
40
41
    /**
42
     * Create a new Crawler Instance
43
     */
44
    public function __construct($config)
45
    {
46
        $this->setParameters($config);
47
    }
48
49
    /**
50
     * Does the magic.
51
     * @return array
52
     */
53
    public function crawl($attributes = [])
54
    {
55
        $this->setAttributes($attributes);
56
57
        $response = $this->fetchUrl($this->getUrl());
58
        $xml = new \SimpleXMLElement($response);
59
        $result = [];
60
        $i = 0;
61
        foreach ($xml->channel->item as $item) {
62
            if ($this->attributes['limit'] > $i) {
63
                $news = new \stdClass;
64
                $news->code = (string)$item->HaberKodu;
65
                $news->title = (string)$item->title;
66
                $news->summary = (string)$this->shortenString($item->description, $this->summary_length);
67
                $news->content = (string)$item->description;
68
                $news->created_at = (new Carbon($item->pubDate))->format('d.m.Y H:i:s');
69
                $news->category = $this->titleCase($item->Kategori);
70
                $news->city = (!empty($item->Sehir) ? $this->titleCase($item->Sehir) : '');
71
                $news->images = [];
72
                if (isset($item->images) && count($item->images->image) > 0) {
73
                    foreach ($item->images->image as $image) {
74
                        $news->images[] = (string)$image;
75
                    }
76
                }
77
78
                $result[] = $news;
79
                $i++;
80
            }
81
        }
82
83
        return $result;
84
    }
85
86
    /**
87
     * Sets config parameters.
88
     */
89
    protected function setParameters($config)
90
    {
91
        if (!is_array($config)) {
92
            throw new \InvalidArgumentException('$config variable must be an array.');
93
        }
94
        if (array_key_exists('user_code', $config)) {
95
            $this->user_code = $config['user_code'];
96
        }
97
        if (array_key_exists('user_name', $config)) {
98
            $this->user_name = $config['user_name'];
99
        }
100
        if (array_key_exists('password', $config)) {
101
            $this->password = $config['password'];
102
        }
103
        if (array_key_exists('summary_length', $config)) {
104
            $this->summary_length = $config['summary_length'];
105
        }
106
    }
107
108
    /**
109
     * Sets filter attributes.
110
     * @param $attributes array
111
     */
112
    protected function setAttributes($attributes)
113
    {
114
        foreach ($attributes as $key => $value) {
115
            $this->attributes[$key] = $value;
116
        }
117
    }
118
119
    /**
120
     * Returns full url for crawling.
121
     * @return string
122
     */
123
    protected function getUrl()
124
    {
125
        $url = 'http://abone.iha.com.tr/yeniabone/rss2.aspx?Sehir=0&UserCode='
126
            . $this->user_code
127
            . '&UserName='
128
            . $this->user_name
129
            . '&UserPassword='
130
            . $this->password;
131
132
        return $url;
133
    }
134
135
136
    /**
137
     * Fethches given url and returns response as string.
138
     * @param $url
139
     * @param string $method
140
     * @param array $options
141
     *
142
     * @return string
143
     */
144
    protected function fetchUrl($url, $method = 'GET', $options = [])
145
    {
146
        $client = new GuzzleHttp\Client();
147
        $res = $client->request($method, $url, $options);
148
        if ($res->getStatusCode() == 200) {
149
            return (string)$res->getBody();
150
        }
151
        return '';
152
    }
153
154
    /**
155
     * Cuts the given string from the end of the appropriate word.
156
     * @param $str
157
     * @param $len
158
     * @return string
159
     */
160
    protected function shortenString($str, $len)
161
    {
162
        if (strlen($str) > $len) {
163
            $str = rtrim(mb_substr($str, 0, $len, 'UTF-8'));
164
            $str = substr($str, 0, strrpos($str, ' '));
165
            $str .= '...';
166
            $str = str_replace(',...', '...', $str);
167
        }
168
        return $str;
169
    }
170
171
    /**
172
     * Converts a string to "Title Case"
173
     * @param $str
174
     * @return string
175
     */
176
    protected function titleCase($str)
177
    {
178
        $str = mb_convert_case($str, MB_CASE_TITLE, 'UTF-8');
179
        return $str;
180
    }
181
}
182