Completed
Push — master ( e75277...6d67f1 )
by Juuso
05:02
created

Searchable::reindex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace leinonen\Yii2Algolia\ActiveRecord;
4
5
use leinonen\Yii2Algolia\AlgoliaManager;
6
use Yii;
7
8
/**
9
 * Simple trait that implements methods needed for the SearchableInterface + couple helpers for dealing with ActiveRecords.
10
 */
11
trait Searchable
12
{
13
    /**
14
     * @see \yii\base\ArrayableTrait::toArray()
15
     *
16
     * @param array $fields
17
     * @param array $expand
18
     * @param bool $recursive
19
     */
20
    abstract public function toArray(array $fields = [], array $expand = [], $recursive = true);
21
22
    /**
23
     * @see \yii\db\BaseActiveRecord::getPrimaryKey()
24
     *
25
     * @param bool $asArray
26
     */
27
    abstract public function getPrimaryKey($asArray = false);
28
29
    /**
30
     * An array of indices that this model uses. If none specified
31
     * The name of the model will be used as the index.
32
     *
33
     * @return array
34
     */
35 3
    public function indices()
36
    {
37 3
        return [];
38
    }
39
40
    /**
41
     * Returns an array of indices for this model.
42
     *
43
     * @return array
44
     */
45 4
    public function getIndices()
46
    {
47 4
        $indices = $this->indices();
48
49 4
        if (empty($indices)) {
50 3
            $className = (new \ReflectionClass($this))->getShortName();
51
52 3
            return [$className];
53
        }
54
55 1
        return $indices;
56
    }
57
58
    /**
59
     * Returns the model in algolia friendly array form.
60
     *
61
     * @return array
62
     */
63 4
    public function getAlgoliaRecord()
64
    {
65 4
        $record = $this->toArray();
66
67 4
        return $record;
68
    }
69
70
    /**
71
     * Returns an unique identifier for the Model.
72
     *
73
     * @return int
74
     */
75 1
    public function getObjectID()
76
    {
77 1
        return $this->getPrimaryKey();
78
    }
79
80
    /**
81
     * Indexes the model to Algolia.
82
     */
83 1
    public function index()
84
    {
85 1
        $manager = $this->getAlgoliaManager();
86
87 1
        return $manager->pushToIndices($this);
88
    }
89
90
    /**
91
     * Removes the model from Algolia.
92
     *
93
     * @throws \Exception
94
     */
95 1
    public function removeFromIndices()
96
    {
97 1
        $manager = $this->getAlgoliaManager();
98
99 1
        $manager->removeFromIndices($this);
100 1
    }
101
102
    /**
103
     * Updates the model in Algolia.
104
     */
105 1
    public function updateInIndices()
106
    {
107 1
        $manager = $this->getAlgoliaManager();
108
109 1
        $manager->updateInIndices($this);
110 1
    }
111
112
    /**
113
     * Re-indexes the indices safely for this ActiveRecord class.
114
     */
115 1
    public static function reindex()
116
    {
117 1
        $manager = static::getAlgoliaManager();
118
        
119 1
        $manager->reindex(__CLASS__);
120 1
    }
121
122
    /**
123
     * Clears the indices for this ActiveRecord class.
124
     */
125 1
    public static function clearIndices()
126
    {
127 1
        $manager = static::getAlgoliaManager();
128
129 1
        $manager->clearIndices(__CLASS__);
130 1
    }
131
132
    /**
133
     * Returns the AlgoliaManager Instance.
134
     *
135
     * @return AlgoliaManager
136
     * @throws \yii\base\InvalidConfigException
137
     */
138 5
    private static function getAlgoliaManager()
139
    {
140 5
        return Yii::$container->get(AlgoliaManager::class);
141
    }
142
}
143