Completed
Pull Request — develop (#241)
by ANTHONIUS
08:03
created

JobEventSubscriber::processLocation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
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
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)
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...
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)
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...
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)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
80
    {
81
        $manager = $serviceLocator->get('Solr/Manager');
82
        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...
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)
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...
108
            );
109
        }
110
111
        if($job->getDatePublishEnd()){
112
            $document->addField('datePublishEnd',
113
                $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...
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());
0 ignored issues
show
Unused Code Comprehensibility introduced by
83% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
}