1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Doctrine; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
15
|
|
|
use FOS\ElasticaBundle\Provider\PagerfantaPager; |
16
|
|
|
use FOS\ElasticaBundle\Provider\PagerProviderInterface; |
17
|
|
|
use Pagerfanta\Adapter\DoctrineODMMongoDBAdapter; |
18
|
|
|
use Pagerfanta\Pagerfanta; |
19
|
|
|
|
20
|
|
View Code Duplication |
final class MongoDBPagerProvider implements PagerProviderInterface |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $objectClass; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ManagerRegistry |
29
|
|
|
*/ |
30
|
|
|
private $doctrine; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $baseOptions; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ManagerRegistry $doctrine |
39
|
|
|
* @param string $objectClass |
40
|
|
|
* @param array $baseOptions |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ManagerRegistry $doctrine, $objectClass, array $baseOptions) |
43
|
|
|
{ |
44
|
|
|
$this->doctrine = $doctrine; |
45
|
|
|
$this->objectClass = $objectClass; |
46
|
|
|
$this->baseOptions = $baseOptions; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function provide(array $options = array()) |
53
|
|
|
{ |
54
|
|
|
$options = array_replace($this->baseOptions, $options); |
55
|
|
|
|
56
|
|
|
$adapter = new DoctrineODMMongoDBAdapter($this->createQueryBuilder($options['query_builder_method'])); |
57
|
|
|
|
58
|
|
|
return new PagerfantaPager(new Pagerfanta($adapter)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
private function createQueryBuilder($method) |
65
|
|
|
{ |
66
|
|
|
$repository = $this->doctrine |
67
|
|
|
->getManagerForClass($this->objectClass) |
68
|
|
|
->getRepository($this->objectClass); |
69
|
|
|
|
70
|
|
|
return call_user_func([$repository, $method]); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.