UsesBlueprints::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Schema\Concerns;
6
7
use Closure;
8
use LaravelFreelancerNL\Aranguent\Schema\Blueprint;
9
10
trait UsesBlueprints
11
{
12
    /**
13
     * Create a new command set with a Closure.
14
     *
15
     * @param  string  $collection
16
     * @return Blueprint
17
     */
18 90
    protected function createBlueprint($table, Closure $callback = null)
19
    {
20 90
        $prefix = $this->connection->getConfig('prefix_indexes')
21
            ? $this->connection->getConfig('prefix')
22 90
            : '';
23
24 90
        if (isset($this->resolver)) {
25
            return call_user_func($this->resolver, $table, $this->grammar, $this->schemaManager, $callback, $prefix);
26
        }
27
28 90
        return new Blueprint($table, $this->grammar, $this->schemaManager, $callback, $prefix);
29
    }
30
31
    /**
32
     * Set the Schema Blueprint resolver callback.
33
     *
34
     *
35
     * @return void
36
     */
37
    public function blueprintResolver(Closure $resolver)
38
    {
39
        $this->resolver = $resolver;
0 ignored issues
show
Bug Best Practice introduced by
The property resolver does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
    }
41
42 90
    protected function build($blueprint)
43
    {
44 90
        $blueprint->build($this->connection, $this->grammar);
45
    }
46
47
    /**
48
     * Create a new collection on the schema.
49
     *
50
     * @param  array<mixed>  $config
51
     */
52 67
    public function create($table, Closure $callback, array $config = []): void
53
    {
54 67
        $this->build(tap($this->createBlueprint($table), function ($blueprint) use ($callback, $config) {
55 67
            $blueprint->create($config);
56
57 67
            $callback($blueprint);
58 67
        }));
59
    }
60
61
    /**
62
     * Modify a table's schema.
63
     *
64
     * @param  string  $table
65
     * @return void
66
     */
67 82
    public function table($table, Closure $callback)
68
    {
69 82
        $this->build($this->createBlueprint($table, $callback));
70
    }
71
}
72