Completed
Pull Request — develop (#241)
by ANTHONIUS
07:36
created

JobBoardPaginationQuery::convertOrganizationName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
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 Jobs\Entity\Location;
14
use Jobs\Entity\Job;
15
use Organizations\Entity\Organization;
16
use Organizations\Entity\OrganizationImage;
17
use Organizations\Entity\OrganizationName;
18
19
/**
20
 * Class JobBoardPaginationQuery
21
 *
22
 * @author  Anthonius Munthi <[email protected]>
23
 * @since   0.27
24
 * @package Solr\Filter
25
 */
26
class JobBoardPaginationQuery extends AbstractPaginationQuery
27
{
28
    /**
29
     * @var array
30
     */
31
    protected $sortPropertiesMap = [
32
        'company' => 'companyName',
33
        'date'    => 'dateCreated',
34
    ];
35
36
    protected $propertiesMap = [
37
        'organizationName' => 'convertOrganizationName',
38
        'companyLogo'      => 'convertCompanyLogo',
39
    ];
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function createQuery(array $params, $query)
45
    {
46
        $search = isset($params['search']) ? $params['search']:'';
47
48
        if(!empty($search)){
49
            $q = 'title:'.$search.' OR organizationName:'.$search;
50
        }else{
51
            $q = '*:*';
52
        }
53
54
        $query->setQuery($q);
55
        
56
        if(isset($params['sort'])){
57
            $sorts = $this->filterSort($params['sort']);
58
            foreach($sorts as $field=>$order){
59
                $query->addSortField($field,$order);
60
            }
61
        }
62
63
        if(isset($params['location'])){
64
            /* @var Location $location */
65
            $location = $params['location'];
66
            if(!is_null($location->getCoordinates())){
67
                $coordinates = $location->getCoordinates()->getCoordinates();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class GeoJson\GeoJson as the method getCoordinates() does only exist in the following sub-classes of GeoJson\GeoJson: GeoJson\Geometry\Geometry, GeoJson\Geometry\GeometryCollection, GeoJson\Geometry\LineString, GeoJson\Geometry\LinearRing, GeoJson\Geometry\MultiLineString, GeoJson\Geometry\MultiPoint, GeoJson\Geometry\MultiPolygon, GeoJson\Geometry\Point, GeoJson\Geometry\Polygon, Geo\Entity\Geometry\Point. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
68
                $query->addFilterQuery(sprintf(
69
                    "{!geofilt pt=%s sfield=latLon d=%s}",
70
                    doubleval($coordinates[0]).','.doubleval($coordinates[1]),
71
                    $params['d']
72
                ));
73
            }
74
        }
75
76
        return $query;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function getEntityClass()
83
    {
84
        return Job::class;
85
    }
86
87
    /**
88
     * Convert organizationName result
89
     * @param Job       $ob
90
     * @param string    $value
91
     */
92
    public function convertOrganizationName($ob,$value)
93
    {
94
        if(!is_object($ob->getOrganization())){
95
            $ob->setOrganization(new Organization());
96
        }
97
        $orgName = new OrganizationName($value);
98
        $ob->getOrganization()->setOrganizationName($orgName);
99
    }
100
101
    /**
102
     * Convert companyLogo result
103
     * @param   Job     $ob
104
     * @param   mixed   $value
105
     */
106
    public function convertCompanyLogo($ob,$value)
107
    {
108
        if(!is_object($ob->getOrganization())){
109
            $ob->setOrganization(new Organization());
110
        }
111
        $exp    = explode('/',$value);
112
        $id     = $exp[3];
113
        $name   = $exp[4];
114
        $image = new OrganizationImage();
115
        $image->setId($id);
116
        $image->setName($name);
117
        $ob->getOrganization()->setImage($image);
118
    }
119
}