Skip to content

Commit

Permalink
chore: php cs lint, remove outdated author list
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Aug 17, 2022
1 parent 6f61c3d commit 93c29bd
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 129 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# For more information about the properties used in
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,js,json,css,scss,feature}]
indent_size = 2
indent_style = space

[composer.json]
indent_size = 4

# Don't perform any clean-up on thirdparty files
[thirdparty/**]
trim_trailing_whitespace = false
insert_final_newline = false
23 changes: 3 additions & 20 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,18 @@
"elastica"
],
"license": "BSD-3-Clause",
"authors": [
{
"name": "Andrew Short",
"email": "[email protected]"
},
{
"name": "Marcus Nyeholt",
"email": "[email protected]"
},
{
"name": "Glenn Bautista",
"email": "[email protected]"
},
{
"name": "Ben Dubuisson",
"email": "[email protected]"
}
],
"support": {
"issues": "http://github.com/heyday/silverstripe-elastica/issues"
},
"require": {
"php": ">=7.2",
"php": ">=7.4",
"ruflin/elastica": "^7",
"elasticsearch/elasticsearch": ">=7.3.0",
"symbiote/silverstripe-queuedjobs": "^4"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.4"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 3 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>

<file>src</file>
<file>tests</file>
<!-- base rules are PSR-2 -->
<rule ref="PSR2">
<!-- Current exclusions -->
<exclude name="PSR1.Methods.CamelCapsMethodName" />
</rule>
</ruleset>

13 changes: 7 additions & 6 deletions src/ElasticaPercolateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class ElasticaPercolateService extends ElasticaService

/**
* ElasticaPercolateService constructor.
* @param Client $client
*
* @param Client $client
* @param $indexName
* @param LoggerInterface|null $logger
* @param null $indexingMemory
* @param string $searchableExtensionClassName
* @param null $doctypeToPercolate
* @param null $indexingMemory
* @param string $searchableExtensionClassName
* @param null $doctypeToPercolate
*/
public function __construct(
Client $client,
Expand All @@ -34,7 +35,7 @@ public function __construct(
}

/**
* @param Searchable $record
* @param Searchable $record
* @return Response|null
* @throws Exception
*/
Expand All @@ -47,7 +48,7 @@ public function index($record)
}

/**
* @param Searchable $record
* @param Searchable $record
* @return Response|null
* @throws Exception
*/
Expand Down
111 changes: 62 additions & 49 deletions src/ElasticaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ class ElasticaService
*
* @param Client $client
* @param string $indexName
* @param LoggerInterface|null $logger Increases the memory limit while indexing.
* @param string $indexingMemory A memory limit string, such as "64M".
* @param LoggerInterface|null $logger Increases the memory limit while indexing.
* @param string $indexingMemory A memory limit string, such as "64M".
* @param string $searchableExtensionClassName
*/
public function __construct(
Expand Down Expand Up @@ -122,9 +122,10 @@ protected function getIndexConfig()

/**
* Performs a search query and returns either a ResultList (SS template compatible) or an Elastica\ResultSet
* @param Query|string|array $query
* @param array $options Options defined in \Elastica\Search
* @param bool $returnResultList
*
* @param Query|string|array $query
* @param array $options Options defined in \Elastica\Search
* @param bool $returnResultList
* @return ResultList | ResultSet
*/
public function search($query, $options = null, $returnResultList = true)
Expand All @@ -142,12 +143,14 @@ public function search($query, $options = null, $returnResultList = true)
*/
public function createIndex()
{
return $this->runQuery(function () {
$index = $this->getIndex();
$config = $this->getIndexConfig() ?: [];
$forceRecreate = !empty($config);
return $index->create($config, $forceRecreate);
});
return $this->runQuery(
function () {
$index = $this->getIndex();
$config = $this->getIndexConfig() ?: [];
$forceRecreate = !empty($config);
return $index->create($config, $forceRecreate);
}
);
}

/**
Expand All @@ -157,16 +160,18 @@ public function createIndex()
*/
public function deleteIndex()
{
return $this->runQuery(function () {
$index = $this->getIndex();
return $index->delete();
});
return $this->runQuery(
function () {
$index = $this->getIndex();
return $index->delete();
}
);
}

/**
* Either creates or updates a record in the index.
*
* @param Searchable|DataObject $record
* @param Searchable|DataObject $record
* @return Response|null|bool Return response, or true if batched
* @throws Exception
*/
Expand Down Expand Up @@ -197,11 +202,13 @@ public function index($record)
}

// Add document
return $this->runQuery(function () use ($index, $document) {
$response = $index->addDocument($document);
$index->refresh();
return $response;
});
return $this->runQuery(
function () use ($index, $document) {
$response = $index->addDocument($document);
$index->refresh();
return $response;
}
);
}

/**
Expand All @@ -219,8 +226,8 @@ protected function isBatching()
* For example, you might call batch with a closure that initiates ->index() on 20 records.
* On the conclusion of this closure, those 20 updates will be batched together into a single update
*
* @param callable $callback Callback within which to batch updates
* @param int $documentsProcessed Number of documents processed during this batch
* @param callable $callback Callback within which to batch updates
* @param int $documentsProcessed Number of documents processed during this batch
* @return mixed result of $callback
* @throws Exception
*/
Expand All @@ -242,7 +249,7 @@ public function batch(callable $callback, &$documentsProcessed = 0)
/**
* Process a batch update
*
* @param Document[][][] $batch List of updates for this batch, grouped by type
* @param Document[][][] $batch List of updates for this batch, grouped by type
* @return int Number of documents updated in this batch
*/
protected function flushBatch($batch)
Expand All @@ -252,13 +259,13 @@ protected function flushBatch($batch)
// process batches
$index = null;
foreach ($batch as $type => $changes) {
$typeObject = null;
foreach ($changes as $action => $documents) {
if (empty($documents)) {
continue;
}
$index = $index ?: $this->getIndex();
$documentsProcessed += count($documents);

switch ($action) {
case self::UPDATES:
$index->addDocuments($documents);
Expand All @@ -275,18 +282,20 @@ protected function flushBatch($batch)
}
}
}

// Refresh if any documents updated
if ($documentsProcessed && $index) {
$index->refresh();
}

return $documentsProcessed;
}

/**
* Add document to batch query
*
* @param string $type elasticsearch type name
* @param string $action self::DELETES or self::UPDATES
* @param string $type elasticsearch type name
* @param string $action self::DELETES or self::UPDATES
* @param Document $document
*/
protected function batchDocument($type, $action, $document)
Expand Down Expand Up @@ -314,7 +323,7 @@ protected function batchDocument($type, $action, $document)
}

/**
* @param Searchable|DataObject $record
* @param Searchable|DataObject $record
* @return Response|null|bool Response, or true if batched
* @throws Exception
*/
Expand Down Expand Up @@ -348,7 +357,7 @@ public function remove($record)
/**
* Creates the index and the type mappings.
*
* @param bool $recreate
* @param bool $recreate
* @throws Exception
*/
public function define($recreate = false)
Expand All @@ -365,7 +374,9 @@ public function define($recreate = false)
}

foreach ($this->getIndexedClasses() as $class) {
/** @var Searchable */
/**
* @var Searchable
*/
$sng = singleton($class);
$props = $sng->getElasticaMapping();

Expand All @@ -375,29 +386,31 @@ public function define($recreate = false)

/**
* Re-indexes each record in the index.
*
* @throws Exception
*/
public function refresh()
{
Versioned::withVersionedMode(function () {
Versioned::set_stage(Versioned::LIVE);

foreach ($this->getIndexedClasses() as $class) {

foreach (DataObject::get($class) as $record) {
// Only index records with Show In Search enabled, or those that don't expose that fielid
if (!$record->hasField('ShowInSearch') || $record->ShowInSearch) {
if ($this->index($record)) {
$this->printActionMessage($record, 'INDEXED');
}
} else {
if ($this->remove($record)) {
$this->printActionMessage($record, 'REMOVED');
Versioned::withVersionedMode(
function () {
Versioned::set_stage(Versioned::LIVE);

foreach ($this->getIndexedClasses() as $class) {
foreach (DataObject::get($class) as $record) {
// Only index records with Show In Search enabled, or those that don't expose that fielid
if (!$record->hasField('ShowInSearch') || $record->ShowInSearch) {
if ($this->index($record)) {
$this->printActionMessage($record, 'INDEXED');
}
} else {
if ($this->remove($record)) {
$this->printActionMessage($record, 'REMOVED');
}
}
}
}
}
});
);
}

/**
Expand Down Expand Up @@ -438,7 +451,7 @@ protected function printActionMessage(DataObject $record, $action)
* If a logger is configured, log the exception there.
* Otherwise the exception is thrown
*
* @param Exception $exception
* @param Exception $exception
* @throws Exception
*/
protected function exception($exception)
Expand All @@ -461,7 +474,7 @@ protected function exception($exception)
/**
* Check if response has any errors
*
* @param Response|null $response
* @param Response|null $response
* @throws Exception
*/
protected function logResponse(Response $response = null)
Expand All @@ -475,7 +488,7 @@ protected function logResponse(Response $response = null)
$data = $response->getData();
$errorMessage = $data['message']
?? $response->getErrorMessage()
?: sprintf("HTTP %d error", $response->getStatus());
?: sprintf("HTTP %d error", $response->getStatus());
$message = "Elastica server error: $errorMessage";

// If no logger specified expose error normally
Expand All @@ -490,7 +503,7 @@ protected function logResponse(Response $response = null)
/**
* Run elastic search query
*
* @param callable $callback A callback that generates an Elastica Response object
* @param callable $callback A callback that generates an Elastica Response object
* @return Response The response
* @throws Exception
*/
Expand Down
Loading

0 comments on commit 93c29bd

Please sign in to comment.