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

JobEventSubscriber   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 159
Duplicated Lines 18.24 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 1
cbo 7
dl 29
loc 159
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 7 1
A postPersist() 15 15 3
A postUpdate() 14 14 3
A factory() 0 5 1
B generateInputDocument() 0 37 6
A processOrganization() 0 9 2
A processLocation() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
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...
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)
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...
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)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
102
    {
103
        $manager = $serviceLocator->get('Solr/Manager');
104
        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...
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)
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...
133
            );
134
        }
135
136
        if($job->getDatePublishEnd()){
137
            $document->addField('datePublishEnd',
138
                $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...
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
}