Factory::make()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 4
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelMetrics\Expressions;
6
7
use Arcanedev\LaravelMetrics\Exceptions\ExpressionNotFound;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Traits\Macroable;
11
12
/**
13
 * Class     Factory
14
 *
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class Factory
18
{
19
    /* -----------------------------------------------------------------
20
     |  Traits
21
     | -----------------------------------------------------------------
22
     */
23
24 1
    use Macroable;
25
26
    /* -----------------------------------------------------------------
27
     |  Properties
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * The registered expressions.
33
     *
34
     * @var array
35
     */
36
    protected static $expressions = [
37
        'if_null' => [
38
            'mariadb' => IfNull\MySqlExpression::class,
39
            'mysql'   => IfNull\MySqlExpression::class,
40
            'pgsql'   => IfNull\PostgresExpression::class,
41
            'sqlite'  => IfNull\SqliteExpression::class,
42
            'sqlsrv'  => IfNull\SqlServerExpression::class,
43
        ],
44
45
        'trend_date_format' => [
46
            'mariadb' => TrendDateFormat\MySqlExpression::class,
47
            'mysql'   => TrendDateFormat\MySqlExpression::class,
48
            'pgsql'   => TrendDateFormat\PostgresExpression::class,
49
            'sqlite'  => TrendDateFormat\SqliteExpression::class,
50
            'sqlsrv'  => TrendDateFormat\SqlServerExpression::class,
51
        ],
52
    ];
53
54
    /* -----------------------------------------------------------------
55
     |  Properties
56
     | -----------------------------------------------------------------
57
     */
58
59
    /**
60
     * Make an expression.
61
     *
62
     * @param  string  $driver
63
     * @param  string  $name
64
     * @param  mixed   $value
65
     * @param  array   $params
66
     *
67
     * @return \Arcanedev\LaravelMetrics\Expressions\Expression|mixed
68
     */
69 272
    public static function make(string $driver, string $name, $value, array $params = []): Expression
70
    {
71 272
        $expression = Arr::get(static::$expressions, "{$name}.{$driver}");
72
73 272
        if (is_null($expression))
74 8
            throw ExpressionNotFound::make($name, $driver);
75
76
77 264
        return new $expression($value, ...$params);
78
    }
79
80
    /**
81
     * Resolve the expression.
82
     *
83
     * @param  \Illuminate\Database\Eloquent\Builder  $query
84
     * @param  string                                 $name
85
     * @param  mixed                                  $value
86
     * @param  array                                  $params
87
     *
88
     * @return \Arcanedev\LaravelMetrics\Expressions\Expression|mixed
89
     */
90 44
    public static function resolveExpression(Builder $query, string $name, $value, array $params = [])
91
    {
92 44
        $driver = $query->getConnection()->getDriverName();
93
94 44
        if (static::hasMacro($driver)) {
95 4
            return static::$driver($name, $value, $params);
96
        }
97
98 40
        return static::make($driver, $name, $value, $params);
99
    }
100
}
101