Skip to content

Commit dd3269d

Browse files
committed
Merge branch 'code-qa' into 3.1.x
2 parents 925410a + 84e5831 commit dd3269d

16 files changed

+108
-85
lines changed

DependencyInjection/FOSElasticaExtension.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ private function loadTypeListener(array $typeConfig, ContainerBuilder $container
502502
break;
503503
}
504504

505-
if ($tagName) {
505+
if (null !== $tagName) {
506506
foreach ($this->getDoctrineEvents($typeConfig) as $event) {
507507
$listenerDef->addTag($tagName, array('event' => $event));
508508
}
@@ -527,7 +527,6 @@ private function getDoctrineEvents(array $typeConfig)
527527
break;
528528
default:
529529
throw new InvalidArgumentException(sprintf('Cannot determine events for driver "%s"', $typeConfig['driver']));
530-
break;
531530
}
532531

533532
$events = array();

Doctrine/AbstractElasticaToModelTransformer.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
namespace FOS\ElasticaBundle\Doctrine;
44

5+
use Doctrine\Common\Persistence\ManagerRegistry;
56
use FOS\ElasticaBundle\HybridResult;
6-
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
7+
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer as BaseTransformer;
78
use FOS\ElasticaBundle\Transformer\HighlightableModelInterface;
8-
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
99

1010
/**
1111
* Maps Elastica documents with Doctrine objects
1212
* This mapper assumes an exact match between
1313
* elastica documents ids and doctrine object ids.
1414
*/
15-
abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTransformerInterface
15+
abstract class AbstractElasticaToModelTransformer extends BaseTransformer
1616
{
1717
/**
1818
* Manager registry.
19+
*
20+
* @var ManagerRegistry
1921
*/
2022
protected $registry = null;
2123

@@ -38,21 +40,14 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
3840
'query_builder_method' => 'createQueryBuilder',
3941
);
4042

41-
/**
42-
* PropertyAccessor instance.
43-
*
44-
* @var PropertyAccessorInterface
45-
*/
46-
protected $propertyAccessor;
47-
4843
/**
4944
* Instantiates a new Mapper.
5045
*
51-
* @param object $registry
46+
* @param ManagerRegistry $registry
5247
* @param string $objectClass
5348
* @param array $options
5449
*/
55-
public function __construct($registry, $objectClass, array $options = array())
50+
public function __construct(ManagerRegistry $registry, $objectClass, array $options = array())
5651
{
5752
$this->registry = $registry;
5853
$this->objectClass = $objectClass;
@@ -69,16 +64,6 @@ public function getObjectClass()
6964
return $this->objectClass;
7065
}
7166

72-
/**
73-
* Set the PropertyAccessor.
74-
*
75-
* @param PropertyAccessorInterface $propertyAccessor
76-
*/
77-
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
78-
{
79-
$this->propertyAccessor = $propertyAccessor;
80-
}
81-
8267
/**
8368
* Transforms an array of elastica objects into an array of
8469
* model objects fetched from the doctrine repository.
@@ -111,10 +96,7 @@ public function transform(array $elasticaObjects)
11196
// sort objects in the order of ids
11297
$idPos = array_flip($ids);
11398
$identifier = $this->options['identifier'];
114-
$propertyAccessor = $this->propertyAccessor;
115-
usort($objects, function ($a, $b) use ($idPos, $identifier, $propertyAccessor) {
116-
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
117-
});
99+
usort($objects, $this->getSortingClosure($idPos, $identifier));
118100

119101
return $objects;
120102
}
@@ -138,7 +120,7 @@ public function hybridTransform(array $elasticaObjects)
138120
}
139121

140122
/**
141-
* {@inheritdoc}
123+
* {@inheritDoc}
142124
*/
143125
public function getIdentifierField()
144126
{

Doctrine/AbstractProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function populate(\Closure $loggerClosure = null, array $options = array(
7171
$objects = $this->getSlice($queryBuilder, $batchSize, $offset, $objects);
7272
$objects = array_filter($objects, array($this, 'isObjectIndexable'));
7373

74-
if ($objects) {
74+
if (!empty($objects)) {
7575
if (!$ignoreErrors) {
7676
$this->objectPersister->insertMany($objects);
7777
} else {

Doctrine/Listener.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace FOS\ElasticaBundle\Doctrine;
44

55
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
6-
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
76
use FOS\ElasticaBundle\Persister\ObjectPersister;
7+
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
88
use FOS\ElasticaBundle\Provider\IndexableInterface;
99
use Psr\Log\LoggerInterface;
1010
use Symfony\Component\PropertyAccess\PropertyAccess;
@@ -19,14 +19,14 @@ class Listener
1919
/**
2020
* Object persister.
2121
*
22-
* @var ObjectPersister
22+
* @var ObjectPersisterInterface
2323
*/
2424
protected $objectPersister;
2525

2626
/**
2727
* Configuration for the listener.
2828
*
29-
* @var string
29+
* @var array
3030
*/
3131
private $config;
3232

@@ -84,7 +84,7 @@ public function __construct(
8484
$this->objectPersister = $objectPersister;
8585
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
8686

87-
if ($logger) {
87+
if ($logger && $this->objectPersister instanceof ObjectPersister) {
8888
$this->objectPersister->setLogger($logger);
8989
}
9090
}

Index/IndexManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
class IndexManager
88
{
9+
/**
10+
* @var Index
11+
*/
12+
private $defaultIndex;
13+
914
/**
1015
* @var array
1116
*/

Index/MappingBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public function buildIndexMapping(IndexConfig $indexConfig)
3838
}
3939

4040
$mapping = array();
41-
if ($typeMappings) {
41+
if (!empty($typeMappings)) {
4242
$mapping['mappings'] = $typeMappings;
4343
}
4444
// 'warmers' => $indexConfig->getWarmers(),
4545

4646
$settings = $indexConfig->getSettings();
47-
if ($settings) {
47+
if (!empty($settings)) {
4848
$mapping['settings'] = $settings;
4949
}
5050

@@ -95,7 +95,7 @@ public function buildTypeMapping(TypeConfig $typeConfig)
9595
$mapping['_meta']['model'] = $typeConfig->getModel();
9696
}
9797

98-
if (!$mapping) {
98+
if (empty($mapping)) {
9999
// Empty mapping, we want it encoded as a {} instead of a []
100100
$mapping = new \stdClass();
101101
}

Persister/ObjectPersisterInterface.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
*/
1111
interface ObjectPersisterInterface
1212
{
13+
/**
14+
* Checks if this persister can handle the given object or not.
15+
*
16+
* @param mixed $object
17+
*
18+
* @return boolean
19+
*/
20+
public function handlesObject($object);
21+
1322
/**
1423
* Insert one object into the type
1524
* The object will be transformed to an elastica document.
@@ -66,13 +75,4 @@ public function deleteMany(array $objects);
6675
* @param array $identifiers array of domain model object identifiers
6776
*/
6877
public function deleteManyByIdentifiers(array $identifiers);
69-
70-
/**
71-
* If the object persister handles the given object.
72-
*
73-
* @param object $object
74-
*
75-
* @return bool
76-
*/
77-
public function handlesObject($object);
7878
}

Persister/ObjectSerializerPersister.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ class ObjectSerializerPersister extends ObjectPersister
1818
protected $serializer;
1919

2020
/**
21+
* @param Type $type
22+
* @param ModelToElasticaTransformerInterface $transformer
2123
* @param string $objectClass
24+
* @param callable $serializer
2225
*/
2326
public function __construct(Type $type, ModelToElasticaTransformerInterface $transformer, $objectClass, $serializer)
2427
{
2528
parent::__construct($type, $transformer, $objectClass, array());
26-
$this->serializer = $serializer;
29+
30+
$this->serializer = $serializer;
2731
}
2832

2933
/**

Propel/ElasticaToModelTransformer.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FOS\ElasticaBundle\Propel;
44

55
use FOS\ElasticaBundle\HybridResult;
6+
use FOS\ElasticaBundle\Transformer\AbstractElasticaToModelTransformer;
67
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
78
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
89

@@ -14,7 +15,7 @@
1415
*
1516
* @author William Durand <[email protected]>
1617
*/
17-
class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
18+
class ElasticaToModelTransformer extends AbstractElasticaToModelTransformer
1819
{
1920
/**
2021
* Propel model class to map to Elastica documents.
@@ -33,13 +34,6 @@ class ElasticaToModelTransformer implements ElasticaToModelTransformerInterface
3334
'identifier' => 'id',
3435
);
3536

36-
/**
37-
* PropertyAccessor instance.
38-
*
39-
* @var PropertyAccessorInterface
40-
*/
41-
protected $propertyAccessor;
42-
4337
/**
4438
* Constructor.
4539
*
@@ -52,16 +46,6 @@ public function __construct($objectClass, array $options = array())
5246
$this->options = array_merge($this->options, $options);
5347
}
5448

55-
/**
56-
* Set the PropertyAccessor instance.
57-
*
58-
* @param PropertyAccessorInterface $propertyAccessor
59-
*/
60-
public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
61-
{
62-
$this->propertyAccessor = $propertyAccessor;
63-
}
64-
6549
/**
6650
* Transforms an array of Elastica document into an array of Propel entities
6751
* fetched from the database.
@@ -82,11 +66,7 @@ public function transform(array $elasticaObjects)
8266
// Sort objects in the order of their IDs
8367
$idPos = array_flip($ids);
8468
$identifier = $this->options['identifier'];
85-
$propertyAccessor = $this->propertyAccessor;
86-
87-
$sortCallback = function ($a, $b) use ($idPos, $identifier, $propertyAccessor) {
88-
return $idPos[$propertyAccessor->getValue($a, $identifier)] > $idPos[$propertyAccessor->getValue($b, $identifier)];
89-
};
69+
$sortCallback = $this->getSortingClosure($idPos, $identifier);
9070

9171
if (is_object($objects)) {
9272
$objects->uasort($sortCallback);
@@ -105,7 +85,7 @@ public function hybridTransform(array $elasticaObjects)
10585
$objects = $this->transform($elasticaObjects);
10686

10787
$result = array();
108-
for ($i = 0; $i < count($elasticaObjects); $i++) {
88+
for ($i = 0, $j = count($elasticaObjects); $i < $j; $i++) {
10989
$result[] = new HybridResult($elasticaObjects[$i], $objects[$i]);
11090
}
11191

Serializer/Callback.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,17 @@ public function setGroups(array $groups)
2323
{
2424
$this->groups = $groups;
2525

26-
if ($this->groups) {
27-
if (!$this->serializer instanceof SerializerInterface) {
28-
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer".');
29-
}
26+
if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface) {
27+
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer".');
3028
}
3129
}
3230

3331
public function setVersion($version)
3432
{
3533
$this->version = $version;
3634

37-
if ($this->version) {
38-
if (!$this->serializer instanceof SerializerInterface) {
39-
throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".');
40-
}
35+
if ($this->version && !$this->serializer instanceof SerializerInterface) {
36+
throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".');
4137
}
4238
}
4339

0 commit comments

Comments
 (0)