Skip to content

Commit

Permalink
[BC] Removed DocumentMetadata::getType()
Browse files Browse the repository at this point in the history
Wait until ES server is up before running travis tests
Updated typehints and docs
  • Loading branch information
pmishev committed Mar 16, 2021
1 parent bd341b5 commit 5141e84
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 53 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ before_script:
- export XDEBUG_MODE=coverage

script:
- wget -q --waitretry=1 --retry-connrefused -T 10 -O - http://127.0.0.1:9200
- vendor/bin/simple-phpunit --coverage-clover=Tests/App/build/clover.xml 2>/dev/null
- vendor/bin/phpcs -np --standard=PSR2 --ignore=vendor/,Tests/App/,var/ ./

Expand Down
8 changes: 4 additions & 4 deletions Document/Provider/AbstractDoctrineProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ abstract class AbstractDoctrineProvider extends AbstractProvider
protected $sourceDataHydration = AbstractQuery::HYDRATE_OBJECT;

/**
* @param string $documentClass The type the provider is for
* @param string $documentClass The document class the provider is for
* @param EntityManagerInterface $em The Doctrine entity manager
*/
public function __construct($documentClass, EntityManagerInterface $em)
public function __construct(string $documentClass, EntityManagerInterface $em)
{
parent::__construct($documentClass);
$this->em = $em;
Expand All @@ -50,7 +50,7 @@ public function __construct($documentClass, EntityManagerInterface $em)
/**
* @param int $batchSize
*/
public function setBatchSize($batchSize)
public function setBatchSize(int $batchSize)
{
$this->batchSize = $batchSize;
}
Expand All @@ -60,7 +60,7 @@ public function setBatchSize($batchSize)
*
* @return Query
*/
abstract public function getQuery();
abstract public function getQuery(): Query;

/**
* Converts a Doctrine entity to Elasticsearch entity
Expand Down
6 changes: 3 additions & 3 deletions Document/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
abstract class AbstractProvider implements ProviderInterface
{
/**
* @var string The type the provider is for
* @var string The document class the provider is for
*/
private $documentClass;

/**
* @param string $documentClass The type the provider is for
* @param string $documentClass The document class the provider is for
*/
public function __construct($documentClass)
public function __construct(string $documentClass)
{
$this->documentClass = $documentClass;
}
Expand Down
16 changes: 10 additions & 6 deletions Document/Provider/ElasticsearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ElasticsearchProvider extends AbstractProvider
protected $sourceIndexManager;

/**
* @var string The type the data is coming from
* @var string The document class the data is coming from
*/
protected $sourceDocumentClass;

Expand All @@ -38,13 +38,17 @@ class ElasticsearchProvider extends AbstractProvider
protected $chunkSize = 500;

/**
* @param string $documentClass The type the provider is for
* @param string $documentClass The document class the provider is for
* @param DocumentMetadataCollector $metadataCollector The metadata collector
* @param IndexManager $sourceIndexManager The index manager of the data source
* @param string $sourceDocumentClass The type the data is coming from
* @param string $sourceDocumentClass The document class the data is coming from
*/
public function __construct($documentClass, DocumentMetadataCollector $metadataCollector, IndexManager $sourceIndexManager, $sourceDocumentClass)
{
public function __construct(
string $documentClass,
DocumentMetadataCollector $metadataCollector,
IndexManager $sourceIndexManager,
string $sourceDocumentClass
) {
parent::__construct($documentClass);
$this->sourceIndexManager = $sourceIndexManager;
$this->metadataCollector = $metadataCollector;
Expand Down Expand Up @@ -87,7 +91,7 @@ public function getDocuments()
*
* @return array
*/
public function getDocument($id)
public function getDocument($id): array
{
$params = [
'index' => $this->sourceIndexManager->getLiveIndex(),
Expand Down
18 changes: 9 additions & 9 deletions Document/Provider/ProviderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

/**
* References persistence providers for each index and type.
* References persistence providers for each index.
*/
class ProviderRegistry implements ContainerAwareInterface
{
Expand Down Expand Up @@ -53,9 +53,9 @@ public function __construct(


/**
* Registers a provider service for the specified type entity.
* Registers a provider service for the specified document class.
*
* @param string $documentClass The FQN or alias to the type entity
* @param string $documentClass The FQN or alias to the document class
* @param string $providerId The provider service id
*/
public function addProvider(string $documentClass, string $providerId) : void
Expand All @@ -64,19 +64,19 @@ public function addProvider(string $documentClass, string $providerId) : void
}

/**
* Unsets registered provider for the specified type entity.
* Unsets registered provider for the specified document class.
*
* @param string $documentClass The FQN or alias to the type entity
* @param string $documentClass The FQN or alias to the document class
*/
public function removeProvider(string $documentClass) : void
{
unset($this->providers[$this->documentMetadataCollector->getDocumentMetadata($documentClass)->getClassName()]);
}

/**
* Gets registered provider service id for the specified type entity.
* Gets registered provider service id for the specified document class.
*
* @param string $documentClass The FQN or alias to the type entity
* @param string $documentClass The FQN or alias to the document class
*
* @return string|null
*/
Expand All @@ -88,13 +88,13 @@ public function getProviderId(string $documentClass) : ?string
}

/**
* Gets the provider for a type.
* Gets the provider for a document class.
*
* @param string $documentClass FQN or alias (e.g App:Entity)
*
* @return ProviderInterface
*
* @throws \InvalidArgumentException if no provider was registered for the type
* @throws \InvalidArgumentException if no provider was registered for the document class
*/
public function getProviderInstance(string $documentClass) : ProviderInterface
{
Expand Down
2 changes: 1 addition & 1 deletion Document/Repository/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Repository
protected $finder;

/**
* The type metadata
* The document metadata
*
* @var DocumentMetadata
*/
Expand Down
22 changes: 7 additions & 15 deletions Mapping/DocumentMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ protected function configureOptions(OptionsResolver $optionsResolver)
}

/**
* Retrieves type mapping for the Elasticsearch client
* Retrieves index mapping for the Elasticsearch client
*
* @return array
*/
public function getClientMapping()
public function getClientMapping(): array
{
$mapping = array_filter(
array_merge(
Expand All @@ -64,48 +64,40 @@ function ($value) {
/**
* @return array
*/
public function getProperties()
public function getProperties(): array
{
return $this->metadata['properties'];
}

/**
* @return array
*/
public function getPropertiesMetadata()
public function getPropertiesMetadata(): array
{
return $this->metadata['propertiesMetadata'];
}

/**
* @return array
*/
public function getFields()
public function getFields(): array
{
return $this->metadata['fields'];
}

/**
* @return string|null
*/
public function getRepositoryClass()
public function getRepositoryClass(): ?string
{
return $this->metadata['repositoryClass'];
}

/**
* @return string
*/
public function getClassName()
public function getClassName(): string
{
return $this->metadata['className'];
}

/**
* @return string
*/
public function getType()
{
return $this->metadata['type'];
}
}
2 changes: 1 addition & 1 deletion Resources/doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Creates a new index in Elasticsearch for the specified manager with the configur

Command name: `sineflow:es:index:build <index_manager_name>`

Rebuilds the data in the specified index, using the configured data providers for each type in the index. If no data providers are configured, by default a *self* provider is registered for each type, so the index would be rebuilt from itself - useful when mapping has changed and you need to update it.
Rebuilds the data in the specified index, using the configured data provider. If no data provider is configured, by default a *self* provider is registered, so the index would be rebuilt from itself - useful when mapping has changed, and you need to update it.

An important thing to note is that currently this command will only work if you have set `use_aliases: true` in your index configuration. What it does is, it creates a new index and points the *write* alias to it, as well as to the old one.
When building the new index is complete without errors, both read and write aliases are pointed to it and removed from the old one.
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/dataproviders.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Simply put, data providers are the sources of data for your Elasticsearch indice

By default, each index is assigned a default *self* data provider (`ElasticsearchProvider`), which retrieves the data from the Elasticsearch index itself. This is useful when you want to rebuild the index (like if you changed the mapping and want to update it).

In order to define your own custom data provider for a certain type, you have to create a service that implements `ProviderInterface` and tag it as `sfes.provider`, specifying the entity it is for in the `type` argument:
In order to define your own custom data provider for an index, you have to create a service that implements `ProviderInterface` and tag it as `sfes.provider`, specifying the entity it is for in the `type` argument:

```
services:
Expand Down
4 changes: 1 addition & 3 deletions Resources/doc/mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The Elasticsearch bundle requires document mapping definitions to create the cor

## Document class annotations

Elasticsearch type mappings are defined using annotations within document entity classes that implement DocumentInterface:
Elasticsearch index mappings are defined using annotations within document entity classes that implement DocumentInterface:
```php
<?php
namespace App\Document;
Expand Down Expand Up @@ -46,8 +46,6 @@ Each field within the document is specified using the `@ES\Property` annotation.

- `name` Specifies the name of the field (required).

- `type` Specifies the type of the field in Elasticsearch (required).

- `multilanguage` A flag that specifies whether the field will be multilanguage. For more information, see [declaring multilanguage properties](#mlproperties).
```
multilanguage=true
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ $productsCount = $repo->count($searchBody);

## Searching in multiple types and indices

It is convenient to search in a single type as shown above, but sometime you may wish to search in multiple indices and/or types. The finder service comes in play:
It is convenient to search in a single index as shown above, but sometime you may wish to search in multiple indices. The finder service comes in play:

```php
$finder = $this->get('sfes.finder');
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sineflow_elasticsearch:

> This is the very basic example only, for a more detailed description of configuration options, please take a look at the [configuration](configuration.md) chapter.
A couple of things to note in this example: `dev_customer` is the name of the physical index in Elasticsearch and `App:Customer` represents the class where the document type mapping is defined. (more info at [the mapping chapter](mapping.md)).
A couple of things to note in this example: `dev_customer` is the name of the physical index in Elasticsearch and `App:Customer` represents the class where the document mapping is defined. (more info at [the mapping chapter](mapping.md)).


### Step 4: Define your Elasticsearch types as `Document` objects
Expand Down
18 changes: 10 additions & 8 deletions Result/DocumentConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DocumentConverter
* @param DocumentMetadataCollector $metadataCollector
* @param string $languageSeparator
*/
public function __construct(DocumentMetadataCollector $metadataCollector, $languageSeparator)
public function __construct(DocumentMetadataCollector $metadataCollector, string $languageSeparator)
{
$this->metadataCollector = $metadataCollector;
$this->languageSeparator = $languageSeparator;
Expand All @@ -45,7 +45,7 @@ public function __construct(DocumentMetadataCollector $metadataCollector, $langu
*
* @return DocumentInterface
*/
public function convertToDocument($rawData, $documentClass)
public function convertToDocument(array $rawData, string $documentClass)
{
// Get document metadata
$metadata = $this->metadataCollector->getDocumentMetadata($documentClass);
Expand Down Expand Up @@ -94,7 +94,7 @@ public function convertToDocument($rawData, $documentClass)
*
* @return ObjectInterface
*/
public function assignArrayToObject(array $array, ObjectInterface $object, array $propertiesMetadata)
public function assignArrayToObject(array $array, ObjectInterface $object, array $propertiesMetadata): ObjectInterface
{
foreach ($propertiesMetadata as $esField => $propertyMetadata) {
// Skip fields from the mapping that have no value set, unless they are multilanguage fields
Expand Down Expand Up @@ -160,8 +160,10 @@ public function assignArrayToObject(array $array, ObjectInterface $object, array
* @param array $propertiesMetadata
*
* @return array
*
* @throws \ReflectionException
*/
public function convertToArray(ObjectInterface $object, $propertiesMetadata = [])
public function convertToArray(ObjectInterface $object, $propertiesMetadata = []): array
{
if (empty($propertiesMetadata)) {
$propertiesMetadata = $this->metadataCollector->getObjectPropertiesMetadata(get_class($object));
Expand Down Expand Up @@ -209,18 +211,18 @@ public function convertToArray(ObjectInterface $object, $propertiesMetadata = []
}

/**
* Check if object is the correct type
* Check if object is the correct class
*
* @param ObjectInterface $object
* @param array $expectedClass
* @param string $expectedClass
*
* @throws \InvalidArgumentException
*/
private function checkObjectType(ObjectInterface $object, $expectedClass)
private function checkObjectType(ObjectInterface $object, string $expectedClass)
{
if (get_class($object) !== $expectedClass) {
throw new \InvalidArgumentException(
sprintf('Expected object of type "%s", got "%s"', $expectedClass, get_class($object))
sprintf('Expected object of "%s", got "%s"', $expectedClass, get_class($object))
);
}
}
Expand Down

0 comments on commit 5141e84

Please sign in to comment.