Completed
Pull Request — develop (#284)
by
unknown
08:39
created

ResultConverter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 76
rs 10
c 1
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C convert() 0 41 8
A factory() 0 4 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Solr\Bridge;
11
12
use Core\Repository\RepositoryService;
13
use Solr\Filter\AbstractPaginationQuery;
14
use Zend\ServiceManager\ServiceLocatorInterface;
15
use ArrayAccess;
16
use InvalidArgumentException;
17
18
/**
19
 * Class ResultConverter
20
 *
21
 * Convert SOLR query result into Doctrine ODM Entity
22
 *
23
 * @author Anthonius Munthi <[email protected]>
24
 * @author Miroslav Fedeleš <[email protected]>
25
 * @package Solr\Bridge
26
 * @since 0.26
27
 */
28
class ResultConverter
29
{
30
31
    /**
32
     * @var RepositoryService
33
     */
34
    protected $repositories;
35
36
    /**
37
     * @param RepositoryService $repositories
38
     */
39
    public function __construct(RepositoryService $repositories)
40
    {
41
        $this->repositories = $repositories;
42
    }
43
44
    /**
45
     * Convert result into entities
46
     *
47
     * @param AbstractPaginationQuery $filter
48
     * @param ArrayAccess $response
49
     * @return array Array of entities
50
     * @throws InvalidArgumentException
51
     */
52
    public function convert(AbstractPaginationQuery $filter, ArrayAccess $response)
53
    {
54
        $entities = [];
55
        $ids = [];
56
        $return = [];
57
58
        if (!isset($response['response'])
59
            || !isset($response['response']['docs'])
60
            || !is_array($response['response']['docs']))
61
        {
62
            throw new InvalidArgumentException('invalid response');
63
        }
64
        
65
        foreach ($response['response']['docs'] as $doc) {
66
            $ids[] = $doc->id;
67
        }
68
69
        // fetch entities with given IDs
70
        $repository = $this->repositories->get($filter->getRepositoryName());
71
        $qb = $repository->createQueryBuilder()
72
            ->field('id')
73
            ->in($ids);
74
        $result = $qb->getQuery()->execute();
75
        
76
        foreach ($result as $document) {
77
            $entities[$document->getId()] = $document;
78
        }
79
        
80
        // iterate over Solr response to preserve sorting
81
        foreach ($response['response']['docs'] as $doc) {
82
            // check if entity exists
83
            if (!isset($entities[$doc->id])) {
84
                // skip non-existent entity
85
                continue;
86
            }
87
            
88
            $return[] = $filter->proxyFactory($entities[$doc->id], $doc);
89
        }
90
        
91
        return $return;
92
    }
93
94
    /**
95
     * Create a new instance of ResultConverter
96
     * @param   ServiceLocatorInterface $sl
97
     * @return  ResultConverter
98
     */
99
    static public function factory(ServiceLocatorInterface $sl)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
100
    {
101
        return new static($sl->get('repositories'));
0 ignored issues
show
Documentation introduced by
$sl->get('repositories') is of type object|array, but the function expects a object<Core\Repository\RepositoryService>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
    }
103
}