1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Request; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
7
|
|
|
|
8
|
|
|
class BatchParser implements ParserInterface |
9
|
|
|
{ |
10
|
|
|
use UploadParserTrait; |
11
|
|
|
|
12
|
|
|
const PARAM_ID = 'id'; |
13
|
|
|
|
14
|
|
|
private static $queriesDefaultValue = [ |
15
|
|
|
self::PARAM_ID => null, |
16
|
|
|
self::PARAM_QUERY => null, |
17
|
|
|
self::PARAM_VARIABLES => null, |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Request $request |
22
|
|
|
* |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
7 |
|
public function parse(Request $request) |
26
|
|
|
{ |
27
|
|
|
// Extracts the GraphQL request parameters |
28
|
7 |
|
$queries = $this->getParsedBody($request); |
29
|
|
|
|
30
|
5 |
|
if (empty($queries)) { |
31
|
1 |
|
throw new BadRequestHttpException('Must provide at least one valid query.'); |
32
|
|
|
} |
33
|
|
|
|
34
|
4 |
|
foreach ($queries as $i => &$query) { |
35
|
4 |
|
$query = array_filter($query) + self::$queriesDefaultValue; |
36
|
|
|
|
37
|
4 |
|
if (!is_string($query[static::PARAM_QUERY])) { |
38
|
4 |
|
throw new BadRequestHttpException(sprintf('%s is not a valid query', json_encode($query[static::PARAM_QUERY]))); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
|
return $queries; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Gets the body from the request. |
47
|
|
|
* |
48
|
|
|
* @param Request $request |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
7 |
|
private function getParsedBody(Request $request) |
53
|
|
|
{ |
54
|
7 |
|
$contentType = explode(';', $request->headers->get('content-type'))[0]; |
55
|
|
|
|
56
|
|
|
// JSON object |
57
|
|
|
switch ($contentType) { |
58
|
7 |
View Code Duplication |
case static::CONTENT_TYPE_JSON: |
|
|
|
|
59
|
5 |
|
$parsedBody = json_decode($request->getContent(), true); |
60
|
|
|
|
61
|
5 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
62
|
1 |
|
throw new BadRequestHttpException('POST body sent invalid JSON'); |
63
|
|
|
} |
64
|
4 |
|
break; |
65
|
|
|
|
66
|
2 |
|
case static::CONTENT_TYPE_FORM_DATA: |
67
|
1 |
|
$parsedBody = $this->treatUploadFiles($request->request->all(), $request->files->all()); |
68
|
1 |
|
break; |
69
|
|
|
|
70
|
|
|
default: |
71
|
1 |
|
throw new BadRequestHttpException(sprintf( |
72
|
1 |
|
'Batching parser only accepts "%s" or "%s" content-type but got %s.', |
73
|
1 |
|
static::CONTENT_TYPE_JSON, |
74
|
1 |
|
static::CONTENT_TYPE_FORM_DATA, |
75
|
1 |
|
json_encode($contentType) |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
return $parsedBody; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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.