Completed
Push — master ( d5e2c3...057503 )
by Nassif
57:42
created

TranslationSheetServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 66
ccs 34
cts 34
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
A register() 0 10 1
A registerGoogleApiClient() 0 11 1
A registerSpreadsheet() 0 9 1
A registerCommands() 0 12 1
A asArray() 0 10 2
1
<?php
2
3
namespace Nikaia\TranslationSheet;
4
5
use Illuminate\Support\ServiceProvider;
6
use Nikaia\TranslationSheet\Client\Client;
7
use Nikaia\TranslationSheet\Commands\Lock;
8
use Nikaia\TranslationSheet\Commands\Prepare;
9
use Nikaia\TranslationSheet\Commands\Pull;
10
use Nikaia\TranslationSheet\Commands\Push;
11
use Nikaia\TranslationSheet\Commands\Setup;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Nikaia\TranslationSheet\Setup.

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...
12
use Nikaia\TranslationSheet\Commands\Status;
13
use Nikaia\TranslationSheet\Commands\Unlock;
14
15
class TranslationSheetServiceProvider extends ServiceProvider
16
{
17 34
    public function boot()
18
    {
19 34
        $this->publishes([
20 34
            __DIR__.'/../config/config.php' => config_path('translation_sheet.php'),
21 34
        ], 'config');
22 34
    }
23
24 34
    public function register()
25
    {
26 34
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'translation_sheet');
27
28 34
        $this->registerGoogleApiClient();
29
30 34
        $this->registerSpreadsheet();
31
32 34
        $this->registerCommands();
33 34
    }
34
35 34
    private function registerGoogleApiClient()
36
    {
37
        $this->app->singleton(Client::class, function () {
38 17
            $client = new Client;
39 17
            $client->setAuthConfigFile($this->app['config']['translation_sheet.serviceAccountCredentialsFile']);
40 17
            $client->setApplicationName($this->app['config']['translation_sheet.googleApplicationName']);
41 17
            $client->setScopes(\Google_Service_Sheets::SPREADSHEETS);
42
43 17
            return $client;
44 34
        });
45 34
    }
46
47 34
    private function registerSpreadsheet()
48
    {
49
        $this->app->singleton(Spreadsheet::class, function () {
50 1
            return new Spreadsheet(
51 1
                $this->app['config']['translation_sheet.spreadsheetId'],
52 1
                $this->asArray($this->app['config']['translation_sheet.locales'])
53
            );
54 34
        });
55 34
    }
56
57 34
    private function registerCommands()
58
    {
59 34
        $this->commands([
60 34
            Setup::class,
61
            Push::class,
62
            Pull::class,
63
            Prepare::class,
64
            Lock::class,
65
            Unlock::class,
66
            Status::class,
67
        ]);
68 34
    }
69
70 1
    private function asArray($value)
71
    {
72 1
        if (is_array($value)) {
73
            return $value;
74
        }
75
76 1
        return array_filter(array_map(function ($item) {
77 1
            return trim($item);
78 1
        }, explode(',', $value)));
79
    }
80
}
81