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 Zend\ServiceManager\ServiceLocatorInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class JobEventSubscriber |
22
|
|
|
* |
23
|
|
|
* @author Anthonius Munthi <[email protected]> |
24
|
|
|
* @since 0.27 |
25
|
|
|
* @package Solr\Event\Listener |
26
|
|
|
*/ |
27
|
|
|
class JobEventSubscriber implements EventSubscriber |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Manager |
31
|
|
|
*/ |
32
|
|
|
protected $solrManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* JobEventSubscriber constructor. |
36
|
|
|
* @param Manager $manager |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Manager $manager) |
39
|
|
|
{ |
40
|
|
|
$this->solrManager = $manager; |
41
|
|
|
} |
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
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Handle doctrine post persist event |
58
|
|
|
* |
59
|
|
|
* @param LifecycleEventArgs $eventArgs |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function postPersist(LifecycleEventArgs $eventArgs) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$document = $eventArgs->getDocument(); |
64
|
|
|
|
65
|
|
|
if (!$document instanceof Job) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$solrDoc = $this->generateInputDocument($document, new \SolrInputDocument()); |
70
|
|
|
try{ |
71
|
|
|
$this->solrManager->addDocument($solrDoc,'/solr/YawikJobs'); |
72
|
|
|
}catch (\Exception $e){ |
73
|
|
|
// @TODO: What to do when the process failed? |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Handle doctrine postUpdate event |
79
|
|
|
* |
80
|
|
|
* @param LifecycleEventArgs $eventArgs |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function postUpdate(LifecycleEventArgs $eventArgs) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$document = $eventArgs->getDocument(); |
85
|
|
|
if (!$document instanceof Job) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$solrDoc = $this->generateInputDocument($document,new \SolrInputDocument()); |
90
|
|
|
try{ |
91
|
|
|
$this->solrManager->addDocument($solrDoc,'/solr/YawikJobs'); |
92
|
|
|
}catch (\Exception $e){ |
93
|
|
|
// @TODO: What to do when the process failed? |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
static public function factory(ServiceLocatorInterface $serviceLocator) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$manager = $serviceLocator->get('Solr/Manager'); |
104
|
|
|
return new static($manager); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Generate input document |
109
|
|
|
* |
110
|
|
|
* @param Job $job |
111
|
|
|
* @param \SolrInputDocument $document |
112
|
|
|
* @return \SolrInputDocument |
113
|
|
|
*/ |
114
|
|
|
public function generateInputDocument(Job $job, $document) |
115
|
|
|
{ |
116
|
|
|
$document->addField('id',$job->getId()); |
117
|
|
|
$document->addField('title',$job->getTitle()); |
118
|
|
|
$document->addField('applicationEmail',$job->getContactEmail()); |
119
|
|
|
|
120
|
|
|
if($job->getDateCreated()){ |
121
|
|
|
$document->addField('dateCreated', |
122
|
|
|
$job->getDateCreated()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT) |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
if($job->getDateModified()){ |
126
|
|
|
$document->addField('dateModified', |
127
|
|
|
$job->getDateModified()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
if($job->getDatePublishStart()){ |
131
|
|
|
$document->addField('datePublishStart', |
132
|
|
|
$job->getDatePublishStart()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT) |
|
|
|
|
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if($job->getDatePublishEnd()){ |
137
|
|
|
$document->addField('datePublishEnd', |
138
|
|
|
$job->getDatePublishEnd()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT) |
|
|
|
|
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$document->addField('isActive',$job->isActive()); |
143
|
|
|
$document->addField('lang',$job->getLanguage()); |
144
|
|
|
|
145
|
|
|
$this->processLocation($job,$document); |
146
|
|
|
if(!is_null($job->getOrganization())){ |
147
|
|
|
$this->processOrganization($job,$document); |
148
|
|
|
} |
149
|
|
|
return $document; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Processing organization part |
154
|
|
|
* |
155
|
|
|
* @param Job $job |
156
|
|
|
* @param \SolrInputDocument $document |
157
|
|
|
*/ |
158
|
|
|
public function processOrganization(Job $job,$document) |
159
|
|
|
{ |
160
|
|
|
if(!is_null($job->getOrganization()->getImage())){ |
161
|
|
|
$uri = $job->getOrganization()->getImage()->getUri(); |
162
|
|
|
$document->addField('companyLogo',$uri); |
163
|
|
|
} |
164
|
|
|
$document->addField('organizationName',$job->getOrganization()->getOrganizationName()->getName()); |
165
|
|
|
$document->addField('organizationId',$job->getOrganization()->getId()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Processing location part |
170
|
|
|
* @param Job $job |
171
|
|
|
* @param \SolrInputDocument $document |
172
|
|
|
*/ |
173
|
|
|
public function processLocation(Job $job,$document) |
174
|
|
|
{ |
175
|
|
|
/* @var \Jobs\Entity\Location $location */ |
176
|
|
|
foreach($job->getLocations() as $location){ |
177
|
|
|
$coord = $location->getCoordinates()->getCoordinates(); |
178
|
|
|
$document->addField('latLon',doubleval($coord[0]).','.doubleval($coord[1])); |
179
|
|
|
$document->addField('postCode',$location->getPostalCode()); |
180
|
|
|
$document->addField('regionText',$location->getRegion()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$document->addField('location',$job->getLocation()); |
184
|
|
|
} |
185
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.