Completed
Pull Request — develop (#246)
by ANTHONIUS
07:21
created

JobEventSubscriber::processOrganization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
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 Zend\ServiceManager\ServiceLocatorInterface;
19
20
/**
21
 * Class JobEventSubscriber
22
 *
23
 * @author  Anthonius Munthi <[email protected]>
24
 * @since   0.26
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
    public function consoleIndex(Job $job)
57
    {
58
        $this->updateIndex($job);
59
    }
60
61
62
63
    /**
64
     * Handle doctrine post persist event
65
     *
66
     * @param LifecycleEventArgs $eventArgs
67
     */
68
    public function postPersist(LifecycleEventArgs $eventArgs)
69
    {
70
        $document = $eventArgs->getDocument();
71
        $this->updateIndex($document);
72
    }
73
74
    /**
75
     * Handle doctrine postUpdate event
76
     *
77
     * @param LifecycleEventArgs $eventArgs
78
     */
79
    public function postUpdate(LifecycleEventArgs $eventArgs)
80
    {
81
        $document = $eventArgs->getDocument();
82
        $this->updateIndex($document);
83
    }
84
85
    /**
86
     * @param ServiceLocatorInterface $serviceLocator
87
     * @return mixed
88
     */
89
    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...
90
    {
91
        /* @var Manager $manager */
92
        $manager = $serviceLocator->get('Solr/Manager');
93
        return new self($manager);
94
    }
95
96
    /**
97
     * @param $document
98
     */
99
    protected function updateIndex($document)
100
    {
101
        if(!$document instanceof Job){
102
            return;
103
        }
104
        $solrDoc = $this->generateInputDocument($document, new \SolrInputDocument());
105
        try{
106
            $this->solrManager->addDocument($solrDoc,$this->solrManager->getOptions()->getJobsPath());
107
        }catch (\Exception $e){
108
            // @TODO: What to do when the process failed?
109
        }
110
    }
111
112
    /**
113
     * Generate input document
114
     *
115
     * @param   Job                 $job
116
     * @param   \SolrInputDocument  $document
117
     * @return  \SolrInputDocument
118
     */
119
    public function generateInputDocument(Job $job, $document)
120
    {
121
        $document->addField('id',$job->getId());
122
        $document->addField('title',$job->getTitle());
123
        $document->addField('applicationEmail',$job->getContactEmail());
124
125
        if($job->getDateCreated()){
126
            $document->addField('dateCreated',
127
                $job->getDateCreated()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT)
128
            );
129
        }
130
        if($job->getDateModified()){
131
            $document->addField('dateModified',
132
                $job->getDateModified()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT)
133
            );
134
        }
135
        if($job->getDatePublishStart()){
136
            $document->addField('datePublishStart',
137
                $job->getDatePublishStart()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT)
0 ignored issues
show
Bug introduced by
The method setTimezone cannot be called on $job->getDatePublishStart() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
138
            );
139
        }
140
141
        if($job->getDatePublishEnd()){
142
            $document->addField('datePublishEnd',
143
                $job->getDatePublishEnd()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT)
0 ignored issues
show
Bug introduced by
The method setTimezone cannot be called on $job->getDatePublishEnd() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
144
            );
145
        }
146
147
        $document->addField('isActive',$job->isActive());
148
        $document->addField('lang',$job->getLanguage());
149
150
        $this->processLocation($job,$document);
151
        if(!is_null($job->getOrganization())){
152
            $this->processOrganization($job,$document);
153
        }
154
        return $document;
155
    }
156
157
    /**
158
     * Processing organization part
159
     *
160
     * @param Job                   $job
161
     * @param \SolrInputDocument    $document
162
     */
163
    public function processOrganization(Job $job,$document)
164
    {
165
        if(!is_null($job->getOrganization()->getImage())){
166
            $uri = $job->getOrganization()->getImage()->getUri();
167
            $document->addField('companyLogo',$uri);
168
        }
169
        $document->addField('organizationName',$job->getOrganization()->getOrganizationName()->getName());
170
        $document->addField('organizationId',$job->getOrganization()->getId());
171
    }
172
173
    /**
174
     * Processing location part
175
     * @param Job                $job
176
     * @param \SolrInputDocument $document
177
     */
178
    public function processLocation(Job $job,$document)
179
    {
180
        /* @var \Jobs\Entity\Location $location */
181
        foreach($job->getLocations() as $location){
182
            if(is_object($location->getCoordinates())){
183
                $coord = $location->getCoordinates()->getCoordinates();
184
                $document->addField('latLon',doubleval($coord[0]).','.doubleval($coord[1]));
185
            }
186
            $document->addField('postCode',$location->getPostalCode());
187
            $document->addField('regionText',$location->getRegion());
188
        }
189
190
        $document->addField('location',$job->getLocation());
191
    }
192
}