Skip to content

Commit aadf370

Browse files
committed
Document method overrides
1 parent c628969 commit aadf370

File tree

7 files changed

+53
-18
lines changed

7 files changed

+53
-18
lines changed

src/Scout/MongoDBScoutServiceProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ public function register(): void
2424
$connectionName = $app->get('config')->get('scout.mongodb.connection', 'mongodb');
2525
$connection = $app->get('db')->connection($connectionName);
2626
$softDelete = (bool) $app->get('config')->get('scout.soft_delete', false);
27-
$prefix = (string) $app->get('config')->get('scout.prefix');
2827

2928
assert($connection instanceof Connection, new InvalidArgumentException(sprintf('The connection "%s" is not a MongoDB connection.', $connectionName)));
3029

31-
return new ScoutEngine($connection->getMongoDB(), $softDelete, $prefix);
30+
return new ScoutEngine($connection->getMongoDB(), $softDelete);
3231
});
3332

3433
return $engineManager;

src/Scout/ScoutEngine.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use MongoDB\Driver\CursorInterface;
2222
use MongoDB\Exception\RuntimeException as MongoDBRuntimeException;
2323
use MongoDB\Laravel\Connection;
24+
use Override;
2425
use Traversable;
2526
use TypeError;
2627

@@ -66,17 +67,19 @@ final class ScoutEngine extends Engine
6667
public function __construct(
6768
private Database $database,
6869
private bool $softDelete,
69-
private string $prefix,
7070
) {
7171
}
7272

7373
/**
7474
* Update the given model in the index.
7575
*
76+
* @see Engine::update()
77+
*
7678
* @param EloquentCollection $models
7779
*
7880
* @throws MongoDBRuntimeException
7981
*/
82+
#[Override]
8083
public function update($models)
8184
{
8285
if ($models->isEmpty()) {
@@ -121,8 +124,11 @@ public function update($models)
121124
/**
122125
* Remove the given model from the index.
123126
*
127+
* @see Engine::delete()
128+
*
124129
* @param EloquentCollection $models
125130
*/
131+
#[Override]
126132
public function delete($models): void
127133
{
128134
assert($models instanceof Collection, new TypeError(sprintf('Argument #1 ($models) must be of type %s, %s given', Collection::class, get_debug_type($models))));
@@ -139,8 +145,11 @@ public function delete($models): void
139145
/**
140146
* Perform the given search on the engine.
141147
*
148+
* @see Engine::search()
149+
*
142150
* @return mixed
143151
*/
152+
#[Override]
144153
public function search(Builder $builder)
145154
{
146155
return $this->performSearch($builder);
@@ -149,11 +158,14 @@ public function search(Builder $builder)
149158
/**
150159
* Perform the given search on the engine.
151160
*
161+
* @see Engine::paginate()
162+
*
152163
* @param int $perPage
153164
* @param int $page
154165
*
155166
* @return mixed
156167
*/
168+
#[Override]
157169
public function paginate(Builder $builder, $perPage, $page)
158170
{
159171
assert(is_int($perPage), new TypeError(sprintf('Argument #2 ($perPage) must be of type int, %s given', get_debug_type($perPage))));
@@ -265,8 +277,11 @@ protected function performSearch(Builder $builder, ?int $offset = null): array
265277
/**
266278
* Pluck and return the primary keys of the given results.
267279
*
280+
* @see Engine::mapIds()
281+
*
268282
* @param list<array|object> $results
269283
*/
284+
#[Override]
270285
public function mapIds($results): Collection
271286
{
272287
assert(is_array($results), new TypeError(sprintf('Argument #1 ($results) must be of type array, %s given', get_debug_type($results))));
@@ -277,9 +292,12 @@ public function mapIds($results): Collection
277292
/**
278293
* Map the given results to instances of the given model.
279294
*
295+
* @see Engine::map()
296+
*
280297
* @param array $results
281298
* @param Model $model
282299
*/
300+
#[Override]
283301
public function map(Builder $builder, $results, $model): Collection
284302
{
285303
assert(is_array($results), new TypeError(sprintf('Argument #2 ($results) must be of type array, %s given', get_debug_type($results))));
@@ -316,8 +334,11 @@ public function map(Builder $builder, $results, $model): Collection
316334
* Get the total count from a raw result returned by the engine.
317335
* This is an estimate if the count is larger than 1000.
318336
*
337+
* @see Engine::getTotalCount()
338+
*
319339
* @param mixed $results
320340
*/
341+
#[Override]
321342
public function getTotalCount($results): int
322343
{
323344
if (! $results) {
@@ -330,8 +351,11 @@ public function getTotalCount($results): int
330351
/**
331352
* Flush all records from the engine.
332353
*
354+
* @see Engine::flush()
355+
*
333356
* @param Model $model
334357
*/
358+
#[Override]
335359
public function flush($model): void
336360
{
337361
assert($model instanceof Model, new TypeError(sprintf('Argument #1 ($model) must be of type %s, %s given', Model::class, get_debug_type($model))));
@@ -344,12 +368,15 @@ public function flush($model): void
344368
/**
345369
* Map the given results to instances of the given model via a lazy collection.
346370
*
371+
* @see Engine::lazyMap()
372+
*
347373
* @param Builder $builder
348374
* @param array|Cursor $results
349375
* @param Model $model
350376
*
351377
* @return LazyCollection
352378
*/
379+
#[Override]
353380
public function lazyMap(Builder $builder, $results, $model): LazyCollection
354381
{
355382
assert($results instanceof Cursor || is_array($results), new TypeError(sprintf('Argument #2 ($results) must be of type %s|array, %s given', Cursor::class, get_debug_type($results))));
@@ -388,9 +415,12 @@ public function lazyMap(Builder $builder, $results, $model): LazyCollection
388415
* Accepted options:
389416
* - wait: bool, default true. Wait for the index to be created.
390417
*
418+
* @see Engine::createIndex()
419+
*
391420
* @param string $name Collection name
392421
* @param array{wait?:bool} $options
393422
*/
423+
#[Override]
394424
public function createIndex($name, array $options = []): void
395425
{
396426
assert(is_string($name), new TypeError(sprintf('Argument #1 ($name) must be of type string, %s given', get_debug_type($name))));
@@ -418,7 +448,10 @@ public function createIndex($name, array $options = []): void
418448

419449
/**
420450
* Delete a "search index", i.e. a MongoDB collection.
451+
*
452+
* @see Engine::deleteIndex()
421453
*/
454+
#[Override]
422455
public function deleteIndex($name): void
423456
{
424457
assert(is_string($name), new TypeError(sprintf('Argument #1 ($name) must be of type string, %s given', get_debug_type($name))));

tests/Scout/Models/ScoutUser.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@ class ScoutUser extends Model
2222
protected $table = 'scout_users';
2323
protected static $unguarded = true;
2424

25-
public function searchableAs(): string
26-
{
27-
return 'mongodb_scout_users';
28-
}
29-
3025
/**
31-
* Check if we need to run the schema.
26+
* Create the SQL table for the model.
3227
*/
3328
public static function executeSchema(): void
3429
{

tests/Scout/Models/SearchableInSameNamespace.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class SearchableInSameNamespace extends Model
2020
/**
2121
* Using the same collection as the model collection as Scout index
2222
* is prohibited to prevent erasing the data.
23+
*
24+
* @see Searchable::searchableAs()
2325
*/
2426
public function indexableAs(): string
2527
{

tests/Scout/Models/SearchableModel.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ class SearchableModel extends Model
1414
protected $connection = 'sqlite';
1515
protected $fillable = ['id', 'name', 'date'];
1616

17+
/** @see Searchable::searchableAs() */
1718
public function searchableAs(): string
1819
{
1920
return 'collection_searchable';
2021
}
2122

23+
/** @see Searchable::indexableAs() */
2224
public function indexableAs(): string
2325
{
2426
return 'collection_indexable';
@@ -27,6 +29,8 @@ public function indexableAs(): string
2729
/**
2830
* Overriding the `getScoutKey` method to ensure the custom key is used for indexing
2931
* and searching the model.
32+
*
33+
* @see Searchable::getScoutKey()
3034
*/
3135
public function getScoutKey(): string
3236
{
@@ -36,6 +40,8 @@ public function getScoutKey(): string
3640
/**
3741
* This method must be overridden when the `getScoutKey` method is also overridden,
3842
* to support model serialization for async indexation jobs.
43+
*
44+
* @see Searchable::getScoutKeyName()
3945
*/
4046
public function getScoutKeyName(): string
4147
{

tests/Scout/ScoutEngineTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testSearch(Closure $builder, array $expectedPipeline): void
5252
})
5353
->andReturn($cursor);
5454

55-
$engine = new ScoutEngine($database, softDelete: false, prefix: '');
55+
$engine = new ScoutEngine($database, softDelete: false);
5656
$result = $engine->search($builder());
5757
$this->assertEquals($data, $result);
5858
}
@@ -393,7 +393,7 @@ public function testPaginate()
393393
->with()
394394
->andReturn([['_id' => 'key_1', '__count' => 17], ['_id' => 'key_2', '__count' => 17]]);
395395

396-
$engine = new ScoutEngine($database, softDelete: false, prefix: '');
396+
$engine = new ScoutEngine($database, softDelete: false);
397397
$builder = new Builder(new SearchableModel(), 'mustang');
398398
$builder->orderBy('name', 'desc');
399399
$engine->paginate($builder, $perPage, $page);
@@ -402,7 +402,7 @@ public function testPaginate()
402402
#[DataProvider('provideResultsForMapIds')]
403403
public function testMapIds(array $results): void
404404
{
405-
$engine = new ScoutEngine(m::mock(Database::class), softDelete: false, prefix: '');
405+
$engine = new ScoutEngine(m::mock(Database::class), softDelete: false);
406406

407407
$ids = $engine->mapIds($results);
408408

@@ -468,7 +468,7 @@ public function testUpdate(): void
468468
],
469469
]);
470470

471-
$engine = new ScoutEngine($database, softDelete: false, prefix: '');
471+
$engine = new ScoutEngine($database, softDelete: false);
472472
$engine->update(EloquentCollection::make([
473473
new SearchableModel([
474474
'id' => 1,
@@ -503,7 +503,7 @@ public function testUpdateWithSoftDelete(): void
503503
$model = new SearchableModel(['id' => 1]);
504504
$model->delete();
505505

506-
$engine = new ScoutEngine($database, softDelete: false, prefix: '');
506+
$engine = new ScoutEngine($database, softDelete: false);
507507
$engine->update(EloquentCollection::make([$model]));
508508
}
509509

@@ -518,7 +518,7 @@ public function testDelete(): void
518518
->once()
519519
->with(['_id' => ['$in' => ['key_1', 'key_2']]]);
520520

521-
$engine = new ScoutEngine($database, softDelete: false, prefix: '');
521+
$engine = new ScoutEngine($database, softDelete: false);
522522
$engine->delete(EloquentCollection::make([
523523
new SearchableModel(['id' => 1]),
524524
new SearchableModel(['id' => 2]),
@@ -542,7 +542,7 @@ public function testDeleteWithRemoveableScoutCollection(): void
542542
->once()
543543
->with(['_id' => ['$in' => ['key_5']]]);
544544

545-
$engine = new ScoutEngine($database, softDelete: false, prefix: 'ignored_prefix_');
545+
$engine = new ScoutEngine($database, softDelete: false);
546546
$engine->delete($job->models);
547547
}
548548
}

tests/Scout/ScoutIntegrationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function getEnvironmentSetUp($app): void
3939
parent::getEnvironmentSetUp($app);
4040

4141
$app['config']->set('scout.driver', 'mongodb');
42-
$app['config']->set('scout.prefix', 'scout_');
42+
$app['config']->set('scout.prefix', 'prefix_');
4343
}
4444

4545
public function testItCannotIndexInTheSameNamespace()
@@ -59,7 +59,7 @@ public function testItCanCreateTheCollection()
5959
{
6060
$this->skipIfSearchIndexManagementIsNotSupported();
6161

62-
$collection = DB::connection('mongodb')->getCollection('mongodb_scout_users');
62+
$collection = DB::connection('mongodb')->getCollection('prefix_scout_users');
6363
$collection->drop();
6464

6565
// Init the SQL database with some objects that will be indexed

0 commit comments

Comments
 (0)