Test Failed
Push — master ( c42c77...46f64b )
by Phan
08:21
created

createParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1.0001

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 23
ccs 18
cts 19
cp 0.9474
rs 9.7
cc 1
nc 1
nop 3
crap 1.0001
1
<?php
2
3
namespace App\Factories;
4
5
use App\Models\Rule;
6
use Carbon\Carbon;
7
use InvalidArgumentException;
8
use Throwable;
9
10
class SmartPlaylistRuleParameterFactory
11 13
{
12
    /**
13
     * @param mixed[]  $value
14 13
     *
15 1
     * @return array
16 12
     * @throws Throwable
17 1
     */
18 11
    public function createParameters(string $model, string $operator, array $value): array
19 4
    {
20 9
        $ruleParameterMap = [
21 1
            Rule::OPERATOR_BEGINS_WITH => [$model, 'LIKE', "{$value[0]}%"],
22 8
            Rule::OPERATOR_ENDS_WITH => [$model, 'LIKE', "%{$value[0]}"],
23 1
            Rule::OPERATOR_IS => [$model, '=', $value[0]],
24 7
            Rule::OPERATOR_IS_NOT =>  [$model, '<>', $value[0]],
25 3
            Rule::OPERATOR_CONTAINS => [$model, 'LIKE', "%{$value[0]}%"],
26 5
            Rule::OPERATOR_NOT_CONTAIN => [$model, 'NOT LIKE', "%{$value[0]}%"],
27 1
            Rule::OPERATOR_IS_LESS_THAN => [$model, '<', $value[0]],
28 4
            Rule::OPERATOR_IS_GREATER_THAN =>  [$model, '>', $value[0]],
29 1
            Rule::OPERATOR_IS_BETWEEN => [$model, $value],
30 3
            Rule::OPERATOR_NOT_IN_LAST => [$model, '<', (new Carbon())->subDay($value[0])],
31 1
            Rule::OPERATOR_IN_LAST => [$model, '>=', (new Carbon())->subDay($value[0])],
32 2
        ];
33 1
34 1
        throw_unless(array_key_exists($operator, $ruleParameterMap), InvalidArgumentException::class, sprintf(
35 1
            'Invalid operator %s. Valid operators are: %s.',
36
            $operator,
37
            implode(', ', array_keys($ruleParameterMap))
38
        ));
39
40
        return $ruleParameterMap[$operator];
41
    }
42
}
43