Completed
Push — master ( f5e17e...ddae74 )
by PROSPER
10s
created

NytController::getRelativeUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use GuzzleHttp\Client;
6
use App\Http\Requests;
7
8
class NytController extends Controller
9
{
10
    const API_URL = 'http://api.nytimes.com/svc';
11
    const RELATIVE_URL = '/books/v3/lists/overview.json?api-key={apiKey}';
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 = self::API_URL;
31
         $this->client = new Client(['base_uri' => $this->baseUrl]);
32
         $this->setGetResponse($this->getRelativeUrl());
33
    }
34
35
    /**
36
     * Get relative url
37
     *
38
     * @return string
39
     */
40
    public function getRelativeUrl()
41
    {
42
        return str_replace('{apiKey}', env('NYT_BOOKS_API_KEY'), self::RELATIVE_URL);
43
    }
44
45
    /**
46
     * Get the response from New York times API
47
     * @param string $relativeUrl
48
     */
49
    private function setGetResponse($relativeUrl)
50
    {
51
        $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...
52
    }
53
54
    /**
55
     * Get the whole response from a get operation
56
     * @return array
57
     */
58
    private function getResponse()
59
    {
60
        return json_decode($this->response->getBody(), true);
61
    }
62
63
    /**
64
     * Get the data response from a get operation
65
     * @return array
66
     */
67
    private function getData()
68
    {
69
        return $this->getResponse()['results']['lists'][0]['books'];
70
    }
71
72
    /**
73
     * Return all the data to the New York times API dashboard
74
     * @return array
75
     */
76
    public function getPage()
77
    {
78
        $data = $this->getData();
79
80
        return view('api.nyt')->withData($data);
81
    }
82
}
83