AttachPartitionCompiler::formatValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 5
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
c 2
b 1
f 1
cc 3
nc 3
nop 1
crap 3
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