DeleteResource   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 17 3
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 12/2/15
5
 * Time: 9:38 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\Errors\Error;
16
use NilPortugues\Api\JsonApi\Server\Errors\ErrorBag;
17
use NilPortugues\Api\JsonApi\Server\Errors\NotFoundError;
18
19
/**
20
 * Class DeleteResource.
21
 */
22
class DeleteResource
23
{
24
    use ResponseTrait;
25
26
    /**
27
     * @var \NilPortugues\Api\JsonApi\Server\Errors\ErrorBag
28
     */
29
    protected $errorBag;
30
31
    /**
32
     * @var JsonApiSerializer
33
     */
34
    protected $serializer;
35
36
    /**
37
     * @param JsonApiSerializer $serializer
38
     */
39
    public function __construct(JsonApiSerializer $serializer)
40
    {
41
        $this->serializer = $serializer;
42
        $this->errorBag = new ErrorBag();
43
    }
44
45
    /**
46
     * @param          $id
47
     * @param          $className
48
     * @param callable $findOneCallable
49
     * @param callable $deleteCallable
50
     *
51
     * @return \Symfony\Component\HttpFoundation\Response
52
     */
53
    public function get($id, $className, callable $findOneCallable, callable $deleteCallable)
54
    {
55
        try {
56
            $data = $findOneCallable();
57
            if (empty($data)) {
58
                $mapping = $this->serializer->getTransformer()->getMappingByClassName($className);
59
60
                return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)]));
61
            }
62
63
            $deleteCallable();
64
65
            return $this->resourceDeleted();
66
        } catch (Exception $e) {
67
            return $this->errorResponse(new ErrorBag([new Error('Bad Request', 'Request could not be served.')]));
68
        }
69
    }
70
}
71