Completed
Push — master ( 564fde...8048fc )
by Karel
06:22
created

TransformedFinder::moreLikeThis()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
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\Finder;
13
14
use Elastica\Document;
15
use Elastica\Query;
16
use Elastica\SearchableInterface;
17
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter;
18
use FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter;
19
use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter;
20
use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter;
21
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
22
use Pagerfanta\Pagerfanta;
23
24
/**
25
 * Finds elastica documents and map them to persisted objects.
26
 */
27
class TransformedFinder implements PaginatedFinderInterface
28
{
29
    protected $searchable;
30
    protected $transformer;
31
32
    /**
33
     * @param SearchableInterface                 $searchable
34
     * @param ElasticaToModelTransformerInterface $transformer
35
     */
36 8
    public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer)
37
    {
38 8
        $this->searchable = $searchable;
39 8
        $this->transformer = $transformer;
40 8
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function find($query, $limit = null, $options = array())
46
    {
47 1
        $results = $this->search($query, $limit, $options);
48
49 1
        return $this->transformer->transform($results);
50
    }
51
52 1
    public function findHybrid($query, $limit = null, $options = array())
53
    {
54 1
        $results = $this->search($query, $limit, $options);
55
56 1
        return $this->transformer->hybridTransform($results);
57
    }
58
59
    /**
60
     * @param $query
61
     * @param null|int $limit
62
     * @param array    $options
63
     *
64
     * @return array
65
     */
66
    protected function search($query, $limit = null, $options = array())
67
    {
68 1
        $queryObject = Query::create($query);
69
        if (null !== $limit) {
70 1
            $queryObject->setSize($limit);
71 1
        }
72
        $results = $this->searchable->search($queryObject, $options)->getResults();
73 1
74
        return $results;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function findPaginated($query, $options = array())
81
    {
82
        $queryObject = Query::create($query);
83 1
        $paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options);
84
85 1
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
86 1
    }
87 1
88
    /**
89 1
     * {@inheritdoc}
90
     */
91 1
    public function createPaginatorAdapter($query, $options = array())
92
    {
93
        $query = Query::create($query);
94
95
        return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
96
    }
97 1
98
    /**
99 1
     * {@inheritdoc}
100 1
     */
101
    public function createHybridPaginatorAdapter($query)
102 1
    {
103
        $query = Query::create($query);
104
105
        return new HybridPaginatorAdapter($this->searchable, $query, $this->transformer);
106
    }
107
108 2
    /**
109
     * {@inheritdoc}
110 2
     */
111
    public function createRawPaginatorAdapter($query, $options = array())
112 2
    {
113
        $query = Query::create($query);
114
115
        return new RawPaginatorAdapter($this->searchable, $query, $options);
116
    }
117
}
118