Completed
Pull Request — develop (#241)
by ANTHONIUS
08:03
created

JobBoardPaginationQuery::convertCompanyLogo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
6
 * @license   MIT
7
 */
8
9
namespace Solr\Filter;
10
11
12
use Jobs\Entity\Location;
13
use Solr\Bridge\JobImportTrait;
14
use Jobs\Entity\Job;
15
use Organizations\Entity\Organization;
16
use Organizations\Entity\OrganizationImage;
17
use Organizations\Entity\OrganizationName;
18
19
class JobBoardPaginationQuery extends AbstractPaginationQuery
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $sortPropertiesMap = [
25
        'company' => 'companyName',
26
        'date'    => 'dateCreated',
27
    ];
28
29
    protected $propertiesMap = [
30
        'organizationName' => 'convertOrganizationName',
31
        'companyLogo'      => 'convertCompanyLogo',
32
    ];
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function createQuery(array $params, $query)
38
    {
39
        $search = isset($params['search']) ? $params['search']:'';
40
41
        if(!empty($search)){
42
            $q = 'title:'.$search.' OR organizationName:'.$search;
43
        }else{
44
            $q = '*:*';
45
        }
46
47
        $query->setQuery($q);
48
        
49
        if(isset($params['sort'])){
50
            $sorts = $this->filterSort($params['sort']);
51
            foreach($sorts as $field=>$order){
52
                $query->addSortField($field,$order);
53
            }
54
        }
55
56
        if(isset($params['location'])){
57
            /* @var Location $location */
58
            $location = $params['location'];
59
            if(!is_null($location->getCoordinates())){
60
                $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...
61
                $query->addFilterQuery(sprintf(
62
                    "{!geofilt pt=%s sfield=latLon d=%s}",
63
                    doubleval($coordinates[0]).','.doubleval($coordinates[1]),
64
                    $params['d']
65
                ));
66
            }
67
        }
68
69
        return $query;
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function getEntityClass()
76
    {
77
        return Job::class;
78
    }
79
80
    /**
81
     * Convert organizationName result
82
     * @param Job       $ob
83
     * @param string    $value
84
     */
85
    public function convertOrganizationName($ob,$value)
86
    {
87
        if(!is_object($ob->getOrganization())){
88
            $ob->setOrganization(new Organization());
89
        }
90
        $orgName = new OrganizationName($value);
91
        $ob->getOrganization()->setOrganizationName($orgName);
92
    }
93
94
    /**
95
     * Convert companyLogo result
96
     * @param   Job     $ob
97
     * @param   mixed   $value
98
     */
99
    public function convertCompanyLogo($ob,$value)
100
    {
101
        if(!is_object($ob->getOrganization())){
102
            $ob->setOrganization(new Organization());
103
        }
104
        $exp    = explode('/',$value);
105
        $id     = $exp[3];
106
        $name   = $exp[4];
107
        $image = new OrganizationImage();
108
        $image->setId($id);
109
        $image->setName($name);
110
        $ob->getOrganization()->setImage($image);
111
    }
112
}