WebScrapingController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPage() 0 6 1
A getData() 0 14 1
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