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 ( 2f2f35...e5e78d )
by Florian
07:45
created

EntityToDocumentMapper::isClassIndexable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
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\Mapping;
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
use StingerSoft\EntitySearchBundle\Services\SearchService;
19
20
/**
21
 * Handles the creation of documents out of entities
22
 */
23
class EntityToDocumentMapper implements EntityToDocumentMapperInterface {
24
25
	/**
26
	 *
27
	 * @var SearchService
28
	 */
29
	protected $searchService;
30
31
	/**
32
	 *
33
	 * @var string[string]
34
	 */
35
	protected $mapping = array();
36
37
	/**
38
	 *
39
	 * @var string[string]
40
	 */
41
	protected $cachedMapping = array();
42
43
	/**
44
	 * Constructor
45
	 *
46
	 * @param SearchService $searchService        	
47
	 */
48
	public function __construct(SearchService $searchService, array $mapping = array()) {
49
		$this->searchService = $searchService;
50
		foreach($mapping as $key => $config) {
51
			if(!isset($config['mappings'])) {
52
				throw new \InvalidArgumentException($key . ' has no mapping defined!');
53
			}
54
			if(!isset($config['persistence'])) {
55
				throw new \InvalidArgumentException($key . ' has no persistence defined!');
56
			}
57
			if(!isset($config['persistence']['model'])) {
58
				throw new \InvalidArgumentException($key . ' has no model defined!');
59
			}
60
			$map = array();
61
			foreach($config['mappings'] as $fieldKey => $fieldConfig) {
62
				$map[$fieldKey] = isset($fieldConfig['propertyPath']) && $fieldConfig['propertyPath'] ? $fieldConfig['propertyPath'] : $fieldKey;
63
			}
64
			
65
			$this->mapping[$config['persistence']['model']] = $map;
66
		}
67
	}
68
69
	/**
70
	 *
71
	 * {@inheritDoc}
72
	 *
73
	 * @see \StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface::isIndexable()
74
	 */
75
	public function isIndexable($object) {
76
		if($object instanceof SearchableEntity) {
77
			return true;
78
		}
79
		if(count($this->getMapping(get_class($object))) > 0) {
80
			return true;
81
		}
82
		return false;
83
	}
84
	
85
	/**
86
	 *
87
	 * {@inheritDoc}
88
	 *
89
	 * @see \StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface::isClassIndexable()
90
	 */
91
	public function isClassIndexable($clazz) {
92
		$reflectionClass = new \ReflectionClass($clazz);
93
		if(array_key_exists(SearchableEntity::class, $reflectionClass->getInterfaces())) {
94
			return true;
95
		}
96
		if(count($this->getMapping($clazz)) > 0) {
97
			return true;
98
		}
99
		return false;
100
	}
101
102
103
	/**
104
	 * {@inheritDoc}
105
	 * @see \StingerSoft\EntitySearchBundle\Services\Mapping\EntityToDocumentMapperInterface::createDocument()
106
	 */
107
	public function createDocument(ObjectManager $manager, $object) {
108
		if(!$this->isIndexable($object))
109
			return false;
110
		$document = $this->getSearchService($manager)->createEmptyDocumentFromEntity($object);
111
		$index = $this->fillDocument($document, $object);
112
		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...
113
			return false;
114
		
115
		return $document;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $document; (StingerSoft\EntitySearchBundle\Model\Document) is incompatible with the return type declared by the interface StingerSoft\EntitySearch...terface::createDocument of type boolean|StingerSoft\Enti...rvices\Mapping\Document.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
116
	}
117
118
	/**
119
	 * Fills the given document based on the object
120
	 *
121
	 * @param Document $document        	
122
	 * @param object $object        	
123
	 * @return boolean
124
	 */
125
	protected function fillDocument(Document &$document, $object) {
126
		if($object instanceof SearchableEntity) {
127
			return $object->indexEntity($document);
128
		}
129
		$mapping = $this->getMapping(get_class($object));
130
		$accessor = PropertyAccess::createPropertyAccessor();
131
		foreach($mapping as $fieldName => $propertyPath) {
132
			$document->addField($fieldName, $accessor->getValue($object, $propertyPath));
133
		}
134
		return true;
135
	}
136
137
	/**
138
	 * Fetches the mapping for the given object including the mapping of superclasses
139
	 *
140
	 * @param object $object        	
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
141
	 * @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...
142
	 */
143
	protected function getMapping($clazz) {
144
		if(isset($this->cachedMapping[$clazz])) {
145
			return $this->cachedMapping[$clazz];
146
		}
147
		$ref = new \ReflectionClass($clazz);
148
		
149
		$mapping = array();
150
		
151
		foreach($this->mapping as $className => $config) {
152
			if($clazz == $className || $ref->isSubclassOf($className)) {
153
				$mapping = array_merge($mapping, $config);
154
			}
155
		}
156
		
157
		$this->cachedMapping[$clazz] = $mapping;
158
		
159
		return $mapping;
160
	}
161
162
	/**
163
	 * Returns the search service
164
	 *
165
	 * @return SearchService
166
	 */
167
	protected function getSearchService(ObjectManager $manager) {
168
		$this->searchService->setObjectManager($manager);
169
		return $this->searchService;
170
	}
171
}