HttpClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace MarkSitko\LaravelUnsplash\Http;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
8
class HttpClient
9
{
10
    const API_URL = 'https://api.unsplash.com/';
11
12
    protected $client;
13
14
    private $accessKey;
15
16
    /**
17
    * Creates a new instance of HttpClient
18
    * @return MarkSitko\LaravelUnsplash\Http\HttpClient
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
19
    */
20
    public function __construct()
21
    {
22
        $this->initalizeConfiguration()
23
            ->createClient();
24
25
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
26
    }
27
28
    /**
29
     * Initalize and store configuration values in class properties
30
     * @return MarkSitko\LaravelUnsplash\Http\HttpClient
31
     */
32
    private function initalizeConfiguration()
33
    {
34
        $this->accessKey = config('unsplash.access_key');
35
36
        if ( !$this->accessKey ) {
37
            throw new Exception('A Unsplash-API access key must be provided');
38
        }
39
40
        return $this;
41
    }
42
43
    /**
44
     * Instantiate the GuzzleHttp Client
45
     * @return MarkSitko\LaravelUnsplash\Http\HttpClient
46
     */
47
    private function createClient()
48
    {
49
        $this->client = new Client([
50
            'base_uri' => self::API_URL,
51
            'headers' => [
52
                'Content-Type' => 'application/x-www-form-urlencoded',
53
                'Authorization' => "Client-ID {$this->accessKey}",
54
            ],
55
        ]);
56
57
        return $this;
58
    }
59
}
60