Completed
Pull Request — develop (#284)
by
unknown
09:16
created

ResultConverter::validateDate()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 14
rs 8.8571
c 1
b 0
f 1
cc 5
eloc 10
nc 5
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A ResultConverter::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
        $emptyEntity = null;
58
59
        if (!isset($response['response'])
60
            || !isset($response['response']['docs'])
61
            || !is_array($response['response']['docs']))
62
        {
63
            throw new InvalidArgumentException('invalid response');
64
        }
65
        
66
        foreach ($response['response']['docs'] as $doc) {
67
            $ids[] = $doc->id;
68
        }
69
70
        // fetch entities with given IDs
71
        $repository = $this->repositories->get($filter->getRepositoryName());
72
        $qb = $repository->createQueryBuilder() /* @var $repository \Core\Repository\AbstractRepository */
73
            ->field('id')
74
            ->in($ids);
75
        $result = $qb->getQuery()->execute();
76
        
77
        foreach ($result as $document) {
78
            $entities[$document->getId()] = $document;
79
        }
80
        
81
        // iterate over Solr response to preserve sorting
82
        foreach ($response['response']['docs'] as $doc) {
83
            // check if entity exists
84
            if (isset($entities[$doc->id])) {
85
                // use found entity
86
                $entity = $entities[$doc->id];
87
            } else {
88
                if (!isset($emptyEntity)) {
89
                    // create empty entity
90
                    $emptyEntity = $repository->create();
0 ignored issues
show
Bug introduced by
The method create() does not exist on Doctrine\ODM\MongoDB\DocumentRepository. Did you maybe mean createQueryBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
91
                }
92
                
93
                // use empty entity
94
                $entity = $emptyEntity;
95
            }
96
            
97
            $return[] = $filter->proxyFactory($entity, $doc);
98
        }
99
        
100
        return $return;
101
    }
102
103
    /**
104
     * Create a new instance of ResultConverter
105
     * @param   ServiceLocatorInterface $sl
106
     * @return  ResultConverter
107
     */
108
    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...
109
    {
110
        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...
111
    }
112
}