PutResource   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 6
c 3
b 0
f 1
lcom 1
cbo 7
dl 76
loc 76
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A get() 22 22 3
A getErrorResponse() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\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
use NilPortugues\Api\JsonApi\Server\Errors\NotFoundError;
20
21
/**
22
 * Class PutResource.
23
 */
24 View Code Duplication
class PutResource
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
25
{
26
    use ResponseTrait;
27
28
    /**
29
     * @var \NilPortugues\Api\JsonApi\Server\Errors\ErrorBag
30
     */
31
    protected $errorBag;
32
33
    /**
34
     * @var JsonApiSerializer
35
     */
36
    protected $serializer;
37
38
    /**
39
     * @param JsonApiSerializer $serializer
40
     */
41
    public function __construct(JsonApiSerializer $serializer)
42
    {
43
        $this->serializer = $serializer;
44
        $this->errorBag = new ErrorBag();
45
    }
46
47
    /**
48
     * @param          $id
49
     * @param array    $data
50
     * @param          $className
51
     * @param callable $findOneCallable
52
     * @param callable $update
53
     *
54
     * @return \Symfony\Component\HttpFoundation\Response
55
     */
56
    public function get($id, array $data, $className, callable $findOneCallable, callable $update)
57
    {
58
        try {
59
            DataObject::assertPut($data, $this->serializer, $className, $this->errorBag);
60
61
            $model = $findOneCallable();
62
63
            if (empty($model)) {
64
                $mapping = $this->serializer->getTransformer()->getMappingByClassName($className);
65
66
                return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)]));
67
            }
68
            $values = DataObject::getAttributes($data, $this->serializer);
69
            $update($model, $values, $this->errorBag);
70
71
            $response = $this->resourceUpdated($this->serializer->serialize($model));
72
        } catch (Exception $e) {
73
            $response = $this->getErrorResponse($e);
74
        }
75
76
        return $response;
77
    }
78
79
    /**
80
     * @param Exception $e
81
     *
82
     * @return \Symfony\Component\HttpFoundation\Response
83
     */
84
    protected function getErrorResponse(Exception $e)
85
    {
86
        switch (get_class($e)) {
87
            case DataException::class:
88
                $response = $this->unprocessableEntity($this->errorBag);
89
                break;
90
91
            default:
92
                $response = $this->errorResponse(
93
                    new ErrorBag([new Error('Bad Request', 'Request could not be served.')])
94
                );
95
        }
96
97
        return $response;
98
    }
99
}
100