Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set scout configuration in engine manager #338

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion src/Managers/EngineManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Algolia\AlgoliaSearch\Support\UserAgent;
use Algolia\ScoutExtended\Engines\AlgoliaEngine;
use Laravel\Scout\EngineManager as BaseEngineManager;
use Algolia\AlgoliaSearch\Config\SearchConfig;

class EngineManager extends BaseEngineManager
{
Expand All @@ -29,6 +30,55 @@ public function createAlgoliaDriver(): AlgoliaEngine
{
UserAgent::addCustomUserAgent('Laravel Scout Extended', '3.0.0');

return new AlgoliaEngine(SearchClient::create(config('scout.algolia.id'), config('scout.algolia.secret')));
$config = SearchConfig::create(
config('scout.algolia.id'),
config('scout.algolia.secret')
)->setDefaultHeaders(
$this->defaultAlgoliaHeaders()
);

if (is_int($connectTimeout = config('scout.algolia.connect_timeout'))) {
$config->setConnectTimeout($connectTimeout);
}

if (is_int($readTimeout = config('scout.algolia.read_timeout'))) {
$config->setReadTimeout($readTimeout);
}

if (is_int($writeTimeout = config('scout.algolia.write_timeout'))) {
$config->setWriteTimeout($writeTimeout);
}

if (is_int($batchSize = config('scout.algolia.batch_size'))) {
$config->setBatchSize($batchSize);
}

return new AlgoliaEngine(SearchClient::createWithConfig($config));
}

/**
* Set the default Algolia configuration headers.
*
* @return array
*/
protected function defaultAlgoliaHeaders()
{
if (! config('scout.identify')) {
return [];
}

$headers = [];

if (! config('app.debug') &&
filter_var($ip = request()->ip(), FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
) {
$headers['X-Forwarded-For'] = $ip;
}

if (($user = request()->user()) && method_exists($user, 'getKey')) {
$headers['X-Algolia-UserToken'] = $user->getKey();
}

return $headers;
}
}