Skip to content
This repository was archived by the owner on Sep 11, 2018. It is now read-only.

Commit ec088ac

Browse files
committed
Fix slaves + env
1 parent 16af088 commit ec088ac

File tree

4 files changed

+86
-24
lines changed

4 files changed

+86
-24
lines changed

src/AlgoliaEloquentTrait.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function _browseFrom($query, $parameters = [], $cursor = null)
9292
*
9393
* @return mixed
9494
*/
95-
public function _browse($query, $parameters = array())
95+
public function _browse($query, $parameters = [])
9696
{
9797
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
9898
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
@@ -117,7 +117,7 @@ public function _browse($query, $parameters = array())
117117
*
118118
* @return mixed
119119
*/
120-
public function _search($query, $parameters = array())
120+
public function _search($query, $parameters = [])
121121
{
122122
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
123123
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
@@ -142,33 +142,40 @@ public function _setSettings()
142142
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
143143

144144
$settings = $modelHelper->getSettings($this);
145-
$slaves_settings = $modelHelper->getSlavesSettings($this);
146-
147145
$indices = $modelHelper->getIndices($this);
148146

147+
$slaves_settings = $modelHelper->getSlavesSettings($this);
149148
$slaves = isset($settings['slaves']) ? $settings['slaves'] : [];
150149

151150
$b = true;
152151

153152
/** @var \AlgoliaSearch\Index $index */
154153
foreach ($indices as $index) {
155-
$index->setSettings($settings);
156154

157-
if ($b) {
155+
if ($b && isset($settings['slaves'])) {
156+
$settings['slaves'] = array_map(function ($indexName) use ($modelHelper) {
157+
return $modelHelper->getFinalIndexName($this, $indexName);
158+
}, $settings['slaves']);
159+
}
160+
161+
if (count(array_keys($settings)) > 0) {
162+
$index->setSettings($settings);
163+
}
164+
165+
if ($b && isset($settings['slaves'])) {
158166
$b = false;
159167
unset($settings['slaves']);
160168
}
161169
}
162170

163-
if (count($slaves) > 0) {
164-
foreach ($slaves as $slave) {
165-
if (isset($slaves_settings[$slave])) {
166-
$index = $modelHelper->algolia->initIndex($slave);
171+
foreach ($slaves as $slave) {
172+
if (isset($slaves_settings[$slave])) {
173+
$index = $modelHelper->getIndices($this, $slave)[0];
167174

168-
$s = array_merge($settings, $slaves_settings[$slave]);
175+
$s = array_merge($settings, $slaves_settings[$slave]);
169176

177+
if (count(array_keys($s)) > 0)
170178
$index->setSettings($s);
171-
}
172179
}
173180
}
174181
}

src/ModelHelper.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,27 @@ public function getObjectIdKey(Model $model)
5555

5656
public function getSettings(Model $model)
5757
{
58-
return property_exists($model, 'algoliaSettings') ? $model->algoliaSettings : array();
58+
return property_exists($model, 'algoliaSettings') ? $model->algoliaSettings : [];
5959
}
6060

6161
public function getSlavesSettings(Model $model)
6262
{
63-
return property_exists($model, 'slavesSettings') ? $model->slavesSettings : array();
63+
return property_exists($model, 'slavesSettings') ? $model->slavesSettings : [];
64+
}
65+
66+
public function getFinalIndexName(Model $model, $indexName)
67+
{
68+
$env_suffix = property_exists($model, 'perEnvironment') && $model::$perEnvironment === true ? '_'.\App::environment() : '';
69+
70+
return $indexName.$env_suffix;
6471
}
6572

6673
/**
6774
* @return \AlgoliaSearch\Index
6875
*/
6976
public function getIndices(Model $model, $indexName = null)
7077
{
71-
$indicesName = array();
78+
$indicesName = [];
7279

7380
if ($indexName !== null) {
7481
$indicesName[] = $indexName;
@@ -78,29 +85,25 @@ public function getIndices(Model $model, $indexName = null)
7885
$indicesName[] = $this->getIndexName($model);
7986
}
8087

81-
$env_suffix = property_exists($model, 'perEnvironment') && $model::$perEnvironment === true ? '_'.\App::environment() : '';
82-
83-
$indices = array_map(function ($index_name) use ($env_suffix) {
84-
return $this->algolia->initIndex($index_name.$env_suffix);
88+
$indices = array_map(function ($index_name) use ($model) {
89+
return $this->algolia->initIndex($this->getFinalIndexName($model, $index_name));
8590
}, $indicesName);
8691

8792
return $indices;
8893
}
8994

9095
public function getIndicesTmp(Model $model)
9196
{
92-
$indicesName = array();
97+
$indicesName = [];
9398

9499
if (property_exists($model, 'indices') && is_array($model->indices)) {
95100
$indicesName = $model->indices;
96101
} else {
97102
$indicesName[] = $this->getIndexName($model);
98103
}
99104

100-
$env_suffix = property_exists($model, 'perEnvironment') && $model::$perEnvironment === true ? '_'.\App::environment() : '';
101-
102-
$indices = array_map(function ($index_name) use ($env_suffix) {
103-
return $this->algolia->initIndex($index_name.$env_suffix.'_tmp');
105+
$indices = array_map(function ($index_name) use ($model) {
106+
return $this->algolia->initIndex($this->getFinalIndexName($index_name).'_tmp');
104107
}, $indicesName);
105108

106109
return $indices;

tests/AlgoliaEloquentTraitTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use AlgoliaSearch\Tests\Models\Model2;
66
use AlgoliaSearch\Tests\Models\Model4;
7+
use AlgoliaSearch\Tests\Models\Model6;
78
use Illuminate\Support\Facades\App;
89
use Illuminate\Support\Facades\Config;
910
use Mockery;
@@ -68,6 +69,30 @@ public function testRemoveFromIndex()
6869
$this->assertEquals(null, $model4->removeFromIndex());
6970
}
7071

72+
public function testSetSettings()
73+
{
74+
$index = Mockery::mock('\AlgoliaSearch\Index');
75+
$index->shouldReceive('setSettings')->with(array('slaves' => array('model_6_desc_testing')));
76+
$index->shouldReceive('setSettings')->with(array('ranking' => array('desc(name)')));
77+
78+
/** @var \AlgoliaSearch\Laravel\ModelHelper $realModelHelper */
79+
$realModelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
80+
$modelHelper = Mockery::mock('\AlgoliaSearch\Laravel\ModelHelper');
81+
82+
App::instance('\AlgoliaSearch\Laravel\ModelHelper', $modelHelper);
83+
84+
$model6 = new Model6();
85+
$modelHelper->shouldReceive('getSettings')->andReturn($realModelHelper->getSettings($model6));
86+
$modelHelper->shouldReceive('getIndices')->andReturn([$index]);
87+
$modelHelper->shouldReceive('getFinalIndexName')->andReturn($realModelHelper->getFinalIndexName($model6, 'model_6_desc'));
88+
$modelHelper->shouldReceive('getSlavesSettings')->andReturn($realModelHelper->getSlavesSettings($model6));
89+
90+
$settings = $realModelHelper->getSettings($model6);
91+
$this->assertEquals($modelHelper->getFinalIndexName($model6, $settings['slaves'][0]), 'model_6_desc_testing');
92+
93+
$model6->setSettings();
94+
}
95+
7196
public function tearDown()
7297
{
7398
Mockery::close();

tests/Models/Model6.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace AlgoliaSearch\Tests\Models;
4+
5+
use AlgoliaSearch\Laravel\AlgoliaEloquentTrait;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Model6 extends Model
9+
{
10+
use AlgoliaEloquentTrait;
11+
12+
public static $perEnvironment = true;
13+
14+
public $algoliaSettings = [
15+
'slaves' => [
16+
'model_6_desc',
17+
],
18+
];
19+
20+
public $slavesSettings = [
21+
'model_6_desc' => [
22+
'ranking' => [
23+
'desc(name)'
24+
]
25+
]
26+
];
27+
}

0 commit comments

Comments
 (0)