Completed
Push — development ( 1cb416...259b9d )
by Alexander
03:03
created

HttpJsonSchemaRequest::getBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Processing HTTP-request with JSON body
4
 *
5
 * This class intended for use with JSON-Schema validator adapter
6
 *
7
 * @file      HttpJsonSchemaRequest.php
8
 *
9
 * PHP version 5.6+
10
 *
11
 * @author    Yancharuk Alexander <alex at itvault dot info>
12
 * @copyright © 2012-2017 Alexander Yancharuk <alex at itvault at info>
13
 * @date      2017-01-18 10:20
14
 * @license   The BSD 3-Clause License
15
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
16
 */
17
18
namespace Veles\Request;
19
20
use Veles\Exceptions\Http\UnprocessableException;
21
22
/**
23
 * Class   HttpJsonSchemaRequest
24
 *
25
 * @author Yancharuk Alexander <alex at itvault at info>
26
 */
27 View Code Duplication
class HttpJsonSchemaRequest extends HttpRequestAbstract
1 ignored issue
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
{
29
	/**
30
	 * Getting http-request body
31
	 *
32
	 * @return array
33
	 */
34
	public function getBody()
35
	{
36
		return json_decode(file_get_contents($this->stream));
37
	}
38
39
	/**
40
	 * Check input according definitions
41
	 *
42
	 * @param mixed $definitions
43
	 *
44
	 * @throws UnprocessableException
45
	 */
46
	public function check($definitions)
47
	{
48
		$raw_data  = $this->getBody();
49
		$schema    = json_decode($definitions);
50
		$validator = $this->getValidator();
51
52
		$validator->check($raw_data, $schema);
53
54
		if (!$validator->isValid()) {
55
			throw new UnprocessableException($validator->getErrors());
56
		}
57
58
		$this->setData((array) $raw_data);
59
	}
60
}
61