GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CreateResourceController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 40
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 19 4
A validate() 0 8 2
1
<?php
2
3
namespace JDesrosiers\Resourceful\Controller;
4
5
use Doctrine\Common\Cache\Cache;
6
use Silex\Application;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
11
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
12
13
class CreateResourceController
14
{
15
    private $service;
16
    private $schema;
17
18 9
    public function __construct(Cache $service, $schema)
19
    {
20 9
        $this->service = $service;
21 9
        $this->schema = $schema;
22 9
    }
23
24 5
    public function __invoke(Application $app, Request $request)
25
    {
26 5
        $requestJson = $request->getContent() ?: "{}";
27 5
        $data = json_decode($requestJson);
28 5
        if (json_last_error() !== JSON_ERROR_NONE) {
29 1
            throw new BadRequestHttpException("Invalid JSON: " . json_last_error_msg());
30
        }
31
32 4
        $data->id = $app["uniqid"];
33
34 4
        $this->validate($app, $data);
35
36 3
        $location = $app["url_generator"]->generate($this->schema, ["id" => $data->id]);
37 3
        if ($this->service->save($location, $data) === false) {
38 1
            throw new ServiceUnavailableHttpException(null, "Failed to save resource");
39
        }
40
41 2
        return JsonResponse::create($data, Response::HTTP_CREATED, ["Location" => $location]);
42
    }
43
44 4
    private function validate(Application $app, $data)
45
    {
46 4
        $schema = $app["json-schema.schema-store"]->get($this->schema);
47 4
        $validation = $app["json-schema.validator"]->validate($data, $schema);
48 4
        if (!$validation->valid) {
49 1
            throw new BadRequestHttpException(json_encode($validation->errors));
50
        }
51 3
    }
52
}
53