Completed
Push — master ( 316baf...2178d1 )
by Raffael
67:25 queued 62:39
created

Helper::watchAll()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 25
cp 0
rs 9.408
c 0
b 0
f 0
cc 4
nc 2
nop 4
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee.io
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Rest;
13
14
use Fig\Http\Message\StatusCodeInterface;
15
use Lcobucci\ContentNegotiation\UnformattedResponse;
16
use Micro\Auth\Identity;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use StreamIterator\StreamIterator;
20
use Tubee\Acl;
21
use Tubee\Resource\ResourceInterface;
22
use Zend\Diactoros\Response;
23
24
class Helper
25
{
26
    /**
27
     * Entrypoint.
28
     */
29
    public static function getAll(ServerRequestInterface $request, Identity $identity, Acl $acl, Iterable $cursor): ResponseInterface
30
    {
31
        $query = $request->getQueryParams();
32
33 View Code Duplication
        if (isset($query['watch']) && $query['watch'] !== 'false' && !empty($query['watch'])) {
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...
34
            return self::watchAll($request, $identity, $acl, $cursor);
35
        }
36
37 View Code Duplication
        if (isset($query['stream']) && $query['stream'] !== 'false' && !empty($query['stream'])) {
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...
38
            return self::stream($request, $identity, $acl, $cursor);
39
        }
40
41
        $body = $acl->filterOutput($request, $identity, $cursor);
42
        $body = Pager::fromRequest($body, $request);
43
44
        return new UnformattedResponse(
45
            (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
46
            $body,
47
            ['pretty' => isset($query['pretty'])]
48
        );
49
    }
50
51
    /**
52
     * Entrypoint.
53
     */
54
    public static function getOne(ServerRequestInterface $request, Identity $identity, ResourceInterface $resource): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $identity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        $query = $request->getQueryParams();
57
58
        return new UnformattedResponse(
59
            (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
60
            $resource->decorate($request),
61
            ['pretty' => isset($query['pretty'])]
62
        );
63
    }
64
65
    /**
66
     * Watch.
67
     */
68
    public static function stream(ServerRequestInterface $request, Identity $identity, Acl $acl, Iterable $cursor): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $identity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $acl is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70
        //Stream is valid for 5min, after a new requests needs to be sent
71
        ini_set('max_execution_time', '300');
72
73
        $query = $request->getQueryParams();
74
        $options = isset($query['pretty']) ? JSON_PRETTY_PRINT : 0;
75
76
        $stream = new StreamIterator($cursor, function ($resource) use ($request, $options) {
77
            if ($this->tell() === 0) {
0 ignored issues
show
Bug introduced by
The method tell() does not seem to exist on object<Tubee\Rest\Helper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
                echo  '[';
79
            } else {
80
                echo  ',';
81
            }
82
83
            echo json_encode($resource->decorate($request), $options);
84
85
            if ($this->eof()) {
0 ignored issues
show
Bug introduced by
The method eof() does not seem to exist on object<Tubee\Rest\Helper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
                echo ']';
87
            }
88
89
            flush();
90
        });
91
92
        return (new Response($stream))
93
            ->withHeader('X-Accel-Buffering', 'no')
94
            ->withHeader('Content-Type', 'application/json;stream')
95
            ->withStatus(StatusCodeInterface::STATUS_OK);
96
    }
97
98
    /**
99
     * Watch.
100
     */
101
    public static function watchAll(ServerRequestInterface $request, Identity $identity, Acl $acl, Iterable $cursor): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $identity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $acl is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
        //Watcher is valid for 5min, after a new requests needs to be sent
104
        ini_set('max_execution_time', '300');
105
106
        $query = $request->getQueryParams();
107
        $options = isset($query['pretty']) ? JSON_PRETTY_PRINT : 0;
108
109
        $stream = new StreamIterator($cursor, function ($event) use ($request, $options) {
110
            if ($this->tell() === 0) {
0 ignored issues
show
Bug introduced by
The method tell() does not seem to exist on object<Tubee\Rest\Helper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
                echo  '[';
112
            } else {
113
                echo  ',';
114
            }
115
116
            echo json_encode([
117
                $event[0],
118
                $event[1]->decorate($request),
119
            ], $options);
120
121
            if ($this->eof()) {
0 ignored issues
show
Bug introduced by
The method eof() does not seem to exist on object<Tubee\Rest\Helper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
                echo ']';
123
            }
124
125
            flush();
126
        });
127
128
        return (new Response($stream))
129
            ->withHeader('X-Accel-Buffering', 'no')
130
            ->withHeader('Content-Type', 'application/json;stream=watch')
131
            ->withStatus(StatusCodeInterface::STATUS_OK);
132
    }
133
}
134