CreateResource   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 5
c 4
b 0
f 1
lcom 1
cbo 5
dl 0
loc 68
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 15 2
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\JsonApiSerializer;
14
use NilPortugues\Api\JsonApi\Server\Actions\Traits\ResponseTrait;
15
use NilPortugues\Api\JsonApi\Server\Data\DataException;
16
use NilPortugues\Api\JsonApi\Server\Data\DataObject;
17
use NilPortugues\Api\JsonApi\Server\Errors\Error;
18
use NilPortugues\Api\JsonApi\Server\Errors\ErrorBag;
19
20
/**
21
 * Class CreateResource.
22
 */
23
class CreateResource
24
{
25
    use ResponseTrait;
26
27
    /**
28
     * @var \NilPortugues\Api\JsonApi\Server\Errors\ErrorBag
29
     */
30
    protected $errorBag;
31
32
    /**
33
     * @var JsonApiSerializer
34
     */
35
    protected $serializer;
36
37
    /**
38
     * @param JsonApiSerializer $serializer
39
     */
40
    public function __construct(JsonApiSerializer $serializer)
41
    {
42
        $this->serializer = $serializer;
43
        $this->errorBag = new ErrorBag();
44
    }
45
46
    /**
47
     * @param array    $data
48
     * @param          $className
49
     * @param callable $callable
50
     *
51
     * @return \Symfony\Component\HttpFoundation\Response
52
     */
53
    public function get(array $data, $className, callable $callable)
54
    {
55
        try {
56
            DataObject::assertPost($data, $this->serializer, $className, $this->errorBag);
57
58
            $values = DataObject::getAttributes($data, $this->serializer);
59
            $model = $callable($data, $values, $this->errorBag);
60
61
            $response = $this->resourceCreated($this->serializer->serialize($model));
62
        } catch (Exception $e) {
63
            $response = $this->getErrorResponse($e, $this->errorBag);
64
        }
65
66
        return $response;
67
    }
68
69
    /**
70
     * @param Exception $e
71
     * @param ErrorBag  $errorBag
72
     *
73
     * @return \Symfony\Component\HttpFoundation\Response
74
     */
75
    protected function getErrorResponse(Exception $e, ErrorBag $errorBag)
76
    {
77
        switch (get_class($e)) {
78
            case DataException::class:
79
                $response = $this->unprocessableEntity($errorBag);
80
                break;
81
82
            default:
83
                $response = $this->errorResponse(
84
                    new ErrorBag([new Error('Bad Request', 'Request could not be served.')])
85
                );
86
        }
87
88
        return $response;
89
    }
90
}
91