Completed
Pull Request — develop (#241)
by ANTHONIUS
07:56
created

JobEventSubscriber::processOrganization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 2
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
/**
21
 * Class JobEventSubscriber
22
 *
23
 * @package Solr\Event\Listener
24
 */
25
class JobEventSubscriber implements EventSubscriber
26
{
27
    /**
28
     * @var Manager
29
     */
30
    protected $solrManager;
31
32
    /**
33
     * JobEventSubscriber constructor.
34
     * @param Manager $manager
35
     */
36
    public function __construct(Manager $manager)
37
    {
38
        $this->solrManager = $manager;
39
    }
40
41
    /**
42
     * Define what event this subscriber listen to
43
     *
44
     * @return array
45
     */
46
    public function getSubscribedEvents()
47
    {
48
        return [
49
            Events::postUpdate,
50
            Events::postPersist,
51
        ];
52
    }
53
54
    /**
55
     * Handle doctrine post persist event
56
     *
57
     * @param LifecycleEventArgs $eventArgs
58
     */
59 View Code Duplication
    public function postPersist(LifecycleEventArgs $eventArgs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
60
    {
61
        $document = $eventArgs->getDocument();
62
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
     * Handle doctrine postUpdate event
77
     *
78
     * @param LifecycleEventArgs $eventArgs
79
     */
80 View Code Duplication
    public function postUpdate(LifecycleEventArgs $eventArgs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
81
    {
82
        $document = $eventArgs->getDocument();
83
        if (!$document instanceof Job) {
84
            return;
85
        }
86
87
        $solrDoc = $this->generateInputDocument($document,new \SolrInputDocument());
88
        try{
89
            $this->solrManager->addDocument($solrDoc,'/solr/YawikJobs');
90
        }catch (\Exception $e){
91
            // @TODO: What to do when the process failed?
92
        }
93
    }
94
95
    /**
96
     * @param ServiceLocatorInterface $serviceLocator
97
     * @return mixed
98
     */
99
    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...
100
    {
101
        $manager = $serviceLocator->get('Solr/Manager');
102
        return new static($manager);
0 ignored issues
show
Documentation introduced by
$manager is of type object|array, but the function expects a object<Solr\Bridge\Manager>.

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...
103
    }
104
105
    /**
106
     * Generate input document
107
     *
108
     * @param   Job                 $job
109
     * @param   \SolrInputDocument  $document
110
     * @return  \SolrInputDocument
111
     */
112
    public function generateInputDocument(Job $job, $document)
113
    {
114
        $document->addField('id',$job->getId());
115
        $document->addField('title',$job->getTitle());
116
        $document->addField('applicationEmail',$job->getContactEmail());
117
118
        if($job->getDateCreated()){
119
            $document->addField('dateCreated',
120
                $job->getDateCreated()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT)
121
            );
122
        }
123
        if($job->getDateModified()){
124
            $document->addField('dateModified',
125
                $job->getDateModified()->setTimezone(new \DateTimeZone('UTC'))->format(Manager::SOLR_DATE_FORMAT)
126
            );
127
        }
128
        if($job->getDatePublishStart()){
129
            $document->addField('datePublishStart',
130
                $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...
131
            );
132
        }
133
134
        if($job->getDatePublishEnd()){
135
            $document->addField('datePublishEnd',
136
                $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...
137
            );
138
        }
139
140
        $document->addField('isActive',$job->isActive());
141
        $document->addField('lang',$job->getLanguage());
142
143
        $this->processLocation($job,$document);
144
        if(!is_null($job->getOrganization())){
145
            $this->processOrganization($job,$document);
146
        }
147
        return $document;
148
    }
149
150
    /**
151
     * Processing organization part
152
     *
153
     * @param Job                   $job
154
     * @param \SolrInputDocument    $document
155
     */
156
    public function processOrganization(Job $job,$document)
157
    {
158
        if(!is_null($job->getOrganization()->getImage())){
159
            $uri = $job->getOrganization()->getImage()->getUri();
160
            $document->addField('companyLogo',$uri);
161
        }
162
        $document->addField('organizationName',$job->getOrganization()->getOrganizationName()->getName());
163
        $document->addField('organizationId',$job->getOrganization()->getId());
164
    }
165
166
    /**
167
     * Processing location part
168
     * @param Job                $job
169
     * @param \SolrInputDocument $document
170
     */
171
    public function processLocation(Job $job,$document)
172
    {
173
        /* @var \Jobs\Entity\Location $location */
174
        foreach($job->getLocations() as $location){
175
            $coord = $location->getCoordinates()->getCoordinates();
176
            $document->addField('latLon',doubleval($coord[0]).','.doubleval($coord[1]));
177
            $document->addField('postCode',$location->getPostalCode());
178
            $document->addField('regionText',$location->getRegion());
179
        }
180
181
        $document->addField('location',$job->getLocation());
182
    }
183
}