Skip to content

Commit

Permalink
Fixing pagination problem and namespace issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Harlan Wilton committed Aug 6, 2019
1 parent bc056b8 commit b7b01fc
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 48 deletions.
27 changes: 13 additions & 14 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ public function deleteDocuments($documentIds)
public function listDocuments($page = 1, $pageSize = self::MAX_PAGE_SIZE)
{
$response = $this->client->get('documents/list', [
'page' => [
'current' => $page,
// enforce 100 max
'size' => min(self::MAX_PAGE_SIZE, $pageSize),
],
'json' => [
'page' => [
'current' => $page,
// enforce 100 max
'size' => min(self::MAX_PAGE_SIZE, $pageSize),
],
]
]);

return json_decode($response->getBody()->getContents(), true);
Expand All @@ -150,15 +152,12 @@ public function listAllDocumentsByPages(callable $action, $page = 1, $pageSize =
{
// start with page 1
$currentPage = $page;
do {
$finalPage = 1;
while($currentPage <= $finalPage) {

// Swiftype paginates results 100 per page
$chunkResult = $this->listDocuments([
'page' => [
'current' => $currentPage,
// enforce self::MAX_PAGE_SIZE max
'size' => min(self::MAX_PAGE_SIZE, $pageSize),
],
]);
$chunkResult = $this->listDocuments($currentPage, $pageSize);

// pagination data
$finalPage = $chunkResult['meta']['page']['total_pages'];
$currentPage = $chunkResult['meta']['page']['current'];
Expand All @@ -171,7 +170,7 @@ public function listAllDocumentsByPages(callable $action, $page = 1, $pageSize =
$action($chunkResult['results'], $currentPage, $finalPage);

$currentPage++;
} while ($currentPage <= $finalPage);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/SwiftypeDelete.php → src/Jobs/DeleteDocument.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Jobs\Swiftype;
namespace Loonpwn\Swiftype\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
Expand All @@ -9,7 +9,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Loonpwn\Swiftype\Facades\SwiftypeEngine;

class SwiftypeDelete implements ShouldQueue
class DeleteDocument implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/SwiftypeSync.php → src/Jobs/SyncDocument.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Jobs\Swiftype;
namespace Loonpwn\Swiftype\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
Expand All @@ -9,7 +9,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Loonpwn\Swiftype\Facades\SwiftypeEngine;

class SwiftypeSync implements ShouldQueue
class SyncDocument implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@

namespace Loonpwn\Swiftype\Traits;

use App\Jobs\Swiftype\SwiftypeSync;
use App\Jobs\Swiftype\SwiftypeDelete;

trait ExistsAsSwiftypeDocument
use Loonpwn\Swiftype\Jobs\DeleteDocument;
use Loonpwn\Swiftype\Jobs\SyncDocument;

trait IsSwiftypeDocument
{
public static function bootExistsAsSwiftypeDocument()
public static function bootIsSwiftypeDocument()
{
static::updated(function ($model) {
$data = $model->getModelSwiftypeTransformed();
$data = $model->getSwiftypeAttributes();
if (! empty($data)) {
dispatch(new SwiftypeSync($data));
dispatch(new SyncDocument($data));
}
});
static::created(function ($model) {
$data = $model->getModelSwiftypeTransformed();
$data = $model->getSwiftypeAttributes();
if (! empty($data)) {
dispatch(new SwiftypeSync($data));
dispatch(new SyncDocument($data));
}
});
static::deleted(function ($model) {
dispatch(new SwiftypeDelete($model->getKey()));
dispatch(new DeleteDocument($model->getKey()));
});
}

public function getModelSwiftypeTransformed()
public function getSwiftypeAttributes()
{
$attributes = $this->getAttributes();
// make sure that there is an id field set for swiftype
Expand Down
20 changes: 4 additions & 16 deletions tests/Feature/SwiftypeEngineFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testSeedDocuments()
$this->log('Starting testSeedDocuments');
$modelData = [];
for ($i = 0; $i < 5; $i++) {
$modelData[] = (new TestModel())->getModelSwiftypeTransformed();
$modelData[] = (new TestModel())->getSwiftypeAttributes();
}
$results = SwiftypeEngine::createOrUpdateDocuments($modelData);
$this->assertCount(5, $results, 'All ids are returned');
Expand Down Expand Up @@ -52,27 +52,15 @@ public function testListAllDocumentsWorks()
*/
public function testSearchWorks()
{
$results = SwiftypeEngine::listDocuments();
$results = SwiftypeEngine::listDocuments(1, 1);
$this->assertCount(1, $results['results'], 'Only one result');

$query = $results['results'][0]['first_name'];
$query = $results['results'][0]['advisor_name'];

$this->log('Starting testSearchWorks');
$documents = SwiftypeEngine::searchWithQuery($query);
$this->assertArrayHasKey('results', $documents, 'We can search engine documents');
$this->log('Search for '.$query.': ', count($documents['results']));
}

/**
* A basic test example.
*
* @return void
*/
public function testPurgeDocuments()
{
$this->log('Starting testPurgeDocuments');
$documentIds = SwiftypeEngine::purgeAllDocuments();

$this->assertNotEmpty($documentIds, 'Purge documents returned a valid response');
$this->log('Deleted documents', count($documentIds));
}
}
7 changes: 3 additions & 4 deletions tests/Models/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
namespace Loonpwn\Swiftype\Tests\Models;

use Ramsey\Uuid\Uuid;
use Loonpwn\Swiftype\Traits\ExistsAsSwiftypeDocument;
use Loonpwn\Swiftype\Traits\IsSwiftypeDocument;

class TestModel
{
use ExistsAsSwiftypeDocument;
use IsSwiftypeDocument;

public function getAttributes()
{
$faker = \Faker\Factory::create();

return [
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'advisor_name' => $faker->name,
'location' => $faker->address,
'created_at' => $faker->dateTime(),
];
Expand Down

0 comments on commit b7b01fc

Please sign in to comment.