Completed
Push — master ( 7e4cd3...397ebb )
by PROSPER
03:28
created

WebScrapingController::getPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
7
use App\Http\Requests;
8
use Goutte\Client;
9
use App\Http\Controllers\Controller;
10
11
class WebScrapingController extends Controller
12
{
13
    protected $crawler;
14
15
    /**
16
     * [__construct description]
17
     */
18
    public function __construct()
19
    {
20
        $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...
21
    }
22
23
    /**
24
     * Return all data to the Stripe API dashboard
25
     * @return mixed
26
     */
27
    public function getPage()
28
    {
29
        $links = $this->getData('https://news.ycombinator.com/');
30
31
        return view('api.scraping')->withLinks($links);
32
    }
33
34
    /**
35
     * Scrape the Links
36
     * @param $siteToCrawl
37
     * @return array
38
     */
39
    public function getData($siteToCrawl)
40
    {
41
        $crawler = $this->client->request('GET', $siteToCrawl);
42
43
        $arr = $crawler->filter('.title a[href^="http"], a[href^="https"]')->each(function($element) {
44
            $links = [];
45
46
            array_push($links, $element->text());
47
48
            return $links;
49
        });
50
51
        return $arr;
52
    }
53
}
54