Completed
Push — master ( f0ac42...cacd42 )
by Felix
07:32
created

SearchConsoleServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 9.68 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 6
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 20 1
B guardAgainstInvalidConfiguration() 6 10 5
A boot() 0 4 1
A setupConfig() 0 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SchulzeFelix\SearchConsole;
4
5
use Illuminate\Support\ServiceProvider;
6
use Laravel\Lumen\Application as LumenApplication;
7
use Illuminate\Foundation\Application as LaravelApplication;
8
use SchulzeFelix\SearchConsole\Exceptions\InvalidConfiguration;
9
10
class SearchConsoleServiceProvider extends ServiceProvider
11
{
12
    /**
13
     * Perform post-registration booting of services.
14
     *
15
     * @return void
16
     */
17
    public function boot()
18
    {
19
        $this->setupConfig();
20
    }
21
22
    /**
23
     * Register any package services.
24
     *
25
     * @return void
26
     */
27
    public function register()
28
    {
29
        $this->mergeConfigFrom(__DIR__.'/config/search-console.php', 'search-console');
30
31
        $searchConsoleConfig = config('search-console');
32
33
        $this->app->singleton(SearchConsoleClient::class, function () use ($searchConsoleConfig) {
34
            return SearchConsoleClientFactory::createForConfig($searchConsoleConfig);
35
        });
36
37
        $this->app->singleton(SearchConsole::class, function () use ($searchConsoleConfig) {
38
            $this->guardAgainstInvalidConfiguration($searchConsoleConfig);
39
40
            $client = app(SearchConsoleClient::class);
41
42
            return new SearchConsole($client);
43
        });
44
45
        $this->app->alias(SearchConsole::class, 'laravel-searchconsole');
46
    }
47
48
    protected function guardAgainstInvalidConfiguration(array $searchConsoleConfig = null)
49
    {
50 View Code Duplication
        if ($searchConsoleConfig['auth_type'] == 'service_account' && ! file_exists($searchConsoleConfig['connections']['service_account']['application_credentials'])) {
51
            throw InvalidConfiguration::credentialsJsonDoesNotExist($searchConsoleConfig['connections']['service_account']['application_credentials']);
52
        }
53
54 View Code Duplication
        if ($searchConsoleConfig['auth_type'] == 'oauth_json' && ! file_exists($searchConsoleConfig['connections']['oauth_json']['auth_config'])) {
55
            throw InvalidConfiguration::credentialsJsonDoesNotExist($searchConsoleConfig['connections']['oauth_json']['auth_config']);
56
        }
57
    }
58
59
    protected function setupConfig()
60
    {
61
        $source = realpath(__DIR__.'/config/search-console.php');
62
63
        if ($this->app instanceof LaravelApplication) {
64
            $this->publishes([$source => config_path('search-console.php')]);
65
        } elseif ($this->app instanceof LumenApplication) {
0 ignored issues
show
Bug introduced by
The class Laravel\Lumen\Application does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
66
            $this->app->configure('search-console');
67
        }
68
69
        $this->mergeConfigFrom($source, 'search-console');
70
    }
71
}
72