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

TranslationSheetServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
A register() 0 10 1
A registerGoogleApiClient() 0 9 1
A registerSpreadsheet() 0 9 1
A registerCommands() 0 13 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\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