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

Commit d9325a2

Browse files
committed
Add PSR-2:ish styled syntax
1 parent 1371009 commit d9325a2

File tree

11 files changed

+190
-142
lines changed

11 files changed

+190
-142
lines changed

src/AlgoliaEloquentTrait.php

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,84 @@
1-
<?php namespace AlgoliaSearch\Laravel;
1+
<?php
2+
3+
namespace AlgoliaSearch\Laravel;
24

35
use Illuminate\Support\Facades\App;
46

5-
Trait AlgoliaEloquentTrait
7+
trait AlgoliaEloquentTrait
68
{
9+
/**
10+
* @var string
11+
*/
712
private static $methodGetName = 'getAlgoliaRecord';
813

914
/**
10-
* Static calls
15+
* Static calls.
16+
*
17+
* @param bool $safe
1118
*/
12-
1319
public function _reindex($safe = true)
1420
{
1521
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
16-
$modelHelper = \App::make('\AlgoliaSearch\Laravel\ModelHelper');
22+
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
1723

1824
$indices = $modelHelper->getIndices($this);
1925
$indicesTmp = $safe ? $modelHelper->getIndicesTmp($this) : $indices;
2026

2127
static::chunk(100, function ($models) use ($indicesTmp, $modelHelper) {
2228
/** @var \AlgoliaSearch\Index $index */
23-
foreach ($indicesTmp as $index)
24-
{
29+
foreach ($indicesTmp as $index) {
2530
$records = [];
2631

27-
foreach ($models as $model)
28-
if ($modelHelper->indexOnly($model, $index->indexName))
32+
foreach ($models as $model) {
33+
if ($modelHelper->indexOnly($model, $index->indexName)) {
2934
$records[] = $model->getAlgoliaRecordDefault();
35+
}
36+
}
3037

3138
$index->addObjects($records);
3239
}
3340

3441
});
3542

36-
if ($safe)
37-
for ($i = 0; $i < count($indices); $i++)
43+
if ($safe) {
44+
for ($i = 0; $i < count($indices); $i++) {
3845
$modelHelper->algolia->moveIndex($indicesTmp[$i]->indexName, $indices[0]->indexName);
46+
}
47+
}
3948
}
4049

4150
public function _clearIndices()
4251
{
43-
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
44-
$modelHelper = \App::make('\AlgoliaSearch\Laravel\ModelHelper');
52+
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
53+
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
4554

4655
$indices = $modelHelper->getIndices($this);
4756

4857
/** @var \AlgoliaSearch\Index $index */
49-
foreach ($indices as $index)
58+
foreach ($indices as $index) {
5059
$index->clearIndex();
60+
}
5161
}
5262

63+
/**
64+
* @param $query
65+
* @param array $parameters
66+
*
67+
* @return mixed
68+
*/
5369
public function _search($query, $parameters = [])
5470
{
55-
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
56-
$modelHelper = \App::make('\AlgoliaSearch\Laravel\ModelHelper');
71+
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
72+
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
5773

5874
$index = null;
5975

60-
if (isset($parameters['index']))
61-
{
76+
if (isset($parameters['index'])) {
6277
$index = $modelHelper->getIndex($parameters['index']);
6378
unset($parameters['index']);
64-
}
65-
else
79+
} else {
6680
$index = $modelHelper->getIndices($this)[0];
81+
}
6782

6883
$result = $index->search($query, ['hitsPerPage' => 0]);
6984

@@ -72,8 +87,8 @@ public function _search($query, $parameters = [])
7287

7388
public function _setSettings()
7489
{
75-
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
76-
$modelHelper = \App::make('\AlgoliaSearch\Laravel\ModelHelper');
90+
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
91+
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
7792

7893
$settings = $modelHelper->getSettings($this);
7994
$slaves_settings = $modelHelper->getSlavesSettings($this);
@@ -85,23 +100,18 @@ public function _setSettings()
85100
$b = true;
86101

87102
/** @var \AlgoliaSearch\Index $index */
88-
foreach ($indices as $index)
89-
{
103+
foreach ($indices as $index) {
90104
$index->setSettings($settings);
91105

92-
if ($b)
93-
{
106+
if ($b) {
94107
$b = false;
95108
unset($settings['slaves']);
96109
}
97110
}
98111

99-
if (count($slaves) > 0)
100-
{
101-
foreach ($slaves as $slave)
102-
{
103-
if (isset($slaves_settings[$slave]))
104-
{
112+
if (count($slaves) > 0) {
113+
foreach ($slaves as $slave) {
114+
if (isset($slaves_settings[$slave])) {
105115
$index = $modelHelper->algolia->initIndex($slave);
106116

107117
$s = array_merge($settings, $slaves_settings[$slave]);
@@ -112,63 +122,67 @@ public function _setSettings()
112122
}
113123
}
114124

115-
116125
public static function __callStatic($method, $parameters)
117126
{
118127
$instance = new static();
119128

120129
$method = '_'.$method;
121130

122131
if (method_exists($instance, $method));
123-
return call_user_func_array([$instance, $method], $parameters);
132+
133+
return call_user_func_array([$instance, $method], $parameters);
124134

125135
return parent::__callStatic($method, $parameters);
126136
}
127137

128138
/**
129-
* Methods
139+
* Methods.
130140
*/
131-
132141
public function getAlgoliaRecordDefault()
133142
{
134-
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
135-
$modelHelper = \App::make('\AlgoliaSearch\Laravel\ModelHelper');
143+
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
144+
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
136145

137146
$record = null;
138147

139-
if (method_exists($this, static::$methodGetName))
148+
if (method_exists($this, static::$methodGetName)) {
140149
$record = $this->{static::$methodGetName}();
141-
else
150+
} else {
142151
$record = $this->toArray();
152+
}
143153

144-
if (isset($record['objectID']) == false)
154+
if (isset($record['objectID']) == false) {
145155
$record['objectID'] = $modelHelper->getObjectId($this);
156+
}
146157

147158
return $record;
148159
}
149160

150161
public function pushToindex()
151162
{
152-
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
153-
$modelHelper = \App::make('\AlgoliaSearch\Laravel\ModelHelper');
163+
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
164+
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
154165

155166
$indices = $modelHelper->getIndices($this);
156167

157168
/** @var \AlgoliaSearch\Index $index */
158-
foreach ($indices as $index)
159-
if ($modelHelper->indexOnly($this, $index->indexName))
169+
foreach ($indices as $index) {
170+
if ($modelHelper->indexOnly($this, $index->indexName)) {
160171
$index->addObject($this->getAlgoliaRecordDefault());
172+
}
173+
}
161174
}
162175

163176
public function removeFromIndex()
164177
{
165-
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
166-
$modelHelper = \App::make('\AlgoliaSearch\Laravel\ModelHelper');
178+
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
179+
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
167180

168181
$indices = $modelHelper->getIndices($this);
169182

170183
/** @var \AlgoliaSearch\Index $index */
171-
foreach ($indices as $index)
184+
foreach ($indices as $index) {
172185
$index->deleteObject($modelHelper->getObjectId($this));
186+
}
173187
}
174188
}

src/AlgoliaServiceProvider.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
<?php namespace AlgoliaSearch\Laravel;
1+
<?php
2+
3+
namespace AlgoliaSearch\Laravel;
24

3-
use Illuminate\Support\ServiceProvider;
45
use Illuminate\Support\Facades\Event;
6+
use Illuminate\Support\ServiceProvider;
57

68
class AlgoliaServiceProvider extends ServiceProvider
79
{
@@ -21,7 +23,7 @@ public function provides()
2123
{
2224
return [
2325
'algolia',
24-
'algolia.factory'
26+
'algolia.factory',
2527
];
2628
}
27-
}
29+
}

src/EloquentSubscriber.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace AlgoliaSearch\Laravel;
1+
<?php
2+
3+
namespace AlgoliaSearch\Laravel;
24

35
class EloquentSubscriber
46
{
@@ -11,25 +13,30 @@ public function __construct(ModelHelper $modelHelper)
1113

1214
public function saved($model)
1315
{
14-
if (! $this->modelHelper->isAutoIndex($model))
16+
if (!$this->modelHelper->isAutoIndex($model)) {
1517
return true;
18+
}
1619

1720
/** @var \AlgoliaSearch\Index $index */
18-
foreach ($this->modelHelper->getIndices($model) as $index);
19-
if ($this->modelHelper->indexOnly($model, $index->indexName))
21+
foreach ($this->modelHelper->getIndices($model) as $index) {
22+
if ($this->modelHelper->indexOnly($model, $index->indexName)) {
2023
$index->addObject($this->modelHelper->getAlgoliaRecord($model), $this->modelHelper->getKey($model));
24+
}
25+
}
2126

2227
return true;
2328
}
2429

2530
public function deleted($model)
2631
{
27-
if (! $this->modelHelper->isAutoDelete($model))
32+
if (!$this->modelHelper->isAutoDelete($model)) {
2833
return true;
34+
}
2935

3036
/** @var \AlgoliaSearch\Index $index */
31-
foreach ($this->modelHelper->getIndices($model) as $index);
37+
foreach ($this->modelHelper->getIndices($model) as $index) {
3238
$index->deleteObject($model->id);
39+
}
3340

3441
return true;
3542
}

src/ModelHelper.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace AlgoliaSearch\Laravel;
1+
<?php
2+
3+
namespace AlgoliaSearch\Laravel;
24

35
use Illuminate\Database\Eloquent\Model;
46
use Vinkla\Algolia\AlgoliaManager;
@@ -29,7 +31,7 @@ public function isAutoIndex(Model $model)
2931

3032
public function isAutoDelete(Model $model)
3133
{
32-
return ($this->hasAlgoliaTrait($model) && (property_exists($model, 'auto_delete') == false ||$model::$auto_delete === true));
34+
return ($this->hasAlgoliaTrait($model) && (property_exists($model, 'auto_delete') == false || $model::$auto_delete === true));
3335
}
3436

3537
public function getKey(Model $model)
@@ -39,7 +41,7 @@ public function getKey(Model $model)
3941

4042
public function indexOnly(Model $model, $index_name)
4143
{
42-
return ! method_exists($model, 'indexOnly') || $model->indexOnly($index_name);
44+
return !method_exists($model, 'indexOnly') || $model->indexOnly($index_name);
4345
}
4446

4547
public function getObjectId(Model $model)
@@ -69,14 +71,15 @@ public function getIndices(Model $model)
6971
{
7072
$indicesName = [];
7173

72-
if (property_exists($model, 'indices') && is_array($model->indices))
74+
if (property_exists($model, 'indices') && is_array($model->indices)) {
7375
$indicesName = $model->indices;
74-
else
76+
} else {
7577
$indicesName[] = $this->getIndexName($model);
78+
}
7679

77-
$env_suffix = property_exists($model, 'per_environment') && $model->per_environment === true ? '_' . \App::environment() : '';
80+
$env_suffix = property_exists($model, 'per_environment') && $model->per_environment === true ? '_'.\App::environment() : '';
7881

79-
$indices = array_map(function ($index_name) use($env_suffix) {
82+
$indices = array_map(function ($index_name) use ($env_suffix) {
8083
return $this->algolia->initIndex($index_name.$env_suffix);
8184
}, $indicesName);
8285

@@ -87,15 +90,16 @@ public function getIndicesTmp(Model $model)
8790
{
8891
$indicesName = [];
8992

90-
if (property_exists($model, 'indices') && is_array($model->indices))
93+
if (property_exists($model, 'indices') && is_array($model->indices)) {
9194
$indicesName = $model->indices;
92-
else
95+
} else {
9396
$indicesName[] = $this->getIndexName($model);
97+
}
9498

95-
$env_suffix = property_exists($model, 'per_environment') && $model->per_environment === true ? '_' . \App::environment() : '';
99+
$env_suffix = property_exists($model, 'per_environment') && $model->per_environment === true ? '_'.\App::environment() : '';
96100

97-
$indices = array_map(function ($index_name) use($env_suffix) {
98-
return $this->algolia->initIndex($index_name.$env_suffix."_tmp");
101+
$indices = array_map(function ($index_name) use ($env_suffix) {
102+
return $this->algolia->initIndex($index_name.$env_suffix.'_tmp');
99103
}, $indicesName);
100104

101105
return $indices;

0 commit comments

Comments
 (0)