Completed
Push — master ( 397ebb...42d8fc )
by PROSPER
02:30
created

YahooController::getPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 6
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 GuzzleHttp\Client;
8
use App\Http\Requests;
9
use App\Http\Controllers\Controller;
10
11 View Code Duplication
class YahooController extends Controller
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
     /**
14
     * Instance of Guzzle Client
15
     * @var object
16
     */
17
    protected $client;
18
19
    /**
20
     * BaseUrl
21
     * @var string
22
     */
23
    protected $baseUrl;
24
25
    /**
26
     * Initialize the Controller with necessary arguments
27
     */
28
    public function __construct()
29
    {
30
         $this->baseUrl = 'https://query.yahooapis.com/v1/public/yql';
31
         $this->client = new Client(['base_uri' => $this->baseUrl]);
32
33
         $query = "SELECT * FROM weather.forecast WHERE (location = 10007)";
34
35
         $relativeUrl = '?q=' . $query . '&format=json';
36
         $this->setGetResponse($relativeUrl);
37
    }
38
39
    /**
40
     * Get the response from Yahoo API
41
     * @param string $relativeUrl
42
     */
43
    private function setGetResponse($relativeUrl)
44
    {
45
        $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...
46
    }
47
48
    /**
49
     * Get the whole response from a get operation
50
     * @return array
51
     */
52
    private function getResponse()
53
    {
54
        return json_decode($this->response->getBody(), true);
55
    }
56
57
    /**
58
     * Get the data response from a get operation
59
     * @return array
60
     */
61
    private function getData()
62
    {
63
        return $this->getResponse()['query']['results']['channel'];
64
    }
65
66
67
     /**
68
     * Return all data to the Yahoo API dashboard
69
     * @return mixed
70
     */
71
    public function getPage()
72
    {
73
        $data = $this->getData();
74
75
        return view('api.yahoo')->withData($data);
76
    }
77
78
}
79