Test Failed
Push — master ( b600aa...6380c9 )
by Pavel
02:04
created

FractalResponse::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest\Response;
2
3
use League\Fractal\Manager;
4
use League\Fractal\Resource\Collection;
5
use League\Fractal\Resource\Item;
6
use League\Fractal\TransformerAbstract;
7
8
use Pz\Doctrine\Rest\Action\Index\ResponseDataInterface;
9
use Pz\Doctrine\Rest\Request\CreateRequestInterface;
10
use Pz\Doctrine\Rest\Request\DeleteRequestInterface;
11
use Pz\Doctrine\Rest\Request\IndexRequestInterface;
12
use Pz\Doctrine\Rest\Request\ShowRequestInterface;
13
use Pz\Doctrine\Rest\Request\UpdateRequestInterface;
14
use Pz\Doctrine\Rest\RestRequestInterface;
15
use Pz\Doctrine\Rest\RestResponseFactory;
16
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\Response;
19
20
class FractalResponse implements RestResponseFactory
21
{
22
    /**
23
     * @var TransformerAbstract
24
     */
25
    protected $transformer;
26
27
    /**
28
     * @var Manager|null
29
     */
30
    protected $fractal;
31
32
    /**
33
     * FractalResponse constructor.
34
     *
35
     * @param TransformerAbstract $transformer
36
     */
37 8
    public function __construct(TransformerAbstract $transformer)
38
    {
39 8
        $this->transformer = $transformer;
40 8
    }
41
42
    /**
43
     * @param IndexRequestInterface $request
44
     * @param ResponseDataInterface $response
45
     *
46
     * @return array
47
     */
48 1
    public function index(IndexRequestInterface $request, ResponseDataInterface $response)
49
    {
50 1
        $resource = new Collection($response->data(), $this->transformer());
51 1
        $resource->setMetaValue('count', $response->count());
52 1
        $resource->setMetaValue('limit', $response->limit());
53 1
        $resource->setMetaValue('start', $response->start());
54
55 1
        return $this->response(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($...($resource)->toArray()) returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the documented return type array.
Loading history...
56 1
            $this->fractal($request)
57 1
                ->createData($resource)
58 1
                ->toArray()
59
        );
60
    }
61
62
    /**
63
     * @param ShowRequestInterface $request
64
     * @param             $entity
65
     *
66
     * @return array
67
     */
68 1
    public function show(ShowRequestInterface $request, $entity)
69
    {
70 1
        return $this->response(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($...sformer()))->toArray()) returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the documented return type array.
Loading history...
71 1
            $this->fractal($request)
72 1
                ->createData(new Item($entity, $this->transformer()))
73 1
                ->toArray()
74
        );
75
    }
76
77
    /**
78
     * @param CreateRequestInterface $request
79
     * @param               $entity
80
     *
81
     * @return array
82
     */
83 1
    public function create(CreateRequestInterface $request, $entity)
84
    {
85 1
        return $this->response(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($...sformer()))->toArray()) returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the documented return type array.
Loading history...
86 1
            $this->fractal($request)
87 1
                ->createData(new Item($entity, $this->transformer()))
88 1
                ->toArray()
89
        );
90
    }
91
92
    /**
93
     * @param UpdateRequestInterface $request
94
     * @param               $entity
95
     *
96
     * @return array
97
     */
98 1
    public function update(UpdateRequestInterface $request, $entity)
99
    {
100 1
        return $this->response(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response($...sformer()))->toArray()) returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the documented return type array.
Loading history...
101 1
            $this->fractal($request)
102 1
                ->createData(new Item($entity, $this->transformer()))
103 1
                ->toArray()
104
        );
105
    }
106
107
    /**
108
     * @param DeleteRequestInterface $request
109
     * @param               $entity
110
     *
111
     * @return JsonResponse
112
     */
113 1
    public function delete(DeleteRequestInterface $request, $entity)
114
    {
115 1
        return $this->response();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response() returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the return type mandated by Pz\Doctrine\Rest\RestResponseFactory::delete() of Pz\Doctrine\Rest\RestResponse.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
116
    }
117
118
    /**
119
     * @param RestRequestInterface $request
120
     *
121
     * @return JsonResponse
122
     */
123 3
    public function notFound(RestRequestInterface $request)
124
    {
125 3
        return $this->response(null, Response::HTTP_NOT_FOUND);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response(n...sponse::HTTP_NOT_FOUND) returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the return type mandated by Pz\Doctrine\Rest\RestResponseFactory::notFound() of Pz\Doctrine\Rest\RestResponse.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
126
    }
127
128
    /**
129
     * @param int    $httpStatus
130
     * @param string $message
131
     * @param array  $errors
132
     *
133
     * @return JsonResponse
134
     */
135
    public function error($httpStatus, $message, $errors)
136
    {
137
        return $this->response(['message' => $message, 'errors' => $errors,], $httpStatus);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->response(a... $errors), $httpStatus) returns the type Symfony\Component\HttpFoundation\JsonResponse which is incompatible with the return type mandated by Pz\Doctrine\Rest\RestResponseFactory::error() of Pz\Doctrine\Rest\RestResponse.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
138
    }
139
140
    /**
141
     * Return configured fractal by request format.
142
     *
143
     * @param RestRequestInterface $request
144
     *
145
     * @return Manager
146
     */
147 4
    protected function fractal(/** @scrutinizer ignore-unused */ RestRequestInterface $request)
148
    {
149 4
        if ($this->fractal === null) {
150 4
            $this->fractal = new Manager();
151
        }
152
153 4
        return $this->fractal;
154
    }
155
156
    /**
157
     * @return TransformerAbstract
158
     */
159 4
    protected function transformer()
160
    {
161 4
        return $this->transformer;
162
    }
163
164
    /**
165
     * @param null $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
166
     * @param int  $httStatus
167
     *
168
     * @return JsonResponse
169
     */
170 8
    protected function response($data = null, $httStatus = Response::HTTP_OK)
171
    {
172 8
        return new JsonResponse($data, $httStatus);
173
    }
174
}
175