SearchTrait::setup()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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