Completed
Push — master ( efeb78...e97d02 )
by PROSPER
03:45
created

NytController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 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
use Illuminate\Http\Request;
8
use App\Http\Controllers\Controller;
9
10
class NytController extends Controller
11
{
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 = 'http://api.nytimes.com/svc';
31
         $this->client = new Client(['base_uri' => $this->baseUrl]);
32
33
         $relativeUrl = '/books/v3/lists/overview.json?api-key=' . env('NYT_BOOKS_API_KEY');
34
         $this->setGetResponse($relativeUrl);
35
    }
36
37
    /**
38
     * Get the response from New York times 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()['results']['lists'][0]['books'];
62
    }
63
64
    /**
65
     * Return all the data to the New York times API dashboard
66
     * @return array
67
     */
68
    public function getPage()
69
    {
70
        $data = $this->getData();
71
72
        return view('api.nyt')->withData($data);
73
    }
74
}
75