Completed
Pull Request — master (#12)
by Adam
04:47 queued 01:24
created

SearchTrait::setup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 4
cts 7
cp 0.5714
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2.3149
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Laravel;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\App;
7
8
/**
9
 * Class SearchTrait
10
 * @package BestServedCold\LaravelZendSearch\Laravel
11
 */
12
trait SearchTrait
13
{
14
    /**
15
     * @var array $searchFields
16
     */
17
    private static $searchFields = [];
18
19
    /**
20
     * @var array $boostFields
21
     */
22
    private static $boostFields = [];
23
24
    /**
25
     * Set up
26
     *
27
     * @throws \Exception
28
     * @return void
29
     */
30 15
    private static function setup()
31
    {
32
        // Ignoring PHP bug #53727 here, Eloquent Models implement several interfaces.
33 15
        if (!is_subclass_of(static::class, Model::class)) {
34
            throw new \Exception(
35
                'SearchTrait must only be used with Eloquent models, ['.get_called_class().'] used.'
36
            );
37
        }
38
39 15
        static::searchFields();
40 15
    }
41
42
    /**
43
     * Search Fields
44
     *
45
     * This should never get called.  If it does get called, then it means that the Model which is using
46
     * "SearchTrait" has not declared a "searchFields" method and made it static.
47
     *
48
     * @throws \Exception
49
     */
50
    private static function searchFields()
51
    {
52
        throw new \Exception("Method [searchFields] must exist and be static.");
53
    }
54
55
    /**
56
     * @param array $fields
57
     */
58 15
    public static function setSearchFields(array $fields)
59
    {
60 15
        self::$searchFields = $fields;
61 15
    }
62
63
    /**
64
     * @return array
65
     */
66
    public static function getSearchFields()
67
    {
68
        return self::$searchFields;
69
    }
70
71
    /**
72
     * @param array $fields
73
     * @throws \Exception
74
     */
75
    public static function setBoostFields(array $fields)
76
    {
77
        if (!array_filter($fields, function($value) {
78
            return is_int($value) || is_float($value);
79
        })) {
80
            throw new \Exception('Boost field values must be integers or floats.');
81
        }
82
        self::$boostFields = $fields;
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public static function getBoostFields()
89
    {
90
        return self::$boostFields;
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    public static function search()
97
    {
98
        self::setup();
99
100
        $search = App::make(Search::class);
101
        $search->model(new static);
102
103
        return $search;
104
    }
105
106
    /**
107
     * @return void
108
     */
109 15
    public static function bootSearchTrait()
110
    {
111 15
        self::setup();
112 15
        $store = App::make(Store::class);
113
        self::saved(self::insertCallback($store));
114
        self::deleting(self::deleteCallback($store));
115
    }
116
117
    /**
118
     * @param Store $store
119
     * @return \Closure
120
     */
121
    private static function insertCallback(Store $store)
122
    {
123
        return function(Model $model) use ($store) {
124
            $store->model($model);
125
            $store->insertModel($model);
126
        };
127
    }
128
129
    /**
130
     * @param Store $store
131
     * @return \Closure
132
     */
133
    private static function deleteCallback(Store $store)
134
    {
135
        return function(Model $model) use ($store) {
136
            $store->model($model);
137
            $store->deleteModel($model);
138
        };
139
    }
140
}
141