WebScrapingController::getData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests;
6
use Goutte\Client;
7
8
class WebScrapingController extends Controller
9
{
10
    protected $crawler;
11
12
    const NEWS_URL = 'https://news.ycombinator.com/';
13
14
    /**
15
     * Initialize controller
16
     */
17
    public function __construct()
18
    {
19
        $this->client = new Client();
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
    }
21
22
    /**
23
     * Return all data to the Stripe API dashboard
24
     * @return mixed
25
     */
26
    public function getPage()
27
    {
28
        $links = $this->getData(self::NEWS_URL);
29
30
        return view('api.scraping')->withLinks($links);
31
    }
32
33
    /**
34
     * Scrape the Links
35
     * @param $siteToCrawl
36
     * @return array
37
     */
38
    public function getData($siteToCrawl)
39
    {
40
        $crawler = $this->client->request('GET', $siteToCrawl);
41
42
        $arr = $crawler->filter('.title a[href^="http"], a[href^="https"]')->each(function($element) {
43
            $links = [];
44
45
            array_push($links, $element->text());
46
47
            return $links;
48
        });
49
50
        return $arr;
51
    }
52
}
53