ExcludeCompiler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 29
dl 0
loc 67
ccs 36
cts 36
cp 1
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A compileWheres() 0 9 2
A compileWith() 0 12 2
A compileMethod() 0 7 2
A compileExclude() 0 8 1
A compileTablespace() 0 7 2
A compile() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\Postgres\Compilers;
6
7
use Illuminate\Database\Schema\Blueprint;
8
use Illuminate\Database\Schema\Grammars\Grammar;
9
use Illuminate\Support\Fluent;
10
use Umbrellio\Postgres\Compilers\Traits\WheresBuilder;
11
use Umbrellio\Postgres\Schema\Builders\Constraints\Exclude\ExcludeBuilder;
12
13
class ExcludeCompiler
14
{
15
    use WheresBuilder;
16
17 6
    public static function compile(Grammar $grammar, Blueprint $blueprint, ExcludeBuilder $command): string
18
    {
19 6
        return implode(' ', array_filter([
20 6
            sprintf('ALTER TABLE %s ADD CONSTRAINT %s EXCLUDE', $blueprint->getTable(), $command->get('index')),
21 6
            static::compileMethod($command),
22 6
            sprintf('(%s)', static::compileExclude($command)),
23 6
            static::compileWith($command),
24 6
            static::compileTablespace($command),
25 6
            static::compileWheres($grammar, $blueprint, $command),
26 6
        ]));
27
    }
28
29 6
    private static function compileExclude(Fluent $command): string
30
    {
31 6
        $items = collect($command->get('using'))
32 6
            ->map(static function ($operator, $excludeElement) {
33 6
                return sprintf('%s WITH %s', $excludeElement, $operator);
34 6
            });
35
36 6
        return implode(', ', $items->toArray());
37
    }
38
39 6
    private static function compileWith(Fluent $command): ?string
40
    {
41 6
        $items = collect($command->get('with'))
42 6
            ->map(static function ($value, $storageParameter) {
43 1
                return sprintf('%s = %s', $storageParameter, static::wrapValue($value));
44 6
            });
45
46 6
        if ($items->count() > 0) {
47 1
            return sprintf('WITH (%s)', implode(', ', $items->toArray()));
48
        }
49
50 5
        return null;
51
    }
52
53 6
    private static function compileTablespace(Fluent $command): ?string
54
    {
55 6
        if ($command->get('tableSpace')) {
56 1
            return sprintf('USING INDEX TABLESPACE %s', $command->get('tableSpace'));
57
        }
58
59 5
        return null;
60
    }
61
62 6
    private static function compileMethod(Fluent $command): ?string
63
    {
64 6
        if ($command->get('method')) {
65 2
            return sprintf('USING %s', $command->get('method'));
66
        }
67
68 4
        return null;
69
    }
70
71 6
    private static function compileWheres(Grammar $grammar, Blueprint $blueprint, Fluent $command): ?string
72
    {
73 6
        $wheres = static::build($grammar, $blueprint, $command);
74
75 6
        if (! empty($wheres)) {
76 3
            return sprintf('WHERE %s', static::removeLeadingBoolean(implode(' ', $wheres)));
77
        }
78
79 3
        return null;
80
    }
81
}
82