IexCloudSdkServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Digitonic\IexCloudSdk;
4
5
use GuzzleHttp\Client;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Digitonic\IexCloudSdk\Client.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Illuminate\Support\ServiceProvider;
7
use Digitonic\IexCloudSdk\Contracts\IEXCloud;
8
use Digitonic\IexCloudSdk\Exceptions\InvalidConfig;
9
10
class IexCloudSdkServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * Bootstrap the application services.
14
     */
15 170
    public function boot()
16
    {
17 170
        if ($this->app->runningInConsole()) {
18 170
            $this->publishes([
19 170
                __DIR__.'/../config/config.php' => config_path('iex-cloud-sdk.php'),
20 170
            ], 'config');
21
        }
22 170
    }
23
24
    /**
25
     * Register the application services.
26
     */
27 170
    public function register()
28
    {
29 170
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'iex-cloud-sdk');
30
31
        $this->app->bind(IEXCloud::class, function () {
32 59
            $config = config('iex-cloud-sdk');
33
34 59
            $this->guardAgainstInvalidConfig($config);
35
36 56
            $guzzle = new Client([
37 56
                'base_uri' => $config['sandbox'] ? $config['sandbox_base_url'] : $config['base_url'],
38
                'headers' => [
39
                    'Accept' => 'application/json',
40
                    'Content-Type' => 'application/json'
41
                ],
42 56
                'query' => ['token' => $config['secret_key']]
43
            ]);
44
45 56
            return new \Digitonic\IexCloudSdk\Client($guzzle);
46 170
        });
47 170
    }
48
49
    /**
50
     * @param  array|null  $config
51
     */
52 59
    protected function guardAgainstInvalidConfig(array $config = null)
53
    {
54 59
        if (empty($config['base_url'])) {
55 1
            throw InvalidConfig::baseUrlNotSpecified();
56
        }
57
58 58
        if (empty($config['secret_key'])) {
59 1
            throw InvalidConfig::apiKeyNotSpecified();
60
        }
61
62 57
        if (empty($config['public_key'])) {
63 1
            throw InvalidConfig::apiKeyNotSpecified();
64
        }
65 56
    }
66
}
67