Passed
Pull Request — master (#189)
by
unknown
04:54
created

WalletServiceProvider::shouldMigrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Bavix\Wallet;
4
5
use Bavix\Wallet\Commands\RefreshBalance;
6
use Bavix\Wallet\Interfaces\Mathable;
7
use Bavix\Wallet\Interfaces\Rateable;
8
use Bavix\Wallet\Interfaces\Storable;
9
use Bavix\Wallet\Models\Transaction;
10
use Bavix\Wallet\Models\Transfer;
11
use Bavix\Wallet\Models\Wallet;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Bavix\Wallet\Wallet. Consider defining an alias.

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 Bavix\Wallet\Objects\Bring;
13
use Bavix\Wallet\Objects\Cart;
14
use Bavix\Wallet\Objects\EmptyLock;
15
use Bavix\Wallet\Objects\Operation;
16
use Bavix\Wallet\Services\CommonService;
17
use Bavix\Wallet\Services\DbService;
18
use Bavix\Wallet\Services\ExchangeService;
19
use Bavix\Wallet\Services\LockService;
20
use Bavix\Wallet\Simple\Math;
21
use Bavix\Wallet\Services\WalletService;
22
use Bavix\Wallet\Simple\Rate;
23
use Bavix\Wallet\Simple\Store;
24
use Bavix\Wallet\Wallet as WalletConfiguration;
25
use Illuminate\Support\ServiceProvider;
26
use function config;
27
use function dirname;
28
use function function_exists;
29
30
class WalletServiceProvider extends ServiceProvider
31
{
32
33
    /**
34
     * Bootstrap services.
35
     *
36
     * @return void
37
     * @codeCoverageIgnore
38
     */
39
    public function boot(): void
40
    {
41
        $this->loadTranslationsFrom(
42
            dirname(__DIR__) . '/resources/lang',
43
            'wallet'
44
        );
45
46
        if (!$this->app->runningInConsole()) {
47
            return;
48
        }
49
50
        $this->commands([RefreshBalance::class]);
51
52
        if ($this->shouldMigrate()) {
53
            $this->loadMigrationsFrom([
54
                __DIR__ . '/../database/migrations_v1',
55
                __DIR__ . '/../database/migrations_v2',
56
            ]);
57
        }
58
59
        if (function_exists('config_path')) {
60
            $this->publishes([
61
                dirname(__DIR__) . '/config/config.php' => config_path('wallet.php'),
62
            ], 'laravel-wallet-config');
63
        }
64
65
        $this->publishes([
66
            dirname(__DIR__) . '/database/migrations_v1/' => database_path('migrations'),
67
            dirname(__DIR__) . '/database/migrations_v2/' => database_path('migrations'),
68
        ], 'laravel-wallet-migrations');
69
70
        $this->publishes([
71
            dirname(__DIR__) . '/database/migrations_v1/' => database_path('migrations'),
72
        ], 'laravel-wallet-migrations-v1');
73
74
        $this->publishes([
75
            dirname(__DIR__) . '/database/migrations_v2/' => database_path('migrations'),
76
        ], 'laravel-wallet-migrations-v2');
77
    }
78
79
    /**
80
     * Register services.
81
     *
82
     * @return void
83
     */
84 160
    public function register(): void
85
    {
86 160
        $this->mergeConfigFrom(
87 160
            dirname(__DIR__) . '/config/config.php',
88 160
            'wallet'
89
        );
90
91
        // Bind eloquent models to IoC container
92 160
        $this->app->singleton(Rateable::class, config('wallet.package.rateable', Rate::class));
93 160
        $this->app->singleton(Storable::class, config('wallet.package.storable', Store::class));
94 160
        $this->app->singleton(Mathable::class, config('wallet.package.mathable', Math::class));
95 160
        $this->app->singleton(DbService::class, config('wallet.services.db', DbService::class));
96 160
        $this->app->singleton(ExchangeService::class, config('wallet.services.exchange', ExchangeService::class));
97 160
        $this->app->singleton(CommonService::class, config('wallet.services.common', CommonService::class));
98 160
        $this->app->singleton(WalletService::class, config('wallet.services.wallet', WalletService::class));
99 160
        $this->app->singleton(LockService::class, config('wallet.services.lock', LockService::class));
100
101
        // models
102 160
        $this->app->bind(Transaction::class, config('wallet.transaction.model', Transaction::class));
103 160
        $this->app->bind(Transfer::class, config('wallet.transfer.model', Transfer::class));
104 160
        $this->app->bind(Wallet::class, config('wallet.wallet.model', Wallet::class));
105
106
        // object's
107 160
        $this->app->bind(Bring::class, config('wallet.objects.bring', Bring::class));
108 160
        $this->app->bind(Cart::class, config('wallet.objects.cart', Cart::class));
109 160
        $this->app->bind(EmptyLock::class, config('wallet.objects.emptyLock', EmptyLock::class));
110 160
        $this->app->bind(Operation::class, config('wallet.objects.operation', Operation::class));
111 160
    }
112
113
    /**
114
     * Determine if we should register the migrations.
115
     *
116
     * @return bool
117
     */
118 160
    protected function shouldMigrate()
119
    {
120 160
        return WalletConfiguration::$runsMigrations;
121
    }
122
}
123