Completed
Pull Request — develop (#291)
by
unknown
08:11
created

Job::filter()   C

Complexity

Conditions 8
Paths 65

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 43
rs 5.3846
cc 8
eloc 31
nc 65
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 * @author Miroslav Fedeleš <[email protected]>
9
 * @since 0.27
10
 */
11
namespace Solr\Filter\EntityToDocument;
12
13
use Zend\Filter\FilterInterface;
14
use Jobs\Entity\Job as JobEntity;
15
use SolrInputDocument;
16
use InvalidArgumentException;
17
use Solr\Bridge\Util;
18
use Zend\Filter\StripTags;
19
20
class Job implements FilterInterface
21
{
22
23
    /**
24
     * @see \Zend\Filter\FilterInterface::filter()
25
     * @param JobEntity $job
26
     * @return SolrInputDocument
27
     */
28
    public function filter($job)
29
    {
30
        if (!$job instanceof JobEntity) {
31
            throw new InvalidArgumentException(sprintf('$job must be instance of "%s"', JobEntity::class));
32
        }
33
        
34
        $document = new SolrInputDocument();
35
        $document->addField('id', $job->getId());
36
        $document->addField('applyId', $job->getApplyId());
37
        $document->addField('entityName', 'job');
38
        $document->addField('title', $job->getTitle());
39
        $document->addField('applicationEmail', $job->getContactEmail());
40
        if ($job->getLink()) {
41
            $document->addField('link', $job->getLink());
42
        }
43
        if ($job->getDateCreated()) {
44
            $document->addField('dateCreated', Util::convertDateTime($job->getDateCreated()));
45
        }
46
        if ($job->getDateModified()) {
47
            $document->addField('dateModified', Util::convertDateTime($job->getDateModified()));
48
        }
49
        if ($job->getDatePublishStart()) {
50
            $document->addField('datePublishStart', Util::convertDateTime($job->getDatePublishStart()));
0 ignored issues
show
Documentation introduced by
$job->getDatePublishStart() is of type string, but the function expects a object<DateTime>.

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...
51
        }
52
        if ($job->getDatePublishEnd()) {
53
            $document->addField('datePublishEnd', Util::convertDateTime($job->getDatePublishEnd()));
0 ignored issues
show
Documentation introduced by
$job->getDatePublishEnd() is of type string, but the function expects a object<DateTime>.

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...
54
        }
55
        $document->addField('isActive', $job->isActive());
0 ignored issues
show
Documentation introduced by
$job->isActive() is of type boolean, but the function expects a string.

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...
56
        $document->addField('lang', $job->getLanguage());
57
        $this->processLocation($job, $document);
58
        if (!is_null($job->getOrganization())) {
59
            $this->processOrganization($job, $document);
60
        }
61
        $templateValues = $job->getTemplateValues();
62
        $description = $templateValues->getDescription();
63
        $stripTags = new StripTags();
64
        $stripTags->setAttributesAllowed([])->setTagsAllowed([]);
65
        $description = $stripTags->filter($description);
66
        
67
        $document->addField('html', $description);
68
        
69
        return $document;
70
    }
71
    
72
    /**
73
     * @param JobEntity $job
74
     * @return array
75
     */
76
    public function getDocumentIds(JobEntity $job)
77
    {
78
        $ids = [$job->getId()];
79
            
80
        /* @var $location \Jobs\Entity\Location */
81
        foreach ($job->getLocations() as $location) {
82
            if (is_object($location->getCoordinates())) {
83
                $ids[] = $this->getLocationDocumentId($job, Util::convertLocationCoordinates($location));
84
            }
85
        }
86
        
87
        return $ids;
88
    }
89
    
90
    /**
91
     * @param JobEntity $job
92
     * @param SolrInputDocument $document
93
     */
94
    public function processOrganization(JobEntity $job, SolrInputDocument $document)
95
    {
96
        if (!is_null($job->getOrganization()->getImage())) {
97
            $uri = $job->getOrganization()
98
                ->getImage()
99
                ->getUri();
100
            $document->addField('companyLogo', $uri);
101
        }
102
        $document->addField('organizationName', $job->getOrganization()->getOrganizationName()->getName());
103
        $document->addField('organizationId', $job->getOrganization()->getId());
104
    }
105
106
    /**
107
     * @param JobEntity $job
108
     * @param SolrInputDocument $document
109
     */
110
    public function processLocation(JobEntity $job, SolrInputDocument $document)
111
    {
112
        /* @var $location \Jobs\Entity\Location */
113
        foreach ($job->getLocations() as $location) {
114
            $loc = new SolrInputDocument();
115
            $loc->addField('entityName', 'location');
116
            if (is_object($location->getCoordinates())) {
117
                $coordinate = Util::convertLocationCoordinates($location);
118
                $region = $location->getRegion();
119
                $loc->addField('point', $coordinate);
120
                $loc->addField('latLon', $coordinate);
121
                $document->addField('locations', $coordinate);
122
                $document->addField('points', $coordinate);
123
                $loc->addField('id', $this->getLocationDocumentId($job, $coordinate));
124
                $loc->addField('city', $location->getCity());
125
                $loc->addField('country', $location->getCountry());
126
                $loc->addField('region', $region);
127
                $loc->addField('postalCode', $location->getPostalCode());
128
                $document->addField('regionList', $region);
129
                $document->addChildDocument($loc);
130
            }
131
        }
132
        
133
        $document->addField('location', $job->getLocation());
134
    }
135
    
136
    /**
137
     * @param JobEntity $job
138
     * @param string $coordinate
139
     * @return string
140
     */
141
    protected function getLocationDocumentId(JobEntity $job, $coordinate)
142
    {
143
        return $job->getId() . '-' . $coordinate;
144
    }
145
}