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

JobEventSubscriber::processLocation()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 2 Features 1
Metric Value
c 7
b 2
f 1
dl 0
loc 26
rs 8.8571
cc 3
eloc 20
nc 3
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\Listener;
11
12
13
use Doctrine\Common\EventSubscriber;
14
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
15
use Doctrine\ODM\MongoDB\Events;
16
use Jobs\Entity\Job;
17
use Solr\Bridge\Manager;
18
use Solr\Bridge\Util;
19
use Zend\ServiceManager\ServiceLocatorInterface;
20
21
/**
22
 * Class JobEventSubscriber
23
 *
24
 * @author  Anthonius Munthi <[email protected]>
25
 * @since   0.26
26
 * @package Solr\Event\Listener
27
 */
28
class JobEventSubscriber implements EventSubscriber
29
{
30
31
    /**
32
     * @var Manager
33
     */
34
    protected $solrManager;
35
    /**
36
     * JobEventSubscriber constructor.
37
     * @param Manager $manager
38
     */
39
    public function __construct(Manager $manager)
40
    {
41
        $this->solrManager = $manager;
42
    }
43
    /**
44
     * Define what event this subscriber listen to
45
     *
46
     * @return array
47
     */
48
    public function getSubscribedEvents()
49
    {
50
        return [
51
            Events::postUpdate,
52
            Events::postPersist,
53
        ];
54
    }
55
    public function consoleIndex(Job $job)
56
    {
57
        $this->updateIndex($job);
58
    }
59
    /**
60
     * Handle doctrine post persist event
61
     *
62
     * @param LifecycleEventArgs $eventArgs
63
     */
64
    public function postPersist(LifecycleEventArgs $eventArgs)
65
    {
66
        $document = $eventArgs->getDocument();
67
        $this->updateIndex($document);
68
    }
69
    /**
70
     * Handle doctrine postUpdate event
71
     *
72
     * @param LifecycleEventArgs $eventArgs
73
     */
74
    public function postUpdate(LifecycleEventArgs $eventArgs)
75
    {
76
        $document = $eventArgs->getDocument();
77
        $this->updateIndex($document);
78
    }
79
    /**
80
     * @param ServiceLocatorInterface $serviceLocator
81
     * @return mixed
82
     */
83
    static public function factory(ServiceLocatorInterface $serviceLocator)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
84
    {
85
        /* @var Manager $manager */
86
        $manager = $serviceLocator->get('Solr/Manager');
87
        return new self($manager);
88
    }
89
    /**
90
     * @param $document
91
     */
92
    protected function updateIndex($document)
93
    {
94
        if(!$document instanceof Job){
95
            return;
96
        }
97
        $solrDoc = $this->generateInputDocument($document, new \SolrInputDocument());
98
        try{
99
            $this->solrManager->addDocument($solrDoc,$this->solrManager->getOptions()->getJobsPath());
100
        }catch (\Exception $e){
101
            // @TODO: What to do when the process failed?
102
        }
103
    }
104
    /**
105
     * Generate input document
106
     *
107
     * @param   Job                 $job
108
     * @param   \SolrInputDocument  $document
109
     * @return  \SolrInputDocument
110
     */
111
    public function generateInputDocument(Job $job, $document)
112
    {
113
        $document->addField('id',$job->getId());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
114
        $document->addField('entityName','job');
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
115
        $document->addField('title',$job->getTitle());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
116
        $document->addField('applicationEmail',$job->getContactEmail());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
117
        if ($job->getLink()) {
118
            $document->addField('link', $job->getLink());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
119
            $oldErrorReporting=error_reporting(0);
120
            $dom = new \DOMDocument();
121
            $dom->loadHTML($job->getLink());
122
            error_reporting($oldErrorReporting);
123
            $document->addField('html',$dom->saveHTML());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
124
        }
125
        if($job->getDateCreated()){
126
            $document->addField('dateCreated',Util::convertDateTime($job->getDateCreated()));
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
127
        }
128
        if($job->getDateModified()){
129
            $document->addField('dateModified',Util::convertDateTime($job->getDateModified()));
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
130
        }
131
        if($job->getDatePublishStart()){
132
            $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...
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
133
        }
134
        if($job->getDatePublishEnd()){
135
            $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...
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
136
        }
137
        $document->addField('isActive',$job->isActive());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
138
        $document->addField('lang',$job->getLanguage());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
139
        $this->processLocation($job,$document);
140
        if(!is_null($job->getOrganization())){
141
            try {
142
                $this->processOrganization($job,$document);
143
            }catch (\Exception $e){
144
                // @TODO: What to do when the process failed?
145
            }
146
        }
147
        return $document;
148
    }
149
    /**
150
     * Processing organization part
151
     *
152
     * @param Job                   $job
153
     * @param \SolrInputDocument    $document
154
     */
155
    public function processOrganization(Job $job,$document)
156
    {
157
        if(!is_null($job->getOrganization()->getImage())){
158
            $uri = $job->getOrganization()->getImage()->getUri();
159
            $document->addField('companyLogo',$uri);
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
160
        }
161
        $document->addField('organizationName',$job->getOrganization()->getOrganizationName()->getName());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
162
        $document->addField('organizationId',$job->getOrganization()->getId());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
163
    }
164
165
    /**
166
     * Processing location part
167
     * @param Job                $job
168
     * @param \SolrInputDocument $document
169
     */
170
    public function processLocation(Job $job,$document)
171
    {
172
        /* @var \Jobs\Entity\Location $location */
173
        $locations=$job->getLocations();
174
        foreach($locations as $location){
175
176
            $loc = new \SolrInputDocument();
177
            $loc->addField('entityName', 'location');
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
178
            if(is_object($location->getCoordinates())){
179
                $coordinate = Util::convertLocationCoordinates($location);
180
                $loc->addField('point', $coordinate);
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
181
                $loc->addField('latLon', $coordinate);
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
182
                $document->addField('locations', $coordinate);
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
183
                $document->addField('points', $coordinate);
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
184
                $loc->addField('id', $job->getId() . '-' . $coordinate);
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
185
                $loc->addField('city', $location->getCity());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
186
                $loc->addField('country', $location->getCountry());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
187
                $loc->addField('region', $location->getRegion());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
188
                $loc->addField('postalCode', $location->getPostalCode());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
189
                $document->addField('regionList',$location->getRegion());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
190
                $document->addChildDocument($loc);
0 ignored issues
show
Bug introduced by
The method addChildDocument() does not seem to exist on object<SolrInputDocument>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191
            }
192
            unset($loc);
193
        }
194
        $document->addField('location',$job->getLocation());
0 ignored issues
show
Bug introduced by
The call to addField() misses a required argument $fieldBoostValue.

This check looks for function calls that miss required arguments.

Loading history...
195
    }
196
}