Skip to content

Commit

Permalink
Refactored a little bit config endpoint
Browse files Browse the repository at this point in the history
- Create an index is a PUT action, configuring it is a post
- Both creating an index and configuring it, work with Config class
- Deleted ImmutableConfig class
- Created a simple class for synonyms load from file
  • Loading branch information
mmoreram committed Sep 13, 2018
1 parent 77c3d2b commit 4810fdf
Show file tree
Hide file tree
Showing 20 changed files with 363 additions and 220 deletions.
7 changes: 3 additions & 4 deletions App/AppRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace Apisearch\App;

use Apisearch\Config\Config;
use Apisearch\Config\ImmutableConfig;
use Apisearch\Exception\ResourceExistsException;
use Apisearch\Exception\ResourceNotAvailableException;
use Apisearch\Model\Index;
Expand Down Expand Up @@ -66,14 +65,14 @@ public function getIndices(): array;
/**
* Create an index.
*
* @param IndexUUID $indexUUID
* @param ImmutableConfig $config
* @param IndexUUID $indexUUID
* @param Config $config
*
* @throws ResourceExistsException
*/
public function createIndex(
IndexUUID $indexUUID,
ImmutableConfig $config
Config $config
);

/**
Expand Down
11 changes: 5 additions & 6 deletions App/HttpAppRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace Apisearch\App;

use Apisearch\Config\Config;
use Apisearch\Config\ImmutableConfig;
use Apisearch\Exception\ResourceExistsException;
use Apisearch\Exception\ResourceNotAvailableException;
use Apisearch\Http\Http;
Expand Down Expand Up @@ -143,20 +142,20 @@ public function getIndices(): array
/**
* Create an index.
*
* @param IndexUUID $indexUUID
* @param ImmutableConfig $config
* @param IndexUUID $indexUUID
* @param Config $config
*
* @throws ResourceExistsException
*/
public function createIndex(
IndexUUID $indexUUID,
ImmutableConfig $config
Config $config
) {
$response = $this
->httpClient
->get(
'/index',
'post',
'put',
Http::getAppQueryValues($this),
[
Http::INDEX_FIELD => $indexUUID->toArray(),
Expand Down Expand Up @@ -246,7 +245,7 @@ public function configureIndex(
$response = $this
->httpClient
->get(
'/index/config',
'/index',
'post',
Http::getAppQueryValues($this, $indexUUID),
[
Expand Down
7 changes: 3 additions & 4 deletions App/InMemoryAppRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace Apisearch\App;

use Apisearch\Config\Config;
use Apisearch\Config\ImmutableConfig;
use Apisearch\Exception\ResourceExistsException;
use Apisearch\Exception\ResourceNotAvailableException;
use Apisearch\Model\AppUUID;
Expand Down Expand Up @@ -77,14 +76,14 @@ public function getIndices(): array
/**
* Create an index.
*
* @param IndexUUID $indexUUID
* @param ImmutableConfig $config
* @param IndexUUID $indexUUID
* @param Config $config
*
* @throws ResourceExistsException
*/
public function createIndex(
IndexUUID $indexUUID,
ImmutableConfig $config
Config $config
) {
if (array_key_exists($this->getIndexKey($indexUUID), $this->indices)) {
throw ResourceExistsException::indexExists();
Expand Down
7 changes: 3 additions & 4 deletions App/MockAppRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace Apisearch\App;

use Apisearch\Config\Config;
use Apisearch\Config\ImmutableConfig;
use Apisearch\Exception\MockException;
use Apisearch\Exception\ResourceExistsException;
use Apisearch\Exception\ResourceNotAvailableException;
Expand Down Expand Up @@ -82,14 +81,14 @@ public function getIndices(): array
/**
* Create an index.
*
* @param IndexUUID $indexUUID
* @param ImmutableConfig $config
* @param IndexUUID $indexUUID
* @param Config $config
*
* @throws ResourceExistsException
*/
public function createIndex(
IndexUUID $indexUUID,
ImmutableConfig $config
Config $config
) {
$this->throwMockException();
}
Expand Down
121 changes: 111 additions & 10 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,114 @@

namespace Apisearch\Config;

use Apisearch\Exception\InvalidFormatException;
use Apisearch\Model\HttpTransportable;

/**
* Class Config.
*/
class Config implements HttpTransportable
{
/**
* @var string|null
*
* Language
*/
private $language;

/**
* @var bool
*
* Store searchable metadata
*/
private $storeSearchableMetadata;

/**
* @var Synonym[]
*
* Synonyms
*/
private $synonyms = [];

/**
* @var Campaigns
*
* Campaigns
*/
private $campaigns;
private $campaigns = [];

/**
* Config constructor.
*
* @param null|string $language
* @param bool $storeSearchableMetadata
*/
public function __construct()
{
public function __construct(
?string $language = null,
bool $storeSearchableMetadata = true
) {
$this->language = $language;
$this->storeSearchableMetadata = $storeSearchableMetadata;
$this->campaigns = new Campaigns();
}

/**
* Get language.
*
* @return null|string
*/
public function getLanguage(): ? string
{
return $this->language;
}

/**
* Get if searchable metadata is stored.
*
* @return bool
*/
public function shouldSearchableMetadataBeStored(): ? bool
{
return $this->storeSearchableMetadata;
}

/**
* Add synonym.
*
* @param Synonym $synonym
*
* @return Config
*/
public function addSynonym(Synonym $synonym): Config
{
$this->synonyms[] = $synonym;

return $this;
}

/**
* get synonyms.
*
* @return Synonym[]
*/
public function getSynonyms(): array
{
return $this->synonyms;
}

/**
* Add campaign.
*
* @param Campaign $campaign
*
* @return Config
*/
public function addCampaign(Campaign $campaign)
public function addCampaign(Campaign $campaign): Config
{
$this
->campaigns
->addCampaign($campaign);

return $this;
}

/**
Expand All @@ -68,10 +143,21 @@ public function getCampaigns(): Campaigns
public function toArray(): array
{
return array_filter([
'language' => $this->language,
'store_searchable_metadata' => ($this->storeSearchableMetadata ? null : false),
'synonyms' => array_map(function (Synonym $synonym) {
return $synonym->toArray();
}, $this->synonyms),
'campaigns' => $this
->campaigns
->toArray(),
]);
], function ($element) {
return
!(
is_null($element) ||
(is_array($element) && empty($element))
);
});
}

/**
Expand All @@ -80,14 +166,29 @@ public function toArray(): array
* @param array $array
*
* @return self
*
* @throws InvalidFormatException
*/
public static function createFromArray(array $array)
public static function createFromArray(array $array): self
{
$config = new self();
$config = new self(
($array['language'] ?? null),
($array['store_searchable_metadata'] ?? true)
);

$config->campaigns = Campaigns::createFromArray($array['campaigns'] ?? []);
$config->synonyms = array_map(function (array $synonym) {
return Synonym::createFromArray($synonym);
}, $array['synonyms'] ?? []);

return $config;
}

/**
* Create empty.
*
* @return self
*/
public static function createEmpty(): self
{
return self::createFromArray([]);
}
}
Loading

0 comments on commit 4810fdf

Please sign in to comment.