Issues (22)

src/ContactsServiceProvider.php (3 issues)

Labels
1
<?php
2
3
namespace Chuckcms\Contacts;
4
5
use Chuckcms\Contacts\Contracts\Contact as ContactContract;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\ServiceProvider;
9
10
class ContactsServiceProvider extends ServiceProvider
11
{
12
    public function boot()
13
    {
14
        $this->doPublishing();
15
16
        $this->registerModelBindings();
17
    }
18
19
    public function register()
20
    {
21
        $this->mergeConfigFrom(
22
            __DIR__.'/../config/contacts.php',
23
            'contacts'
24
        );
25
    }
26
27
    public function doPublishing()
28
    {
29
        if (!function_exists('config_path')) {
30
            // function not available and 'publish' not relevant in Lumen (credit: Spatie)
31
            return;
32
        }
33
34
        $this->publishes([
35
            __DIR__.'/../config/contacts.php' => config_path('contacts.php'),
36
        ], 'config');
37
38
        $this->publishes([
39
            __DIR__.'/../database/migrations/create_contacts_tables.php.stub' => $this->getMigrationFileName('create_contacts_tables.php'),
40
        ], 'migrations');
41
    }
42
43
    public function registerModelBindings()
44
    {
45
        $config = $this->app->config['contacts.models'];
0 ignored issues
show
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
46
47
        $this->app->bind(ContactContract::class, $config['contact']);
48
    }
49
50
    /**
51
     * Returns existing migration file if found, else uses the current timestamp.
52
     *
53
     * @return string
54
     */
55
    public function getMigrationFileName($migrationFileName): string
56
    {
57
        $timestamp = date('Y_m_d_His');
58
59
        $filesystem = $this->app->make(Filesystem::class);
60
61
        return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
0 ignored issues
show
$this->app->databasePath...cts\DIRECTORY_SEPARATOR of type string is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::make(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        return Collection::make(/** @scrutinizer ignore-type */ $this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
Loading history...
62
            ->flatMap(function ($path) use ($filesystem, $migrationFileName) {
63
                return $filesystem->glob($path.'*_'.$migrationFileName);
64
            })
65
            ->push($this->app->databasePath()."/migrations/{$timestamp}_{$migrationFileName}")
0 ignored issues
show
$this->app->databasePath....'_'.$migrationFileName of type string is incompatible with the type Illuminate\Support\TValue expected by parameter $values of Illuminate\Support\Collection::push(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            ->push(/** @scrutinizer ignore-type */ $this->app->databasePath()."/migrations/{$timestamp}_{$migrationFileName}")
Loading history...
66
            ->first();
67
    }
68
}
69