Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

AjaxResponseHandler::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 4
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 13
cp 0
crap 6
1
<?php
2
3
namespace Elgg\Likes;
4
5
use Elgg\Services\AjaxResponse;
6
7
/**
8
 * Ajax response handler
9
 */
10
class AjaxResponseHandler {
11
12
	/**
13
	 * Alter ajax response to send back likes count
14
	 *
15
	 * @param string                      $hook     Hook name (AjaxResponse::RESPONSE_HOOK)
16
	 * @param string                      $type     'all'
17
	 * @param \Elgg\Services\AjaxResponse $response Ajax response
18
	 * @param array                       $params   Hook params
19
	 *
20
	 * @return void|\Elgg\Services\AjaxResponse
21
	 */
22
	public function __invoke($hook, $type, AjaxResponse $response, $params) {
23
		$entity = get_entity(get_input('guid'));
0 ignored issues
show
Bug introduced by
It seems like get_input('guid') can also be of type string; however, parameter $guid of get_entity() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
		$entity = get_entity(/** @scrutinizer ignore-type */ get_input('guid'));
Loading history...
24
		if (!$entity) {
25
			return;
26
		}
27
		
28
		$response->getData()->likes_status = [
29
			'guid' => $entity->guid,
30
			'count' => likes_count($entity),
31
			'count_menu_item' => elgg_view_menu_item(_likes_count_menu_item($entity)),
32
			'like_menu_item' => elgg_view_menu_item(_likes_menu_item($entity)),
33
			'is_liked' => DataService::instance()->currentUserLikesEntity($entity->guid),
34
		];
35
		
36
		return $response;
37
	}
38
}
39