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.
Completed
Push — master ( b85921...456cb8 )
by Yavuz Selim
01:27
created

Crawler::setAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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