YahooController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A setGetResponse() 0 4 1
A getResponse() 0 4 1
A getData() 0 4 1
A getPage() 0 6 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use GuzzleHttp\Client;
6
use App\Http\Requests;
7
8
class YahooController extends Controller
9
{
10
    const YAHOO_API = 'https://query.yahooapis.com/v1/public/yql';
11
    /**
12
     * Instance of Guzzle Client
13
     * @var object
14
     */
15
    protected $client;
16
17
    /**
18
     * BaseUrl
19
     * @var string
20
     */
21
    protected $baseUrl;
22
23
    /**
24
     * Initialize the Controller with necessary arguments
25
     */
26
    public function __construct()
27
    {
28
         $this->baseUrl = self::YAHOO_API;
29
         $this->client = new Client(['base_uri' => $this->baseUrl]);
30
31
         $query = "SELECT * FROM weather.forecast WHERE (location = 10007)";
32
33
         $relativeUrl = '?q=' . $query . '&format=json';
34
         $this->setGetResponse($relativeUrl);
35
    }
36
37
    /**
38
     * Get the response from Yahoo API
39
     * @param string $relativeUrl
40
     */
41
    private function setGetResponse($relativeUrl)
42
    {
43
        $this->response = $this->client->get($this->baseUrl . $relativeUrl, []);
0 ignored issues
show
Bug introduced by
The property response 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...
44
    }
45
46
    /**
47
     * Get the whole response from a get operation
48
     * @return array
49
     */
50
    private function getResponse()
51
    {
52
        return json_decode($this->response->getBody(), true);
53
    }
54
55
    /**
56
     * Get the data response from a get operation
57
     * @return array
58
     */
59
    private function getData()
60
    {
61
        return $this->getResponse()['query']['results']['channel'];
62
    }
63
64
65
     /**
66
     * Return all data to the Yahoo API dashboard
67
     * @return mixed
68
     */
69
    public function getPage()
70
    {
71
        $data = $this->getData();
72
73
        return view('api.yahoo')->withData($data);
74
    }
75
76
}
77