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

HttpJsonSchemaRequest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 34
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getBody() 4 4 1
A check() 14 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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