Skip to content

Commit e3f217b

Browse files
Improved all the things!
1 parent bab37af commit e3f217b

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/Console/AlgoliaCommand.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
use AlgoliaSearch\Client;
66
use Illuminate\Console\Command;
77
use Illuminate\Support\Facades\File;
8+
use Laravel\Scout\Searchable;
89

910

1011
class AlgoliaCommand extends Command
1112
{
13+
protected $path;
14+
1215
public function __construct()
1316
{
1417
parent::__construct();
1518

19+
$this->path = resource_path(env('ALGOLIA_SETTINGS_FOLDER', 'algolia-settings/'));
20+
1621
// Ensure settings directory exists
17-
if (! File::exists(resource_path('settings/'))) {
22+
if (! File::exists($this->path)) {
1823
File::makeDirectory();
1924
}
2025
}
@@ -26,4 +31,14 @@ protected function getIndex($indexName)
2631
config('scout.algolia.secret'))
2732
)->initIndex($indexName);
2833
}
34+
35+
protected function isClassSearchable($class)
36+
{
37+
if (!in_array(Searchable::class, class_uses($class))) {
38+
$this->warn('The class [' . $class . '] does not use the [' . Searchable::class . '] trait');
39+
return false;
40+
}
41+
42+
return true;
43+
}
2944
}

src/Console/BackupCommand.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,23 @@ public function handle()
2929
{
3030
$class = $this->argument('model');
3131

32+
if (! $this->isClassSearchable($class)) {
33+
return;
34+
}
35+
3236
$indexName = (new $class)->searchableAs();
3337

3438
$index = $this->getIndex($indexName);
3539

36-
File::put(
37-
resource_path('settings/'.$indexName.'json'),
40+
$success = File::put(
41+
$this->path.$indexName.'.json',
3842
json_encode($index->getSettings(), JSON_PRETTY_PRINT)
3943
);
4044

41-
$this->info('All settings for ['.$class.'] index have been backed up.');
45+
if ($success) {
46+
$this->info('All settings for ['.$class.'] index have been backed up.');
47+
} else {
48+
$this->warn('The settings could not be saved');
49+
}
4250
}
4351
}

src/Console/PushCommand.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Algolia\Settings\Console;
44

5+
use AlgoliaSearch\Json;
56
use Illuminate\Support\Facades\File;
67

78
class PushCommand extends AlgoliaCommand
@@ -29,15 +30,21 @@ public function handle()
2930
{
3031
$class = $this->argument('model');
3132

33+
if (! $this->isClassSearchable($class)) {
34+
return;
35+
}
36+
3237
$indexName = (new $class)->searchableAs();
3338

3439
$index = $this->getIndex($indexName);
3540

36-
$settings = json_decode(File::get(
37-
resource_path('settings/'.$indexName.'json')
38-
));
41+
$jsonSettings = File::get($this->path.$indexName.'.json');
42+
43+
if (! $jsonSettings) {
44+
$this->warn("It seems that you don't have any settings to push for [$class].");
45+
}
3946

40-
$index->setSettings($settings);
47+
$index->setSettings(Json::decode($jsonSettings));
4148

4249
$this->info('All settings for ['.$class.'] index have been pushed.');
4350
}

0 commit comments

Comments
 (0)