AttachPartitionCompiler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 6
eloc 17
dl 0
loc 35
ccs 20
cts 20
cp 1
rs 10
c 2
b 1
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compileForValues() 0 10 2
A formatValue() 0 11 3
A compile() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\Postgres\Compilers;
6
7
use Carbon\Carbon;
8
use Illuminate\Database\Schema\Blueprint;
9
use Illuminate\Database\Schema\Grammars\Grammar;
10
use Illuminate\Support\Fluent;
11
use InvalidArgumentException;
12
13
class AttachPartitionCompiler
14
{
15 4
    public static function compile(Grammar $grammar, Blueprint $blueprint, Fluent $command): string
16
    {
17 4
        return sprintf(
18 4
            'alter table %s attach partition %s %s',
19 4
            $grammar->wrapTable($blueprint),
20 4
            $command->get('partition'),
21 4
            self::compileForValues($command)
22 4
        );
23
    }
24
25 4
    private static function compileForValues(Fluent $command): string
26
    {
27 4
        $range = $command->get('range');
28 4
        if ($range) {
29 3
            $from = self::formatValue($range['from']);
30 3
            $to = self::formatValue($range['to']);
31 3
            return "for values from ({$from}) to ({$to})";
32
        }
33
34 1
        throw new InvalidArgumentException('Not set "for values" for attachPartition');
35
    }
36
37 3
    private static function formatValue($value)
38
    {
39 3
        if ($value instanceof Carbon) {
40 1
            return "'{$value->toDateTimeString()}'";
41
        }
42
43 2
        if (is_string($value)) {
44 1
            return "'{$value}'";
45
        }
46
47 1
        return $value;
48
    }
49
}
50