Completed
Push — master ( 4ce37f...6373f9 )
by Tobias
01:52 queued 11s
created

ResponseFactory::createWithArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Happyr\JsonApiResponseFactory;
6
7
use Happyr\JsonApiResponseFactory\Transformer\AbstractTransformer;
8
use League\Fractal\Manager;
9
use League\Fractal\Pagination\CursorInterface;
10
use League\Fractal\Pagination\PaginatorInterface;
11
use League\Fractal\Resource\Collection;
12
use League\Fractal\Resource\Item;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
15
/**
16
 * @author Tobias Nyholm <[email protected]>
17
 */
18
final class ResponseFactory
19
{
20
    private $fractal;
21
    private $paginator;
22
    private $cursor;
23
24 7
    public function __construct(Manager $fractal)
25
    {
26 7
        $this->fractal = $fractal;
27 7
    }
28
29
    public function getFractal(): Manager
30
    {
31
        return $this->fractal;
32
    }
33
34 2
    public function createWithItem($item, AbstractTransformer $transformer, array $meta = []): JsonResponse
35
    {
36 2
        $resource = new Item($item, $transformer, $transformer->getResourceName());
37 2
        $resource->setMeta($meta);
38 2
        $rootScope = $this->fractal->createData($resource);
39
40 2
        return $this->createWithArray($rootScope->toArray());
0 ignored issues
show
Bug introduced by
It seems like $rootScope->toArray() targeting League\Fractal\Scope::toArray() can also be of type null; however, Happyr\JsonApiResponseFa...tory::createWithArray() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
41
    }
42
43 2
    public function createWithCollection($collection, AbstractTransformer $transformer, array $meta = []): JsonResponse
44
    {
45 2
        $resource = new Collection($collection, $transformer, $transformer->getResourceName());
46 2
        $resource->setMeta($meta);
47 2
        if (null !== $this->paginator) {
48
            $resource->setPaginator($this->paginator);
49 2
        } elseif (null !== $this->cursor) {
50
            $resource->setCursor($this->cursor);
51
        }
52 2
        $rootScope = $this->fractal->createData($resource);
53
54 2
        return $this->createWithArray($rootScope->toArray());
0 ignored issues
show
Bug introduced by
It seems like $rootScope->toArray() targeting League\Fractal\Scope::toArray() can also be of type null; however, Happyr\JsonApiResponseFa...tory::createWithArray() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
55
    }
56
57 1
    public function withPaginator(PaginatorInterface $paginator): self
58
    {
59 1
        $new = clone $this;
60 1
        $new->paginator = $paginator;
61
62 1
        return $new;
63
    }
64
65 1
    public function withCursor(CursorInterface $cursor): self
66
    {
67 1
        $new = clone $this;
68 1
        $new->cursor = $cursor;
69
70 1
        return $new;
71
    }
72
73 1
    public function createWithResponseModel(ResponseModelInterface $model): JsonResponse
74
    {
75 1
        return $this->createWithArray($model->getPayload(), $model->getHttpStatusCode());
76
    }
77
78 5
    private function createWithArray(array $array, int $statusCode = 200): JsonResponse
79
    {
80 5
        return new JsonResponse($array, $statusCode, [
81 5
            'Content-Type' => 'application/vnd.api+json',
82
        ]);
83
    }
84
}
85