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 ( 1a5f24...36b55b )
by Florian
03:10
created

DoctrineListener::getEntityToDocumentMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
	public function getSubscribedEvents() {
37
		return array(
38
			'postPersist',
39
			'postUpdate',
40
			'preRemove',
41
			'postFlush' 
42
		);
43
	}
44
45
	/**
46
	 * Constructor
47
	 *
48
	 * @param SearchService $searchService        	
49
	 */
50
	public function __construct(EntityToDocumentMapperInterface $entityToDocumentMapper, SearchService $searchService) {
51
		$this->entityToDocumentMapper = $entityToDocumentMapper;
52
		$this->searchService = $searchService;
53
	}
54
55
	/**
56
	 * Index the entity after it is persisted for the first time
57
	 *
58
	 * @param LifecycleEventArgs $args        	
59
	 */
60
	public function postPersist(LifecycleEventArgs $args) {
61
		$this->updateEntity($args->getObject(), $args->getObjectManager());
62
	}
63
64
	public function postFlush(PostFlushEventArgs $eventArgs) {
65
		if($this->needsFlush) {
66
			$this->needsFlush = false;
67
			$eventArgs->getEntityManager()->flush();
68
		}
69
	}
70
71
	/**
72
	 * Removes the entity from the index when it marked for deletion
73
	 *
74
	 * @param LifecycleEventArgs $args        	
75
	 */
76
	public function preRemove(LifecycleEventArgs $args) {
77
		$this->removeEntity($args->getObject(), $args->getObjectManager());
78
	}
79
80
	/**
81
	 * Updates the entity in the index after it is updated
82
	 *
83
	 * @param LifecycleEventArgs $args        	
84
	 */
85
	public function postUpdate(LifecycleEventArgs $args) {
86
		$this->updateEntity($args->getObject(), $args->getObjectManager());
87
	}
88
89
	/**
90
	 *
91
	 * @return EntityToDocumentMapperInterface
92
	 */
93
	protected function getEntityToDocumentMapper() {
94
		return $this->entityToDocumentMapper;
95
	}
96
97
	/**
98
	 *
99
	 * @return SearchService
100
	 */
101
	protected function getSearchService(ObjectManager $manager) {
102
		$this->searchService->setObjectManager($manager);
103
		return $this->searchService;
104
	}
105
106
	/**
107
	 *
108
	 * @param object $entity        	
109
	 */
110 View Code Duplication
	protected function updateEntity($entity, ObjectManager $manager) {
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...
111
		$document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity);
112
		if($document !== false) {
113
			$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 111 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...
114
			$this->needsFlush = true;
115
		}
116
	}
117
118
	/**
119
	 *
120
	 * @param object $entity        	
121
	 */
122 View Code Duplication
	protected function removeEntity($entity, ObjectManager $manager) {
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...
123
		$document = $this->getEntityToDocumentMapper()->createDocument($manager, $entity);
124
		if($document !== false) {
125
			$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 123 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...
126
			$this->needsFlush = true;
127
		}
128
	}
129
}