Completed
Push — dev-master ( e70af7...75df6a )
by Derek Stephen
02:11
created

HalCollection::process()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 59

Duplication

Lines 10
Ratio 16.95 %

Code Coverage

Tests 28
CRAP Score 5.0073

Importance

Changes 0
Metric Value
dl 10
loc 59
ccs 28
cts 30
cp 0.9333
rs 8.5833
c 0
b 0
f 0
cc 5
nc 16
nop 2
crap 5.0073

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Bone\Http\Middleware;
4
5
use League\Route\Dispatcher;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
11
class HalCollection implements MiddlewareInterface
12
{
13
    /** @var int $numPerPage */
14
    private $numPerPage;
15
16
    /**
17
     * HalCollection constructor.
18
     * @param int $numPerPage
19
     */
20 1
    public function __construct(int $numPerPage)
21
    {
22 1
        $this->numPerPage = $numPerPage;
23 1
    }
24
25
    /**
26
     * @param ServerRequestInterface $request
27
     * @param RequestHandlerInterface $handler
28
     * @return ResponseInterface
29
     */
30 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
31
    {
32 1
        $params = $request->getQueryParams();
33 1
        $page = isset($params['page']) ? (int) $params['page'] : 1;
34 1
        $limit = $this->numPerPage;
35 1
        $params['limit'] = $limit;
36 1
        $params['offset'] = ($page *  $limit) - $limit;
37 1
        $request = $request->withQueryParams($params);
38
39 1
        $response = $handler->handle($request);
40
41 1
        $uri = $request->getUri();
42 1
        $data = json_decode($response->getBody()->getContents(), true);
43 1
        $pageCount = (int) ceil($data['total'] / $limit);
44
45
        $hal = [
46
            '_links' => [
47
                'self' => [
48 1
                    'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath(),
49
                ],
50
                'first' => [
51 1
                    'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath(),
52
                ],
53
            ],
54
        ];
55
56 1 View Code Duplication
        if ($page !== 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
57
            $hal['_links']['prev'] = [
58
                'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath() . '?page=' . ($page - 1),
59
            ];
60
        }
61
62 1 View Code Duplication
        if ($page !== $pageCount) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63 1
            $hal['_links']['next'] = [
64 1
                'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath() . '?page=' . ($page + 1),
65
            ];
66
        }
67
68 1
        $hal['_links']['last'] = [
69 1
            'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath() . '?page=' . $pageCount,
70
        ];
71
72
        /** @todo add _self links for entities in collection */
73
74 1
        $data = array_merge($hal, $data);
75 1
        foreach ($data['_embedded'] as $key => $value) {
76 1
            $data['_embedded'][$key]['_links'] = [
77
                'self' => [
78 1
                    'href' => $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath() . '/' . $value['id'],
79
                ],
80
            ];
81
        }
82 1
        $body = $response->getBody();
83 1
        $body->rewind();
84 1
        $body->write(json_encode($data));
85 1
        $response = $response->withHeader('Content-Type', 'application/hal+json');
86
87 1
        return $response->withBody($body);
88
    }
89
}