Completed
Push — master ( 9cc449...9c9e31 )
by Matt
02:49 queued 02:34
created

increment_controller::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 2
crap 3
1
<?php
2
/**
3
 *
4
 * Advertisement management. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2017 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ads\controller;
12
13
/**
14
* Increment controller
15
*/
16
class increment_controller
17
{
18
	/** @var \phpbb\ads\ad\manager */
19
	protected $manager;
20
21
	/** @var \phpbb\request\request */
22
	protected $request;
23
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param \phpbb\ads\ad\manager    $manager Advertisement manager object
28
	 * @param \phpbb\request\request   $request	Request object
29
	 */
30 7
	public function __construct(\phpbb\ads\ad\manager $manager, \phpbb\request\request $request)
31
	{
32 7
		$this->manager = $manager;
33 7
		$this->request = $request;
34 7
	}
35
36
	/**
37
	 * Handle request.
38
	 *
39
	 * @param	mixed	$data	Ad ID or ad IDs
40
	 * @param	string	$mode	clicks or views
41
	 * @return	\Symfony\Component\HttpFoundation\JsonResponse	A Symfony JsonResponse object
42
	 * @throws	\phpbb\exception\http_exception
43
	 */
44 7
	public function handle($data, $mode)
45
	{
46 7
		if (!empty($data) && $this->request->is_ajax())
47 7
		{
48 3
			$this->{$mode}($data);
49
50 3
			return new \Symfony\Component\HttpFoundation\JsonResponse();
51
		}
52
53 4
		throw new \phpbb\exception\http_exception(403, 'NOT_AUTHORISED');
54
	}
55
56
	/**
57
	 * Increment clicks for an ad.
58
	 *
59
	 * @param	int	$ad_id	Advertisement ID
60
	 */
61 1
	protected function clicks($ad_id)
62
	{
63 1
		$this->manager->increment_ad_clicks($ad_id);
64 1
	}
65
66
	/**
67
	 * Increment views for ads.
68
	 *
69
	 * @param	string	$ad_ids	Advertisement IDs
70
	 */
71 2
	protected function views($ad_ids)
72
	{
73 2
		$this->manager->increment_ads_views(explode('-', $ad_ids));
74 2
	}
75
}
76