Passed
Pull Request — master (#70)
by
unknown
13:15 queued 05:58
created

shouldPublishMigrations()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.679

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 3
cts 7
cp 0.4286
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 4.679
1
<?php
2
3
namespace NotificationChannels\ExpoPushNotifications;
4
5
use ExponentPhpSDK\Expo;
6
use ExponentPhpSDK\ExpoRegistrar;
7
use ExponentPhpSDK\ExpoRepository;
8
use ExponentPhpSDK\Repositories\ExpoFileDriver;
9
use Illuminate\Support\ServiceProvider;
10
use NotificationChannels\ExpoPushNotifications\Repositories\ExpoDatabaseDriver;
11
12
class ExpoPushNotificationsServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Bootstrap the application services.
16
     *
17
     * @return void
18
     */
19 23
    public function boot(\Illuminate\Routing\Router $router)
20
    {
21 23
        $this->setupConfig();
22
23 23
        $repository = $this->getInterestsDriver();
24
25 23
        $this->shouldPublishMigrations($repository);
0 ignored issues
show
Bug introduced by
It seems like $repository defined by $this->getInterestsDriver() on line 23 can also be of type null; however, NotificationChannels\Exp...ouldPublishMigrations() does only seem to accept object<ExponentPhpSDK\ExpoRepository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
26
27 23
        $this->app->when(ExpoChannel::class)
28 23
            ->needs(Expo::class)
29
            ->give(function () use ($repository) {
30
                return new Expo(new ExpoRegistrar($repository));
31 23
            });
32
            
33 23
        $router->middlewareGroup('expo.middleware', config("exponent-push-notifications")["middleware"]);
34
35 23
        $this->loadRoutesFrom(__DIR__.'/Http/routes.php');
36 23
    }
37
38
    /**
39
     * Register the application services.
40
     *
41
     * @return void
42
     */
43 23
    public function register()
44
    {
45 23
        $this->app->bind(ExpoRepository::class, get_class($this->getInterestsDriver()));
46 23
    }
47
48
    /**
49
     * Gets the Expo repository driver based on config.
50
     *
51
     * @return ExpoRepository
52
     */
53 23
    public function getInterestsDriver()
54
    {
55 23
        $driver = config('exponent-push-notifications.interests.driver');
56
57 23
        switch ($driver) {
58 23
            case 'database':
59
                return new ExpoDatabaseDriver();
60
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
61
            default:
62 23
                return new ExpoFileDriver();
63
        }
64
    }
65
66
    /**
67
     * Publishes the configuration files for the package.
68
     *
69
     * @return void
70
     */
71 23
    protected function setupConfig()
72
    {
73 23
        $this->publishes([
74 23
            __DIR__.'/../config/exponent-push-notifications.php' => config_path('exponent-push-notifications.php'),
75 23
        ], 'config');
76
77 23
        $this->mergeConfigFrom(__DIR__.'/../config/exponent-push-notifications.php', 'exponent-push-notifications');
78 23
    }
79
80
    /**
81
     * Publishes the migration files needed in the package.
82
     *
83
     * @param ExpoRepository $repository
84
     *
85
     * @return void
86
     */
87 23
    private function shouldPublishMigrations(ExpoRepository $repository)
88
    {
89 23
        if ($repository instanceof ExpoDatabaseDriver && ! class_exists('CreateExponentPushNotificationInterestsTable')) {
90
            $timestamp = date('Y_m_d_His', time());
91
            $this->publishes([
92
                __DIR__.'/../migrations/create_exponent_push_notification_interests_table.php.stub' => database_path("/migrations/{$timestamp}_create_exponent_push_notification_interests_table.php"),
93
            ], 'migrations');
94
        }
95 23
    }
96
}
97