PayuServiceProvider::migrationFileExists()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Tzsk\Payu;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\View\Compilers\BladeCompiler;
8
use Tzsk\Payu\Commands\PublishComponents;
9
use Tzsk\Payu\Commands\VerifyPendingTransactions;
10
use Tzsk\Payu\Components\Form;
11
12
class PayuServiceProvider extends ServiceProvider
13
{
14
    public function boot(): void
15
    {
16
        if ($this->app->runningInConsole()) {
17
            $this->publishes([
18
                __DIR__ . '/../config/payu.php' => config_path('payu.php'),
19
            ], 'payu-config');
20
21
            $this->publishes([
22
                __DIR__ . '/../resources/views' => base_path('resources/views/vendor/payu'),
23
            ], 'payu-template');
24
25
            $migrationFileName = 'create_payu_transactions_table.php';
26
            $source = __DIR__ . "/../database/migrations/{$migrationFileName}";
27
28
            if (! $this->migrationFileExists($migrationFileName)) {
29
                $destination = database_path('migrations/' . date('Y_m_d_His', time()) . '_' . $migrationFileName);
30
                $this->publishes([$source => $destination], 'payu-migration');
31
            }
32
33
            $this->commands([
34
                PublishComponents::class,
35
                VerifyPendingTransactions::class,
36
            ]);
37
        }
38
39
        $this->loadViewsFrom(__DIR__ . '/../resources/views', 'payu');
40
        $this->loadRoutesFrom(__DIR__ . '/../routes/payu.php');
41
        $this->callAfterResolving(BladeCompiler::class, function () {
42
            Blade::component(Form::class, 'payu-form');
43
        });
44
45
        $this->app->bind('payu', function () {
46
            return new Payu();
47
        });
48
    }
49
50
    public function register(): void
51
    {
52
        $this->mergeConfigFrom(__DIR__ . '/../config/payu.php', 'payu');
53
    }
54
55
    public static function migrationFileExists(string $migrationFileName): bool
56
    {
57
        $len = strlen($migrationFileName);
58
        foreach (glob(database_path("migrations/*.php")) as $filename) {
59
            if ((substr($filename, -$len) === $migrationFileName)) {
60
                return true;
61
            }
62
        }
63
64
        return false;
65
    }
66
}
67