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\Event\Listener; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Doctrine\Common\EventSubscriber; |
13
|
|
|
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; |
14
|
|
|
use Doctrine\ODM\MongoDB\Events; |
15
|
|
|
use Jobs\Entity\Job; |
16
|
|
|
use Solr\Bridge\Manager; |
17
|
|
|
use Solr\Exception\ListenerException; |
18
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
19
|
|
|
|
20
|
|
|
class JobEventSubscriber implements EventSubscriber |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Manager |
24
|
|
|
*/ |
25
|
|
|
protected $solrManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* JobEventSubscriber constructor. |
29
|
|
|
* @param Manager $manager |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Manager $manager) |
32
|
|
|
{ |
33
|
|
|
$this->solrManager = $manager; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getSubscribedEvents() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
Events::postUpdate, |
40
|
|
|
Events::postPersist, |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function postPersist(LifecycleEventArgs $eventArgs) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$document = $eventArgs->getDocument(); |
47
|
|
|
|
48
|
|
|
if (!$document instanceof Job) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$solrDoc = $this->generateInputDocument($document, new \SolrInputDocument()); |
53
|
|
|
try{ |
54
|
|
|
$this->solrManager->addDocument($solrDoc,'/solr/YawikJobs'); |
55
|
|
|
}catch (\Exception $e){ |
56
|
|
|
// @TODO: What to do when the process failed? |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function postUpdate(LifecycleEventArgs $eventArgs) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$document = $eventArgs->getDocument(); |
63
|
|
|
if (!$document instanceof Job) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$solrDoc = $this->generateInputDocument($document,new \SolrInputDocument()); |
68
|
|
|
try{ |
69
|
|
|
$this->solrManager->addDocument($solrDoc,'/solr/YawikJobs'); |
70
|
|
|
}catch (\Exception $e){ |
71
|
|
|
// @TODO: What to do when the process failed? |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
static public function factory(ServiceLocatorInterface $serviceLocator) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$manager = $serviceLocator->get('Solr/Manager'); |
82
|
|
|
return new static($manager); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Job $job |
87
|
|
|
* @return \SolrInputDocument |
88
|
|
|
*/ |
89
|
|
|
public function generateInputDocument(Job $job, $document) |
90
|
|
|
{ |
91
|
|
|
$document->addField('id',$job->getId()); |
92
|
|
|
$document->addField('title',$job->getTitle()); |
93
|
|
|
$document->addField('applicationEmail',$job->getContactEmail()); |
94
|
|
|
|
95
|
|
|
if($job->getDateCreated()){ |
96
|
|
|
$document->addField('dateCreated', |
97
|
|
|
$job->getDateCreated()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
if($job->getDateModified()){ |
101
|
|
|
$document->addField('dateModified', |
102
|
|
|
$job->getDateModified()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
if($job->getDatePublishStart()){ |
106
|
|
|
$document->addField('datePublishStart', |
107
|
|
|
$job->getDatePublishStart()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT) |
|
|
|
|
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if($job->getDatePublishEnd()){ |
112
|
|
|
$document->addField('datePublishEnd', |
113
|
|
|
$job->getDatePublishEnd()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT) |
|
|
|
|
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$document->addField('isActive',$job->isActive()); |
118
|
|
|
$document->addField('lang',$job->getLanguage()); |
119
|
|
|
|
120
|
|
|
$this->processLocation($job,$document); |
121
|
|
|
if(!is_null($job->getOrganization())){ |
122
|
|
|
$this->processOrganization($job,$document); |
123
|
|
|
} |
124
|
|
|
return $document; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function processOrganization(Job $job,$document) |
128
|
|
|
{ |
129
|
|
|
if(!is_null($job->getOrganization()->getImage())){ |
130
|
|
|
$uri = $job->getOrganization()->getImage()->getUri(); |
131
|
|
|
$document->addField('companyLogo',$uri); |
132
|
|
|
} |
133
|
|
|
$document->addField('organizationName',$job->getOrganization()->getOrganizationName()->getName()); |
134
|
|
|
// @TODO: uncomment this when organization id is fix |
135
|
|
|
//$document->addField('organizationId',$job->getOrganization()->getId()); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function processLocation(Job $job,$document) |
139
|
|
|
{ |
140
|
|
|
/* @var \Jobs\Entity\Location $location */ |
141
|
|
|
foreach($job->getLocations() as $location){ |
142
|
|
|
$coord = $location->getCoordinates()->getCoordinates(); |
143
|
|
|
$document->addField('latLon',doubleval($coord[0]).','.doubleval($coord[1])); |
144
|
|
|
$document->addField('postCode',$location->getPostalCode()); |
145
|
|
|
$document->addField('regionText',$location->getRegion()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$document->addField('location',$job->getLocation()); |
149
|
|
|
} |
150
|
|
|
} |
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.