Completed
Push — master ( cd9d30...5f41c9 )
by Karel
13s
created

TransformedFinder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 98
wmc 9
lcom 1
cbo 9
ccs 30
cts 30
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A find() 0 6 1
A findHybrid() 0 6 1
A moreLikeThis() 0 7 1
A search() 0 10 2
A findPaginated() 0 7 1
A createPaginatorAdapter() 0 6 1
A createHybridPaginatorAdapter() 0 6 1
1
<?php
2
3
namespace FOS\ElasticaBundle\Finder;
4
5
use Elastica\Document;
6
use FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter;
7
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
8
use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter;
9
use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter;
10
use Pagerfanta\Pagerfanta;
11
use Elastica\SearchableInterface;
12
use Elastica\Query;
13
14
/**
15
 * Finds elastica documents and map them to persisted objects.
16
 */
17
class TransformedFinder implements PaginatedFinderInterface
18
{
19
    protected $searchable;
20
    protected $transformer;
21
22
    /**
23
     * @param SearchableInterface                 $searchable
24
     * @param ElasticaToModelTransformerInterface $transformer
25
     */
26 7
    public function __construct(SearchableInterface $searchable, ElasticaToModelTransformerInterface $transformer)
27
    {
28 7
        $this->searchable  = $searchable;
29 7
        $this->transformer = $transformer;
30 7
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 1
    public function find($query, $limit = null, $options = array())
36
    {
37 1
        $results = $this->search($query, $limit, $options);
38
39 1
        return $this->transformer->transform($results);
40
    }
41
42 1
    public function findHybrid($query, $limit = null, $options = array())
43
    {
44 1
        $results = $this->search($query, $limit, $options);
45
46 1
        return $this->transformer->hybridTransform($results);
47
    }
48
49
    /**
50
     * Find documents similar to one with passed id.
51
     *
52
     * @param integer $id
53
     * @param array   $params
54
     * @param array   $query
55
     *
56
     * @return array of model objects
57
     **/
58 1
    public function moreLikeThis($id, $params = array(), $query = array())
59
    {
60 1
        $doc = new Document($id);
61 1
        $results = $this->searchable->moreLikeThis($doc, $params, $query)->getResults();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Elastica\SearchableInterface as the method moreLikeThis() does only exist in the following implementations of said interface: Elastica\Type.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
62
63 1
        return $this->transformer->transform($results);
64
    }
65
66
    /**
67
     * @param $query
68
     * @param null|int $limit
69
     * @param array    $options
70
     *
71
     * @return array
72
     */
73 1
    protected function search($query, $limit = null, $options = array())
74
    {
75 1
        $queryObject = Query::create($query);
76 1
        if (null !== $limit) {
77 1
            $queryObject->setSize($limit);
78
        }
79 1
        $results = $this->searchable->search($queryObject, $options)->getResults();
80
81 1
        return $results;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    public function findPaginated($query, $options = array())
88
    {
89 1
        $queryObject = Query::create($query);
90 1
        $paginatorAdapter = $this->createPaginatorAdapter($queryObject, $options);
91
92 1
        return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 2
    public function createPaginatorAdapter($query, $options = array())
99
    {
100 2
        $query = Query::create($query);
101
102 2
        return new TransformedPaginatorAdapter($this->searchable, $query, $options, $this->transformer);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 1
    public function createHybridPaginatorAdapter($query)
109
    {
110 1
        $query = Query::create($query);
111
112 1
        return new HybridPaginatorAdapter($this->searchable, $query, $this->transformer);
113
    }
114
}
115