Passed
Push — develop ( 5f0710...340c0b )
by Neill
12:23 queued 14s
created

JsonResponseFormatter::formatJson()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 1
ccs 0
cts 9
cp 0
crap 20
1
<?php
2
/**
3
 * @link http://www.newicon.net/neon
4
 * @copyright Copyright (c) 2017 Newicon Ltd
5
 * @license http://www.newicon.net/neon/license/
6
 * @author Steve O'Brien <[email protected]> 29/01/2017 23:05
7
 * @package neon
8
 */
9
10
namespace neon\core\web;
11
12
13
class JsonResponseFormatter extends \yii\web\JsonResponseFormatter
14
{
15
	protected function formatJson($response)
16
	{
17
		// If we have requested an endpoint that returns json with a preferred format
18
		// of text/html instead of application/json then we are probably browsing the API
19
		// and therefore want a prettier version of the response.
20
		$acceptableContentTypes = array_keys(neon()->request->getAcceptableContentTypes());
21
		$contentTypeIsHtml =  (isset($acceptableContentTypes[0]) && $acceptableContentTypes[0] === 'text/html');
22
		$prettyGetVarExists = (neon()->request->get('_pretty', false) !== false);
23
		if ($contentTypeIsHtml || $prettyGetVarExists) {
24
			$this->prettyPrint = true;
25
		}
26
		parent::formatJson($response);
27
	}
28
}