Completed
Branch master (3a11a5)
by Adam
06:08 queued 02:35
created

SearchTrait::deleteCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
    private static $searchFields = [ ];
15
16
    private static function setup()
17
    {
18
        if (!is_subclass_of(static::class, Model::class)) {
1 ignored issue
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Illuminate\Database\Eloquent\Model::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
19
            throw new \Exception(
20
                'SearchTrait must only be used with Eloquent models, [' . get_called_class() . '] used'
21
            );
22
        }
23
24
        static::searchFields();
25
    }
26
27
    private static function searchFields()
28
    {
29
        throw new \Exception("Method [searchFields] must exist and be static");
30
    }
31
32
    /**
33
     * @param array $fields
34
     */
35
    public static function setSearchFields(array $fields)
36
    {
37
        self::$searchFields = $fields;
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public static function getSearchFields()
44
    {
45
        return self::$searchFields;
46
    }
47
48
    /**
49
     * @return mixed
50
     */
51
    public static function search()
52
    {
53
        self::setup();
54
55
        $search = App::make(Search::class);
56
        $search->model(new static);
57
58
        return $search;
59
    }
60
61
    public static function bootSearchTrait()
62
    {
63
        self::setup();
64
65
        $store = App::make(Store::class);
66
67
        self::saved(
68
            function(Model $model) use ($store) {
69
                self::insertCallback($model, $store);
70
            }
71
        );
72
73
        self::deleting(
74
            function(Model $model) use ($store) {
75
                self::deleteCallback($model, $store);
76
            }
77
        );
78
    }
79
80
    /**
81
     * @param Model $model
82
     * @param Store $store
83
     */
84
    private static function insertCallback(Model $model, Store $store)
85
    {
86
        $store->model($model);
87
        $store->insertModel($model);
88
    }
89
90
    /**
91
     * @param Model $model
92
     * @param Store $store
93
     */
94
    private static function deleteCallback(Model $model, Store $store)
95
    {
96
        $store->model($model);
97
        $store->deleteModel($model);
98
    }
99
}
100