Completed
Pull Request — develop (#254)
by Carsten
07:52
created

JobBoardPaginationQuery::createQuery()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 2
Metric Value
c 7
b 1
f 2
dl 0
loc 56
rs 7.7489
cc 7
eloc 35
nc 24
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Filter;
11
12
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Jobs\Entity\Location;
15
use Jobs\Entity\Job;
16
use Organizations\Entity\Organization;
17
use Organizations\Entity\OrganizationImage;
18
use Organizations\Entity\OrganizationName;
19
use Solr\Bridge\Util;
20
21
/**
22
 * Class JobBoardPaginationQuery
23
 *
24
 * @author  Anthonius Munthi <[email protected]>
25
 * @since   0.26
26
 * @package Solr\Filter
27
 */
28
class JobBoardPaginationQuery extends AbstractPaginationQuery
29
{
30
    /**
31
     * @var array
32
     */
33
    protected $sortPropertiesMap = [
34
        'company' => 'companyName',
35
        'date'    => 'dateCreated',
36
    ];
37
38
    protected $propertiesMap = [
39
        'organizationName' => 'convertOrganizationName',
40
        'companyLogo'      => 'convertCompanyLogo',
41
        'locations'        => 'convertLocations'
42
    ];
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function createQuery(array $params, $query)
48
    {
49
        $search = isset($params['search']) ? $params['search']:'';
50
51
        if(!empty($search)){
52
            $q = \SolrUtils::escapeQueryChars($search);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $q is correct as \SolrUtils::escapeQueryChars($search) (which targets SolrUtils::escapeQueryChars()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53
        }else{
54
            $q = '*:*';
55
        }
56
57
        $query->setQuery($q);
58
        $query->addFilterQuery('entityName:job');
59
        $query->addField('*');
60
        
61
        if(isset($params['sort'])){
62
            $sorts = $this->filterSort($params['sort']);
63
            foreach($sorts as $field=>$order){
64
                $query->addSortField($field,$order);
65
            }
66
        }
67
68
        if(isset($params['location'])){
69
            /* @var Location $location */
70
            $location = $params['location'];
71
            if(is_object($location->getCoordinates())){
72
                $coordinate = Util::convertLocationCoordinates($location);
73
74
                $query->addFilterQuery(
75
                    sprintf(
76
                        '{!parent which="entityName:job" childQuery="entityName:location"}{!geofilt pt=%s sfield=point d=%d score="kilometers"}',
77
                        $coordinate,
78
                        $params['d']
79
                    ));
80
                $query->addParam(
81
                    'locations.q',
82
                    sprintf(
83
                        'entityName:location AND {!terms f=_root_ v=$row.id} AND {!geofilt pt=%s sfield=point d=%s}',
84
                        $coordinate,
85
                        $params['d']
86
                    )); // join
87
88
                $query->addField('locations:[subquery]')
89
                      ->addField('distance:min(geodist(points,'.$coordinate.'))');
90
91
            }
92
93
            $query->addField('score');
94
95
            $query->setFacet(true);
96
            $query->addFacetField('regionList');
97
            $query->addFacetDateField('datePublishStart');
98
99
        }
100
101
        return $query;
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function getEntityClass()
108
    {
109
        return Job::class;
110
    }
111
112
    /**
113
     * Convert organizationName result
114
     * @param Job       $ob
115
     * @param string    $value
116
     */
117
    public function convertOrganizationName($ob,$value)
118
    {
119
        if(!is_object($ob->getOrganization())){
120
            $ob->setOrganization(new Organization());
121
        }
122
        $orgName = new OrganizationName($value);
123
        $ob->getOrganization()->setOrganizationName($orgName);
124
    }
125
126
    /**
127
     * Convert companyLogo result
128
     * @param   Job     $ob
129
     * @param   mixed   $value
130
     */
131
    public function convertCompanyLogo($ob,$value)
132
    {
133
        if(!is_object($ob->getOrganization())){
134
            $ob->setOrganization(new Organization());
135
        }
136
        $exp    = explode('/',$value);
137
        $id     = $exp[3];
138
        $name   = isset($exp[4])?:null;
139
        $image = new OrganizationImage();
140
        $image->setId($id);
141
        $image->setName($name);
142
        $ob->getOrganization()->setImage($image);
143
    }
144
145
    /**
146
     * Convert locations result
147
     * @param   Job     $ob
148
     * @param   mixed   $value
149
     */
150
    public function convertLocations($ob,$value)
151
    {
152
        $locations = [];
153
        foreach($value->docs as $doc) {
154
            $locations[] = $doc->city;
155
        }
156
        $ob->setLocation(implode(', ', array_unique($locations)));
157
    }
158
}