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 ( 926faa...eb8df6 )
by Florian
04:05
created

EntityHandler::fillDocument()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Stinger Enity 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 StingerSoft\EntitySearchBundle\Model\SearchableEntity;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use StingerSoft\EntitySearchBundle\Model\Document;
17
use Symfony\Component\PropertyAccess\PropertyAccess;
18
19
/**
20
 * Handles the creation of documents out of entities
21
 */
22
class EntityHandler {
23
24
	/**
25
	 *
26
	 * @var SearchService
27
	 */
28
	protected $searchService;
29
30
	/**
31
	 *
32
	 * @var string[string]
33
	 */
34
	protected $mapping = array();
35
36
	/**
37
	 *
38
	 * @var string[string]
39
	 */
40
	protected $cachedMapping = array();
41
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param SearchService $searchService        	
46
	 */
47
	public function __construct(SearchService $searchService, array $mapping = array()) {
48
		$this->searchService = $searchService;
49
		foreach($mapping as $key => $config) {
50
			if(!isset($config['mappings'])) {
51
				throw new \InvalidArgumentException($key . ' has no mapping defined!');
52
			}
53
			if(!isset($config['persistence'])) {
54
				throw new \InvalidArgumentException($key . ' has no persistence defined!');
55
			}
56
			if(!isset($config['persistence']['model'])) {
57
				throw new \InvalidArgumentException($key . ' has no model defined!');
58
			}
59
			$map = array();
60
			foreach($config['mappings'] as $fieldKey => $fieldConfig) {
61
				$map[$fieldKey] = isset($fieldConfig['propertyPath']) && $fieldConfig['propertyPath'] ? $fieldConfig['propertyPath'] : $fieldKey;
62
			}
63
			
64
			$this->mapping[$config['persistence']['model']] = $map;
65
		}
66
	}
67
68
	/**
69
	 * Checks if the given object can be added to the index
70
	 *
71
	 * @param object $object        	
72
	 * @return boolean
73
	 */
74
	public function isIndexable($object) {
75
		if($object instanceof SearchableEntity) {
76
			return true;
77
		}
78
		if(count($this->getMapping($object)) > 0) {
79
			return true;
80
		}
81
		return false;
82
	}
83
84
	/**
85
	 * Tries to create a document from the given object
86
	 *
87
	 * @param ObjectManager $manager        	
88
	 * @param object $object        	
89
	 * @return boolean|Document Returns false if no document could be created
90
	 */
91
	public function createDocument(ObjectManager $manager, $object) {
92
		if(!$this->isIndexable($object))
93
			return false;
94
		$document = $this->getSearchService($manager)->createEmptyDocumentFromEntity($object);
95
		$index = $this->fillDocument($document, $object);
96
		if($index == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
97
			return false;
98
		
99
		return $document;
100
	}
101
102
	/**
103
	 * Fills the given document based on the object
104
	 *
105
	 * @param Document $document        	
106
	 * @param object $object        	
107
	 * @return boolean
108
	 */
109
	protected function fillDocument(Document &$document, $object) {
110
		if($object instanceof SearchableEntity) {
111
			return $object->indexEntity($document);
112
		}
113
		$mapping = $this->getMapping($object);
114
		$accessor = PropertyAccess::createPropertyAccessor();
115
		foreach($mapping as $fieldName => $propertyPath) {
116
			$document->addField($fieldName, $accessor->getValue($object, $propertyPath));
117
		}
118
		return true;
119
	}
120
121
	/**
122
	 * Fetches the mapping for the given object including the mapping of superclasses
123
	 *
124
	 * @param object $object        	
125
	 * @return \StingerSoft\EntitySearchBundle\Services\string[string]
0 ignored issues
show
Documentation introduced by
The doc-type \StingerSoft\EntitySearc...Services\string[string] could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
126
	 */
127
	protected function getMapping($object) {
128
		$clazz = get_class($object);
129
		if(isset($this->cachedMapping[$clazz])) {
130
			return $this->cachedMapping[$clazz];
131
		}
132
		$ref = new \ReflectionClass($clazz);
133
		
134
		$mapping = array();
135
		
136
		foreach($this->mapping as $className => $config) {
137
			if($clazz == $className || $ref->isSubclassOf($className)) {
138
				$mapping = array_merge($mapping, $config);
139
			}
140
		}
141
		
142
		$this->cachedMapping[$clazz] = $mapping;
143
		
144
		return $mapping;
145
	}
146
147
	/**
148
	 * Returns the search service
149
	 * 
150
	 * @return SearchService
151
	 */
152
	protected function getSearchService(ObjectManager $manager) {
153
		$this->searchService->setObjectManager($manager);
154
		return $this->searchService;
155
	}
156
}