Commit 2a03c938 authored by Bert Balcaen's avatar Bert Balcaen
Browse files

Fixing coding standards.

parent 850bf812
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
<description>The coding standard for our project.</description>
<file>.</file>
<exclude-pattern>vendor</exclude-pattern>
<arg value="np"/>
<rule ref="PSR2">
<exclude name="Generic.Files.LineLength"/>
<exclude name="Generic.ControlStructures.InlineControlStructure.NotAllowed"/>
</rule>
</ruleset>
......@@ -3,10 +3,12 @@
use Illuminate\Support\ServiceProvider;
use Laravel\Scout\EngineManager;
class ScoutServiceProvider extends ServiceProvider{
public function boot(){
class ScoutServiceProvider extends ServiceProvider
{
public function boot()
{
resolve(EngineManager::class)->extend('solr', function () {
return new SolrEngine();
});
}
}
\ No newline at end of file
}
......@@ -2,7 +2,8 @@
use Laravel\Scout\Searchable as ScoutSearchable;
trait Searchable{
trait Searchable
{
use ScoutSearchable;
/**
......@@ -10,7 +11,8 @@ trait Searchable{
*
* @return array
*/
public function getScoutMap(){
public function getScoutMap()
{
return array_combine($this->attributes, $this->attributes);
}
......@@ -19,17 +21,18 @@ trait Searchable{
*
* @return array
*/
public function toSearchableArray(){
public function toSearchableArray()
{
$attributes = $this->toArray();
$map = $this->getScoutMap();
foreach ($attributes as $attribute => &$value) {
if($attribute == 'id')
if ($attribute == 'id')
$value = $this->getTable()."-$value";
if($attribute == $map[$attribute])
if ($attribute == $map[$attribute])
continue;
$attributes[$map[$attribute]] = $value;
unset($attributes[$attribute]);
}
return $attributes;
}
}
\ No newline at end of file
}
......@@ -4,7 +4,8 @@ use Illuminate\Database\Eloquent\Collection;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
class SolrEngine extends Engine{
class SolrEngine extends Engine
{
/**
* Update the given model in the index.
......@@ -13,9 +14,10 @@ class SolrEngine extends Engine{
* @throws \Exception In case of update command failure.
* @return void
*/
public function update($models){
public function update($models)
{
$update = \Solr::createUpdate();
$models->each(function($model) use(&$update){
$models->each(function ($model) use (&$update) {
$document = $update->createDocument($model->toSearchableArray());
$update->addDocument($document);
});
......@@ -29,9 +31,10 @@ class SolrEngine extends Engine{
* @throws \Exception In case of delete command failure.
* @return void
*/
public function delete($models){
public function delete($models)
{
$update = \Solr::createUpdate();
$models->each(function($model) use(&$update){
$models->each(function ($model) use (&$update) {
$update->addDeleteById($model->getKey());
});
$this->executeStatement($update);
......@@ -43,10 +46,11 @@ class SolrEngine extends Engine{
* @throws \Exception In case of command failure.
* @param $statement \Solarium\QueryType\Update\Query\Query
*/
private function executeStatement(&$statement){
private function executeStatement(&$statement)
{
$statement->addCommit();
$response = \Solr::update($statement);
if($response->getStatus() != 0)
if ($response->getStatus() != 0)
throw new \Exception("Update command failed \n\n".json_encode($response->getData()));
}
......@@ -56,7 +60,8 @@ class SolrEngine extends Engine{
* @param \Laravel\Scout\Builder $builder
* @return mixed
*/
public function search(Builder $builder){
public function search(Builder $builder)
{
$query = \Solr::createSelect();
return $this->executeQuery($query, $builder);
}
......@@ -69,7 +74,8 @@ class SolrEngine extends Engine{
* @param int $page
* @return mixed
*/
public function paginate(Builder $builder, $perPage, $page){
public function paginate(Builder $builder, $perPage, $page)
{
$query = \Solr::createSelect();
$offset = ($page - 1) * $perPage;
return $this->executeQuery($query, $builder, $offset, $perPage);
......@@ -84,12 +90,13 @@ class SolrEngine extends Engine{
* @param int $limit
* @return \Solarium\QueryType\Select\Result\Result
*/
private function executeQuery(&$query, &$builder, $offset = 0, $limit = null){
private function executeQuery(&$query, &$builder, $offset = 0, $limit = null)
{
$conditions = (!empty($builder->query))? [$builder->query] : [];
foreach($builder->wheres as $key => &$value)
foreach ($builder->wheres as $key => &$value)
$conditions[] = "$key:\"$value\"";
$query->setQuery(implode(' ', $conditions));
if(!is_null($limit))
if (!is_null($limit))
$query->setStart($offset)->setRows($limit);
return \Solr::select($query);
}
......@@ -100,9 +107,10 @@ class SolrEngine extends Engine{
* @param \Solarium\QueryType\Select\Result\Result $results
* @return \Illuminate\Support\Collection
*/
public function mapIds($results){
public function mapIds($results)
{
$ids = [];
foreach($results as $document)
foreach ($results as $document)
$ids[] = $document->id;
return collect($ids);
}
......@@ -114,13 +122,14 @@ class SolrEngine extends Engine{
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Collection
*/
public function map($results, $model){
public function map($results, $model)
{
$map = array_flip($model->getScoutMap());
$models = [];
$modelClass = get_class($model);
foreach($results as $document){
foreach ($results as $document) {
$attributes = [];
foreach($document as $field => $value)
foreach ($document as $field => $value)
$attributes[$map[$field]] = ($map[$field] != 'id')? $value : str_replace($model->getTable().'-', '', $value);
$models[] = new $modelClass($attributes);
}
......@@ -133,7 +142,8 @@ class SolrEngine extends Engine{
* @param \Solarium\QueryType\Select\Result\Result $results
* @return int
*/
public function getTotalCount($results){
public function getTotalCount($results)
{
return $results->getNumFound();
}
}
\ No newline at end of file
}
......@@ -3,8 +3,10 @@
use Illuminate\Support\Facades\Facade;
use Solarium\Client;
class SolrFacade extends Facade{
protected static function getFacadeAccessor(){
class SolrFacade extends Facade
{
protected static function getFacadeAccessor()
{
return Client::class;
}
}
\ No newline at end of file
}
......@@ -3,22 +3,26 @@
use Illuminate\Support\ServiceProvider;
use Solarium\Client;
class SolrServiceProvider extends ServiceProvider{
class SolrServiceProvider extends ServiceProvider
{
protected $defer = true;
public function boot(){
public function boot()
{
$this->publishes([
__DIR__.'/solr-config.php' => config_path('solr.php')
]);
}
public function register(){
$this->app->singleton(Client::class, function(){
public function register()
{
$this->app->singleton(Client::class, function () {
return new Client(config('solr'));
});
}
public function provides(){
public function provides()
{
return [Client::class];
}
}
\ No newline at end of file
}
......@@ -7,4 +7,4 @@ return [
'path' => '/solr/',
]
]
];
\ No newline at end of file
];
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment