GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 36b55b...fa7cf7 )
by Florian
03:13
created

DoctrineListener   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
lcom 2
cbo 5
dl 0
loc 126
rs 10
c 1
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 8 1
A __construct() 0 5 1
A enableIndexing() 0 3 1
A disableIndexing() 0 3 1
A isIndexingEnabled() 0 3 1
A postPersist() 0 4 2
A postFlush() 0 6 2
A preRemove() 0 4 2
A postUpdate() 0 4 2
A getEntityToDocumentMapper() 0 3 1
A getSearchService() 0 4 1
A updateEntity() 0 6 2
A removeEntity() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the Stinger Entity Search package.
5
 *
6
 * (c) Oliver Kotte <[email protected]>
7
 * (c) Florian Meyer <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
namespace StingerSoft\EntitySearchBundle\Services;
13
14
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Doctrine\Common\EventSubscriber;
17
use StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface;
18
use Doctrine\ORM\Event\PostFlushEventArgs;
19
20
class DoctrineListener implements EventSubscriber {
21
22
	/**
23
	 *
24
	 * @var EntityToDocumentMapperInterface
25
	 */
26
	protected $entityToDocumentMapper;
27
28
	/**
29
	 *
30
	 * @var SearchService
31
	 */
32
	protected $searchService;
33
34
	protected $needsFlush = false;
35
	
36
	protected $enableIndexing;
37
38
	public function getSubscribedEvents() {
39
		return array(
40
			'postPersist',
41
			'postUpdate',
42
			'preRemove',
43
			'postFlush' 
44
		);
45
	}
46
47
	/**
48
	 * Constructor
49
	 *
50
	 * @param SearchService $searchService        	
51
	 */
52
	public function __construct(EntityToDocumentMapperInterface $entityToDocumentMapper, SearchService $searchService, $enableIndexing = true) {
53
		$this->entityToDocumentMapper = $entityToDocumentMapper;
54
		$this->searchService = $searchService;
55
		$this->enableIndexing = $enableIndexing;
56
	}
57
	
58
	public function enableIndexing() {
59
		$this->enableIndexing = true;
60
	}
61
	
62
	public function disableIndexing() {
63
		$this->enableIndexing = false;
64
	}
65
	
66
	public function isIndexingEnabled() {
67
		return $this->enableIndexing;
68
	}
69
70
	/**
71
	 * Index the entity after it is persisted for the first time
72
	 *
73
	 * @param LifecycleEventArgs $args        	
74
	 */
75
	public function postPersist(LifecycleEventArgs $args) {
76
		if(!$this->enableIndexing) return;
77
		$this->updateEntity($args->getObject(), $args->getObjectManager());
78
	}
79
80
	public function postFlush(PostFlushEventArgs $eventArgs) {
81
		if($this->needsFlush) {
82
			$this->needsFlush = false;
83
			$eventArgs->getEntityManager()->flush();
84
		}
85
	}
86
87
	/**
88
	 * Removes the entity from the index when it marked for deletion
89
	 *
90
	 * @param LifecycleEventArgs $args        	
91
	 */
92
	public function preRemove(LifecycleEventArgs $args) {
93
		if(!$this->enableIndexing) return;
94
		$this->removeEntity($args->getObject(), $args->getObjectManager());
95
	}
96
97
	/**
98
	 * Updates the entity in the index after it is updated
99
	 *
100
	 * @param LifecycleEventArgs $args        	
101
	 */
102
	public function postUpdate(LifecycleEventArgs $args) {
103
		if(!$this->enableIndexing) return;
104
		$this->updateEntity($args->getObject(), $args->getObjectManager());
105
	}
106
107
	/**
108
	 *
109
	 * @return EntityToDocumentMapperInterface
110
	 */
111
	protected function getEntityToDocumentMapper() {
112
		return $this->entityToDocumentMapper;
113
	}
114
115
	/**
116
	 *
117
	 * @return SearchService
118
	 */
119
	protected function getSearchService(ObjectManager $manager) {
120
		$this->searchService->setObjectManager($manager);
121
		return $this->searchService;
122
	}
123
124
	/**
125
	 *
126
	 * @param object $entity        	
127
	 */
128
	protected function updateEntity($entity, ObjectManager $manager) {
129
		$document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity);
130
		if($document !== false) {
131
			$this->getSearchService($manager)->saveDocument($document);
0 ignored issues
show
Bug introduced by
It seems like $document defined by $this->getEntityToDocume...ment($manager, $entity) on line 129 can also be of type boolean; however, StingerSoft\EntitySearch...Service::saveDocument() does only seem to accept object<StingerSoft\Entit...hBundle\Model\Document>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
132
		}
133
	}
134
135
	/**
136
	 *
137
	 * @param object $entity        	
138
	 */
139
	protected function removeEntity($entity, ObjectManager $manager) {
140
		$document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity);
141
		if($document !== false) {
142
			$this->getSearchService($manager)->removeDocument($document);
0 ignored issues
show
Bug introduced by
It seems like $document defined by $this->getEntityToDocume...ment($manager, $entity) on line 140 can also be of type boolean; however, StingerSoft\EntitySearch...rvice::removeDocument() does only seem to accept object<StingerSoft\Entit...hBundle\Model\Document>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
143
		}
144
	}
145
}