GetResource   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 6
c 4
b 0
f 1
lcom 1
cbo 9
dl 0
loc 89
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A get() 0 19 3
A getErrorResponse() 0 15 2
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 12/2/15
5
 * Time: 9:37 PM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace NilPortugues\Api\JsonApi\Server\Actions;
11
12
use Exception;
13
use NilPortugues\Api\JsonApi\Http\Request\Parameters\Fields;
14
use NilPortugues\Api\JsonApi\Http\Request\Parameters\Included;
15
use NilPortugues\Api\JsonApi\Http\Request\Parameters\Sorting;
16
use NilPortugues\Api\JsonApi\JsonApiSerializer;
17
use NilPortugues\Api\JsonApi\Server\Actions\Traits\RequestTrait;
18
use NilPortugues\Api\JsonApi\Server\Actions\Traits\ResponseTrait;
19
use NilPortugues\Api\JsonApi\Server\Errors\Error;
20
use NilPortugues\Api\JsonApi\Server\Errors\ErrorBag;
21
use NilPortugues\Api\JsonApi\Server\Errors\NotFoundError;
22
use NilPortugues\Api\JsonApi\Server\Query\QueryException;
23
use NilPortugues\Api\JsonApi\Server\Query\QueryObject;
24
25
/**
26
 * Class GetResource.
27
 */
28
class GetResource
29
{
30
    use RequestTrait;
31
    use ResponseTrait;
32
33
    /**
34
     * @var \NilPortugues\Api\JsonApi\Server\Errors\ErrorBag
35
     */
36
    protected $errorBag;
37
38
    /**
39
     * @var JsonApiSerializer
40
     */
41
    protected $serializer;
42
43
    /**
44
     * @var Fields
45
     */
46
    protected $fields;
47
48
    /**
49
     * @var Included
50
     */
51
    protected $included;
52
53
    /**
54
     * @param JsonApiSerializer $serializer
55
     * @param Fields            $fields
56
     * @param Included          $included
57
     */
58
    public function __construct(
59
        JsonApiSerializer $serializer,
60
        Fields $fields,
61
        Included $included
62
    ) {
63
        $this->serializer = $serializer;
64
        $this->errorBag = new ErrorBag();
65
        $this->fields = $fields;
66
        $this->included = $included;
67
    }
68
69
    /**
70
     * @param string|int $id
71
     * @param string     $className
72
     * @param callable   $callable
73
     *
74
     * @return \Symfony\Component\HttpFoundation\Response
75
     */
76
    public function get($id, $className, callable $callable)
77
    {
78
        try {
79
            QueryObject::assert($this->serializer, $this->fields, $this->included, new Sorting(), $this->errorBag, $className);
80
            $data = $callable();
81
82
            if (empty($data)) {
83
                $mapping = $this->serializer->getTransformer()->getMappingByClassName($className);
84
85
                return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)]));
86
            }
87
88
            $response = $this->response($this->serializer->serialize($data, $this->fields, $this->included));
89
        } catch (Exception $e) {
90
            $response = $this->getErrorResponse($e);
91
        }
92
93
        return $response;
94
    }
95
96
    /**
97
     * @param Exception $e
98
     *
99
     * @return \Symfony\Component\HttpFoundation\Response
100
     */
101
    protected function getErrorResponse(Exception $e)
102
    {
103
        switch (get_class($e)) {
104
            case QueryException::class:
105
                $response = $this->errorResponse($this->errorBag);
106
                break;
107
108
            default:
109
                $response = $this->errorResponse(
110
                    new ErrorBag([new Error('Bad Request', 'Request could not be served.')])
111
                );
112
        }
113
114
        return $response;
115
    }
116
}
117