Completed
Push — master ( a624e1...736705 )
by Nassif
12:28
created

TranslationSheetServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\Open;
9
use Nikaia\TranslationSheet\Commands\Prepare;
10
use Nikaia\TranslationSheet\Commands\Pull;
11
use Nikaia\TranslationSheet\Commands\Push;
12
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...
13
use Nikaia\TranslationSheet\Commands\Status;
14
use Nikaia\TranslationSheet\Commands\Unlock;
15
16
class TranslationSheetServiceProvider extends ServiceProvider
17
{
18 36
    public function boot()
19
    {
20 36
        $this->publishes([
21 36
            __DIR__.'/../config/config.php' => config_path('translation_sheet.php'),
22 36
        ], 'config');
23 36
    }
24
25 36
    public function register()
26
    {
27 36
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'translation_sheet');
28
29 36
        $this->registerGoogleApiClient();
30
31 36
        $this->registerSpreadsheet();
32
33 36
        $this->registerCommands();
34 36
    }
35
36 36
    private function registerGoogleApiClient()
37
    {
38
        $this->app->singleton(Client::class, function () {
39 18
            return Client::create(
40 18
                $this->app['config']['translation_sheet.serviceAccountCredentialsFile'],
41 18
                $this->app['config']['translation_sheet.googleApplicationName']
42
            );
43 36
        });
44 36
    }
45
46 36
    private function registerSpreadsheet()
47
    {
48
        $this->app->singleton(Spreadsheet::class, function () {
49 1
            return new Spreadsheet(
50 1
                $this->app['config']['translation_sheet.spreadsheetId'],
51 1
                Util::asArray($this->app['config']['translation_sheet.locales'])
52
            );
53 36
        });
54 36
    }
55
56 36
    private function registerCommands()
57
    {
58 36
        $this->commands([
59 36
            Setup::class,
60
            Push::class,
61
            Pull::class,
62
            Prepare::class,
63
            Lock::class,
64
            Unlock::class,
65
            Status::class,
66
            Open::class,
67
        ]);
68 36
    }
69
}
70