Skip to content

Commit

Permalink
Refactor duplicate code in Managed Resources (solariumphp#847)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomascorthals authored Jul 20, 2020
1 parent 5b2cc06 commit 11fecf8
Show file tree
Hide file tree
Showing 38 changed files with 530 additions and 1,040 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed

### Changed
- Refactored Managed Resources code: use `createCommand()` and `createInitArgs()` to issue commands

### Removed

Expand Down
186 changes: 186 additions & 0 deletions src/QueryType/ManagedResources/Query/AbstractQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

/*
* This file is part of the Solarium package.
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code.
*/

namespace Solarium\QueryType\ManagedResources\Query;

use Solarium\Core\Query\AbstractQuery as BaseQuery;
use Solarium\Core\Query\RequestBuilderInterface;
use Solarium\Core\Query\ResponseParserInterface;
use Solarium\Exception\InvalidArgumentException;
use Solarium\QueryType\ManagedResources\RequestBuilder\Resource as RequestBuilder;

abstract class AbstractQuery extends BaseQuery
{
/**
* Command add.
*/
const COMMAND_ADD = 'add';

/**
* Command config.
*/
const COMMAND_CONFIG = 'config';

/**
* Command create.
*/
const COMMAND_CREATE = 'create';

/**
* Command delete.
*/
const COMMAND_DELETE = 'delete';

/**
* Command exists.
*/
const COMMAND_EXISTS = 'exists';

/**
* Command remove.
*/
const COMMAND_REMOVE = 'remove';

/**
* Command types.
*
* @var array
*/
protected $commandTypes;

/**
* Name of the managed resource.
*
* @var string
*/
protected $name = '';

/**
* Command.
*
* @var \Solarium\QueryType\ManagedResources\Query\AbstractCommand
*/
protected $command;

/**
* Get query type.
*
* @return string
*/
abstract public function getType(): string;

/**
* Get the request builder class for this query.
*
* @return \Solarium\QueryType\ManagedResources\RequestBuilder\Resource
*/
public function getRequestBuilder(): RequestBuilderInterface
{
return new RequestBuilder();
}

/**
* Get the response parser class for this query.
*
* @return \Solarium\Core\Query\ResponseParserInterface
*/
abstract public function getResponseParser(): ResponseParserInterface;

/**
* Get the name of the managed resource.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* Set the name of the managed resource.
*
* @param string $name
*
* @return self
*/
public function setName(string $name): self
{
$this->name = $name;

return $this;
}

/**
* Create a command instance.
*
* @param string $type
* @param mixed $options
*
* @throws \Solarium\Exception\InvalidArgumentException
*
* @return \Solarium\QueryType\ManagedResources\Query\AbstractCommand
*/
public function createCommand($type, $options = null): AbstractCommand
{
$type = strtolower($type);

if (!isset($this->commandTypes[$type])) {
throw new InvalidArgumentException(sprintf('Managed resource command type unknown: %s', $type));
}

$class = $this->commandTypes[$type];

return new $class($options);
}

/**
* Get command for this query.
*
* @return \Solarium\QueryType\ManagedResources\Query\AbstractCommand|null
*/
public function getCommand(): ?AbstractCommand
{
return $this->command;
}

/**
* Set a command to the query.
*
* @param \Solarium\QueryType\ManagedResources\Query\AbstractCommand $command
*
* @return self Provides fluent interface
*/
public function setCommand(AbstractCommand $command): self
{
$this->command = $command;

return $this;
}

/**
* Remove command from the query.
*
* @return self Provides fluent interface
*/
public function removeCommand(): self
{
$this->command = null;

return $this;
}

/**
* Create an init args instance.
*
* @param array $initArgs
*
* @return \Solarium\QueryType\ManagedResources\Query\InitArgsInterface
*/
abstract public function createInitArgs(array $initArgs = null): InitArgsInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
* file that was distributed with this source code.
*/

namespace Solarium\QueryType\ManagedResources\Query\Stopwords\Command;
namespace Solarium\QueryType\ManagedResources\Query\Command;

use Solarium\Core\Client\Request;
use Solarium\QueryType\ManagedResources\Query\AbstractCommand;
use Solarium\QueryType\ManagedResources\Query\Stopwords;
use Solarium\QueryType\ManagedResources\Query\AbstractQuery as Query;

/**
* Remove.
* Command Add base class.
*/
class Remove extends AbstractCommand
abstract class AbstractAdd extends AbstractCommand
{
/**
* Returns command type, for use in adapters.
Expand All @@ -25,7 +25,7 @@ class Remove extends AbstractCommand
*/
public function getType(): string
{
return Stopwords::COMMAND_REMOVE;
return Query::COMMAND_ADD;
}

/**
Expand All @@ -35,17 +35,7 @@ public function getType(): string
*/
public function getRequestMethod(): string
{
return Request::METHOD_DELETE;
}

/**
* Returns the raw data to be sent to Solr.
*
* @return string
*/
public function getRawData(): string
{
return '';
return Request::METHOD_PUT;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
* file that was distributed with this source code.
*/

namespace Solarium\QueryType\ManagedResources\Query\Stopwords\Command;
namespace Solarium\QueryType\ManagedResources\Query\Command;

use Solarium\Core\Client\Request;
use Solarium\QueryType\ManagedResources\Query\AbstractCommand;
use Solarium\QueryType\ManagedResources\Query\Stopwords;
use Solarium\QueryType\ManagedResources\Query\AbstractQuery as Query;

/**
* Create.
* Command Create base class.
*/
class Create extends AbstractCommand
abstract class AbstractCreate extends AbstractCommand
{
/**
* Returns command type, for use in adapters.
Expand All @@ -25,7 +25,7 @@ class Create extends AbstractCommand
*/
public function getType(): string
{
return Stopwords::COMMAND_CREATE;
return Query::COMMAND_CREATE;
}

/**
Expand All @@ -38,16 +38,6 @@ public function getRequestMethod(): string
return Request::METHOD_PUT;
}

/**
* Returns the raw data to be sent to Solr.
*
* @return string
*/
public function getRawData(): string
{
return json_encode(['class' => 'org.apache.solr.rest.schema.analysis.ManagedWordSetResource']);
}

/**
* Empty.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
* file that was distributed with this source code.
*/

namespace Solarium\QueryType\ManagedResources\Query\Stopwords\Command;
namespace Solarium\QueryType\ManagedResources\Query\Command;

use Solarium\Core\Client\Request;
use Solarium\QueryType\ManagedResources\Query\AbstractCommand;
use Solarium\QueryType\ManagedResources\Query\AbstractQuery as Query;
use Solarium\QueryType\ManagedResources\Query\InitArgsInterface;
use Solarium\QueryType\ManagedResources\Query\Stopwords;

/**
* Config.
Expand All @@ -22,7 +22,7 @@ class Config extends AbstractCommand
/**
* Configuration parameters to set.
*
* @var InitArgsInterface
* @var \Solarium\QueryType\ManagedResources\Query\InitArgsInterface
*/
protected $initArgs;

Expand All @@ -33,7 +33,7 @@ class Config extends AbstractCommand
*/
public function getType(): string
{
return Stopwords::COMMAND_CONFIG;
return Query::COMMAND_CONFIG;
}

/**
Expand All @@ -49,7 +49,7 @@ public function getRequestMethod(): string
/**
* Returns configuration parameters.
*
* @return InitArgsInterface
* @return \Solarium\QueryType\ManagedResources\Query\InitArgsInterface
*/
public function getInitArgs(): InitArgsInterface
{
Expand All @@ -59,7 +59,7 @@ public function getInitArgs(): InitArgsInterface
/**
* Set configuration parameters.
*
* @param InitArgsInterface $initArgs
* @param \Solarium\QueryType\ManagedResources\Query\InitArgsInterface $initArgs
*
* @return self
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* file that was distributed with this source code.
*/

namespace Solarium\QueryType\ManagedResources\Query\Synonyms\Command;
namespace Solarium\QueryType\ManagedResources\Query\Command;

use Solarium\Core\Client\Request;
use Solarium\QueryType\ManagedResources\Query\AbstractCommand;
use Solarium\QueryType\ManagedResources\Query\Synonyms;
use Solarium\QueryType\ManagedResources\Query\AbstractQuery as Query;

/**
* Delete.
Expand All @@ -32,7 +32,7 @@ class Delete extends AbstractCommand
*/
public function getType(): string
{
return Synonyms::COMMAND_DELETE;
return Query::COMMAND_DELETE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* file that was distributed with this source code.
*/

namespace Solarium\QueryType\ManagedResources\Query\Synonyms\Command;
namespace Solarium\QueryType\ManagedResources\Query\Command;

use Solarium\Core\Client\Request;
use Solarium\QueryType\ManagedResources\Query\AbstractCommand;
use Solarium\QueryType\ManagedResources\Query\Synonyms;
use Solarium\QueryType\ManagedResources\Query\AbstractQuery as Query;

/**
* Exists.
Expand All @@ -32,7 +32,7 @@ class Exists extends AbstractCommand
*/
public function getType(): string
{
return Synonyms::COMMAND_EXISTS;
return Query::COMMAND_EXISTS;
}

/**
Expand Down
Loading

0 comments on commit 11fecf8

Please sign in to comment.