Completed
Push — master ( 8f69e2...0f05a9 )
by ARCANEDEV
06:56
created

Factory::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
rs 9.9666
cc 2
nc 2
nop 4
crap 2.032
1
<?php namespace Arcanedev\LaravelMetrics\Expressions;
2
3
use Illuminate\Support\Arr;
4
use InvalidArgumentException;
5
6
/**
7
 * Class     Factory
8
 *
9
 * @package  Arcanedev\LaravelMetrics\Expressions
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Factory
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * The registered expressions.
21
     *
22
     * @var array
23
     */
24
    protected static $expressions = [
25
        'if_null' => [
26
            'mariadb' => IfNull\MySqlExpression::class,
27
            'mysql'   => IfNull\MySqlExpression::class,
28
            'pgsql'   => IfNull\PostgresExpression::class,
29
            'sqlite'  => IfNull\SqliteExpression::class,
30
        ],
31
    ];
32
33
    /* -----------------------------------------------------------------
34
     |  Properties
35
     | -----------------------------------------------------------------
36
     */
37
38
    /**
39
     * Make an expression.
40
     *
41
     * @param  string  $driver
42
     * @param  string  $name
43
     * @param  mixed   $value
44
     * @param  array   $params
45
     *
46
     * @return \Arcanedev\LaravelMetrics\Expressions\Expression|mixed
47
     */
48 4
    public static function make(string $driver, string $name, $value, array $params = []): Expression
49
    {
50 4
        $expression = Arr::get(static::$expressions, "{$name}.{$driver}");
51
52 4
        if (is_null($expression))
53
            throw new InvalidArgumentException("Expression `{$name}` not found for `{$driver}` driver");
54
55 4
        return new $expression($value, ...$params);
56
    }
57
}
58