Completed
Push — develop ( 016805...3ba432 )
by Abdelrahman
01:08
created

SeederHelper::ensureExistingDatabaseTables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Support\Traits;
6
7
use Closure;
8
use Exception;
9
use Illuminate\Support\Facades\Schema;
10
use Illuminate\Database\Eloquent\Model;
11
12
trait SeederHelper
13
{
14
    /**
15
     * Seed the given resources.
16
     *
17
     * @param \Illuminate\Database\Eloquent\Model $model
18
     * @param string                              $seeder
19
     * @param array                               $initialExclude
20
     * @param \Closure                            $callback
0 ignored issues
show
Documentation introduced by
Should the type for parameter $callback not be null|Closure?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
21
     *
22
     * @throws \Exception
23
     *
24
     * @return void
25
     */
26
    protected function seedResources(Model $model, string $seeder, array $initialExclude = [], Closure $callback = null)
27
    {
28
        if (! file_exists($seeder)) {
29
            throw new Exception("Resources seeder file '{$seeder}' does NOT exist!");
30
        }
31
32
        $this->warn('Seeding: '.str_after($seeder, $this->laravel->basePath().'/'));
0 ignored issues
show
Bug introduced by
The property laravel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like warn() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33
34
        // Create new resources
35
        foreach (json_decode(file_get_contents($seeder), true) as $resource) {
36
            $model->firstOrCreate(array_except($resource, $initialExclude), array_only($resource, $initialExclude));
37
        }
38
39
        $this->info('Seeded: '.str_after($seeder, $this->laravel->basePath().'/'));
0 ignored issues
show
Bug introduced by
It seems like info() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40
41
        ! $callback || call_user_func($callback);
42
    }
43
44
    /**
45
     * Ensure existing database tables.
46
     *
47
     * @param string $package
48
     *
49
     * @return bool
50
     */
51
    protected function ensureExistingDatabaseTables(string $package)
52
    {
53
        if (! $this->hasDatabaseTables($package)) {
54
            $package = explode('/', $package);
55
            $this->call("{$package[0]}:migrate:{$package[1]}");
0 ignored issues
show
Bug introduced by
It seems like call() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
56
        }
57
58
        return true;
59
    }
60
61
    /**
62
     * Check if all required database tables exists.
63
     *
64
     * @param string $package
65
     *
66
     * @return bool
67
     */
68
    protected function hasDatabaseTables(string $package)
69
    {
70
        $package = explode('/', $package);
71
72
        foreach (config("{$package[0]}.{$package[1]}.tables") as $table) {
73
            if (! Schema::hasTable($table)) {
74
                return false;
75
            }
76
        }
77
78
        return true;
79
    }
80
}
81