Helper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 15
c 1
b 0
f 0
dl 0
loc 70
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldTypeOptionsValue() 0 9 2
A validateOptionValue() 0 4 2
A validateOptions() 0 4 2
A isArray() 0 5 1
A isCarbonDate() 0 5 1
1
<?php
2
3
namespace OfflineAgency\MongoAutoSync\Traits;
4
5
use Exception;
6
use Illuminate\Support\Arr;
7
8
trait Helper
9
{
10
    /**
11
     * @param $options
12
     * @return bool|mixed
13
     *
14
     * @throws Exception
15
     */
16
    public function isArray($options)
17
    {
18
        $this->validateOptions($options);
19
20
        return $this->getFieldTypeOptionsValue($options, 'is-array', 'boolean');
21
    }
22
23
    /**
24
     * @param $options
25
     * @return bool|mixed
26
     *
27
     * @throws Exception
28
     */
29
    public function isCarbonDate($options)
30
    {
31
        $this->validateOptions($options);
32
33
        return $this->getFieldTypeOptionsValue($options, 'is-carbon-date', 'boolean');
34
    }
35
36
    /**
37
     * @param $options
38
     *
39
     * @throws Exception
40
     */
41
    private function validateOptions($options)
42
    {
43
        if (gettype($options) !== 'array') {
44
            throw new Exception($options.' is not a valid array!');
45
        }
46
    }
47
48
    /**
49
     * @param $value
50
     * @param  string  $expected
51
     *
52
     * @throws Exception
53
     */
54
    private function validateOptionValue($value, string $expected)
55
    {
56
        if (gettype($value) !== $expected) {
57
            throw new Exception($value.' is not a valid '.$expected.' found '.gettype($value).'! Check on your model configurations.');
58
        }
59
    }
60
61
    /**
62
     * @param  array  $options
63
     * @param  string  $key
64
     * @param  string  $expected
65
     * @return bool|mixed
66
     *
67
     * @throws Exception
68
     */
69
    private function getFieldTypeOptionsValue(array $options, string $key, string $expected)
70
    {
71
        if (Arr::has($options, $key)) {
72
            $value = Arr::get($options, $key);
73
            $this->validateOptionValue($value, $expected);
74
75
            return $value;
76
        } else {
77
            return false;
78
        }
79
    }
80
}
81