Merge branch 'code-qa' into 3.1.x

This commit is contained in:
Tim Nagel 2015-03-13 15:17:52 +11:00
commit dd3269d1ef
16 changed files with 108 additions and 85 deletions

View file

@ -502,7 +502,7 @@ class FOSElasticaExtension extends Extension
break;
}
if ($tagName) {
if (null !== $tagName) {
foreach ($this->getDoctrineEvents($typeConfig) as $event) {
$listenerDef->addTag($tagName, array('event' => $event));
}
@ -527,7 +527,6 @@ class FOSElasticaExtension extends Extension
break;
default:
throw new InvalidArgumentException(sprintf('Cannot determine events for driver "%s"', $typeConfig['driver']));
break;
}
$events = array();

View file

@ -2,20 +2,22 @@
namespace FOS\ElasticaBundle\Doctrine;
use Doctrine\Common\Persistence\ManagerRegistry;
use FOS\ElasticaBundle\HybridResult;
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer as BaseTransformer;
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
/**
* Maps Elastica documents with Doctrine objects
* This mapper assumes an exact match between
* elastica documents ids and doctrine object ids.
*/
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface
abstract class AbstractElasticaToModelTransformer extends BaseTransformer
{
/**
* Manager registry.
*
* @var ManagerRegistry
*/
protected $registry = null;
@ -38,21 +40,14 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
'query_builder_method' => 'createQueryBuilder',
);
/**
* PropertyAccessor instance.
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/**
* Instantiates a new Mapper.
*
* @param object $registry
* @param ManagerRegistry $registry
* @param string $objectClass
* @param array $options
*/
public function __construct($registry, $objectClass, array $options = array())
public function __construct(ManagerRegistry $registry, $objectClass, array $options = array())
{
$this->registry = $registry;
$this->objectClass = $objectClass;
@ -69,16 +64,6 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
return $this->objectClass;
}
/**
* Set the PropertyAccessor.
*
* @param PropertyAccessorInterface $propertyAccessor
*/
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{
$this->propertyAccessor = $propertyAccessor;
}
/**
* Transforms an array of elastica objects into an array of
* model objects fetched from the doctrine repository.
@ -111,10 +96,7 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
// sort objects in the order of ids
$idPos = array_flip($ids);
$identifier = $this->options['identifier'];
$propertyAccessor = $this->propertyAccessor;
usort($objects, function ($a, $b) use ($idPos, $identifier, $propertyAccessor) {
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
});
usort($objects, $this->getSortingClosure($idPos, $identifier));
return $objects;
}
@ -138,7 +120,7 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
}
/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function getIdentifierField()
{

View file

@ -71,7 +71,7 @@ abstract class AbstractProvider extends BaseAbstractProvider
$objects = $this->getSlice($queryBuilder, $batchSize, $offset, $objects);
$objects = array_filter($objects, array($this, 'isObjectIndexable'));
if ($objects) {
if (!empty($objects)) {
if (!$ignoreErrors) {
$this->objectPersister->insertMany($objects);
} else {

View file

@ -3,8 +3,8 @@
namespace FOS\ElasticaBundle\Doctrine;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
use FOS\ElasticaBundle\Persister\ObjectPersister;
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
use FOS\ElasticaBundle\Provider\IndexableInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
@ -19,14 +19,14 @@ class Listener
/**
* Object persister.
*
* @var ObjectPersister
* @var ObjectPersisterInterface
*/
protected $objectPersister;
/**
* Configuration for the listener.
*
* @var string
* @var array
*/
private $config;
@ -84,7 +84,7 @@ class Listener
$this->objectPersister = $objectPersister;
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
if ($logger) {
if ($logger && $this->objectPersister instanceof ObjectPersister) {
$this->objectPersister->setLogger($logger);
}
}

View file

@ -6,6 +6,11 @@ use FOS\ElasticaBundle\Elastica\Index;
class IndexManager
{
/**
* @var Index
*/
private $defaultIndex;
/**
* @var array
*/

View file

@ -38,13 +38,13 @@ class MappingBuilder
}
$mapping = array();
if ($typeMappings) {
if (!empty($typeMappings)) {
$mapping['mappings'] = $typeMappings;
}
// 'warmers' => $indexConfig->getWarmers(),
$settings = $indexConfig->getSettings();
if ($settings) {
if (!empty($settings)) {
$mapping['settings'] = $settings;
}
@ -95,7 +95,7 @@ class MappingBuilder
$mapping['_meta']['model'] = $typeConfig->getModel();
}
if (!$mapping) {
if (empty($mapping)) {
// Empty mapping, we want it encoded as a {} instead of a []
$mapping = new \stdClass();
}

View file

@ -10,6 +10,15 @@ namespace FOS\ElasticaBundle\Persister;
*/
interface ObjectPersisterInterface
{
/**
* Checks if this persister can handle the given object or not.
*
* @param mixed $object
*
* @return boolean
*/
public function handlesObject($object);
/**
* Insert one object into the type
* The object will be transformed to an elastica document.
@ -66,13 +75,4 @@ interface ObjectPersisterInterface
* @param array $identifiers array of domain model object identifiers
*/
public function deleteManyByIdentifiers(array $identifiers);
/**
* If the object persister handles the given object.
*
* @param object $object
*
* @return bool
*/
public function handlesObject($object);
}

View file

@ -18,12 +18,16 @@ class ObjectSerializerPersister extends ObjectPersister
protected $serializer;
/**
* @param Type $type
* @param ModelToElasticaTransformerInterface $transformer
* @param string $objectClass
* @param callable $serializer
*/
public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, $serializer)
{
parent::__construct($type, $transformer, $objectClass, array());
$this->serializer = $serializer;
$this->serializer = $serializer;
}
/**

View file

@ -3,6 +3,7 @@
namespace FOS\ElasticaBundle\Propel;
use FOS\ElasticaBundle\HybridResult;
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer;
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@ -14,7 +15,7 @@ use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
*
* @author William Durand <william.durand1@gmail.com>
*/
class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
class ElasticaToModelTransformer extends AbstractElasticaToModelTransformer
{
/**
* Propel model class to map to Elastica documents.
@ -33,13 +34,6 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
'identifier' => 'id',
);
/**
* PropertyAccessor instance.
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/**
* Constructor.
*
@ -52,16 +46,6 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
$this->options = array_merge($this->options, $options);
}
/**
* Set the PropertyAccessor instance.
*
* @param PropertyAccessorInterface $propertyAccessor
*/
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{
$this->propertyAccessor = $propertyAccessor;
}
/**
* Transforms an array of Elastica document into an array of Propel entities
* fetched from the database.
@ -82,11 +66,7 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
// Sort objects in the order of their IDs
$idPos = array_flip($ids);
$identifier = $this->options['identifier'];
$propertyAccessor = $this->propertyAccessor;
$sortCallback = function ($a, $b) use ($idPos, $identifier, $propertyAccessor) {
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
};
$sortCallback = $this->getSortingClosure($idPos, $identifier);
if (is_object($objects)) {
$objects->uasort($sortCallback);
@ -105,7 +85,7 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
$objects = $this->transform($elasticaObjects);
$result = array();
for ($i = 0; $i < count($elasticaObjects); $i++) {
for ($i = 0, $j = count($elasticaObjects); $i < $j; $i++) {
$result[] = new HybridResult($elasticaObjects[$i], $objects[$i]);
}

View file

@ -23,10 +23,8 @@ class Callback
{
$this->groups = $groups;
if ($this->groups) {
if (!$this->serializer instanceof SerializerInterface) {
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer".');
}
if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface) {
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer".');
}
}
@ -34,10 +32,8 @@ class Callback
{
$this->version = $version;
if ($this->version) {
if (!$this->serializer instanceof SerializerInterface) {
throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".');
}
if ($this->version && !$this->serializer instanceof SerializerInterface) {
throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".');
}
}

View file

@ -58,7 +58,6 @@ class AbstractProviderTest extends \PHPUnit_Framework_TestCase
->with('index', 'type', $this->anything())
->will($this->returnValue(true));
$providerInvocationOffset = 2;
$previousSlice = array();
foreach ($objectsByIteration as $i => $objects) {

View file

@ -13,12 +13,12 @@ namespace FOS\ElasticaBundle\Tests\Functional\app\ORM;
class IndexableService
{
public function isIndexable($object)
public function isIndexable()
{
return true;
}
public static function isntIndexable($object)
public static function isntIndexable()
{
return false;
}

View file

@ -37,7 +37,7 @@ class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCa
$this->collection = new ElasticaToModelTransformerCollection($this->transformers = array(
'type1' => $transformer1,
'type2' => $transformer2,
), array());
));
}
public function testGetObjectClass()

View file

@ -0,0 +1,51 @@
<?php
/**
* This file is part of the FOSElasticaBundle project.
*
* (c) FriendsOfSymfony <https://github.com/FriendsOfSymfony/FOSElasticaBundle/graphs/contributors>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\ElasticaBundle\Transformer;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface
{
/**
* PropertyAccessor instance.
*
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;
/**
* Set the PropertyAccessor instance.
*
* @param PropertyAccessorInterface $propertyAccessor
*/
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
{
$this->propertyAccessor = $propertyAccessor;
}
/**
* Returns a sorting closure to be used with usort() to put retrieved objects
* back in the order that they were returned by ElasticSearch.
*
* @param array $idPos
* @param string $identifierPath
* @return callable
*/
protected function getSortingClosure(array $idPos, $identifierPath)
{
$propertyAccessor = $this->propertyAccessor;
return function ($a, $b) use ($idPos, $identifierPath, $propertyAccessor) {
return $idPos[$propertyAccessor->getValue($a, $identifierPath)] > $idPos[$propertyAccessor->getValue($b, $identifierPath)];
};
}
}

View file

@ -81,7 +81,7 @@ class ElasticaToModelTransformerCollection implements ElasticaToModelTransformer
$objects = $this->transform($elasticaObjects);
$result = array();
for ($i = 0; $i < count($elasticaObjects); $i++) {
for ($i = 0, $j = count($elasticaObjects); $i < $j; $i++) {
$result[] = new HybridResult($elasticaObjects[$i], $objects[$i]);
}

View file

@ -3,10 +3,17 @@
namespace FOS\ElasticaBundle\Transformer;
/**
* Maps Elastica documents with model objects.
* Indicates that the model should have elastica highlights injected.
*/
interface HighlightableModelInterface
{
/**
* Returns a unique identifier for the model.
*
* @return mixed
*/
public function getId();
/**
* Set ElasticSearch highlight data.
*