Completed
Push — master ( 595f51...871913 )
by Nassif
13:24
created

TranslationSheetServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

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 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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 35
    public function boot()
19
    {
20 35
        $this->publishes([
21 35
            __DIR__.'/../config/config.php' => config_path('translation_sheet.php'),
22 35
        ], 'config');
23 35
    }
24
25 35
    public function register()
26
    {
27 35
        $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'translation_sheet');
28
29 35
        $this->registerGoogleApiClient();
30
31 35
        $this->registerSpreadsheet();
32
33 35
        $this->registerCommands();
34 35
    }
35
36 35
    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 35
        });
44 35
    }
45
46 35
    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
                $this->asArray($this->app['config']['translation_sheet.locales'])
52
            );
53 35
        });
54 35
    }
55
56 35
    private function registerCommands()
57
    {
58 35
        $this->commands([
59 35
            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 35
    }
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