Completed
Push — master ( e9a4c1...1f7593 )
by Raffael
09:08 queued 01:05
created

Helper::stream()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 44

Duplication

Lines 15
Ratio 34.09 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 15
loc 44
ccs 0
cts 34
cp 0
rs 8.9048
c 0
b 0
f 0
cc 5
nc 2
nop 4
crap 30
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee
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
        $error = null;
0 ignored issues
show
Unused Code introduced by
$error is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
77
        $stream = new StreamIterator($cursor, function ($resource) use ($request, $options) {
78
            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...
79
                echo  '[';
80
            } else {
81
                echo  ',';
82
            }
83
84
            echo json_encode($resource->decorate($request), $options);
85
86
            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...
87
                echo ']';
88
            }
89
90
            flush();
91 View Code Duplication
        }, function (\Throwable $e) {
0 ignored issues
show
Unused Code introduced by
The call to StreamIterator::__construct() has too many arguments starting with function (\Throwable $e)...) . ']'; flush(); }.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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...
92
            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...
93
                echo  '[';
94
            } else {
95
                echo  ',';
96
            }
97
98
            echo json_encode([
99
                'kind' => 'StreamError',
100
                'error' => $e->getMessage(),
101
                'class' => get_class($e),
102
            ]).']';
103
104
            flush();
105
        });
106
107
        return (new Response($stream))
108
            ->withHeader('X-Accel-Buffering', 'no')
109
            ->withHeader('Content-Type', 'application/json;stream')
110
            ->withStatus(StatusCodeInterface::STATUS_OK);
111
    }
112
113
    /**
114
     * Watch.
115
     */
116
    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...
117
    {
118
        //Watcher is valid for 5min, after a new requests needs to be sent
119
        ini_set('max_execution_time', '300');
120
121
        $query = $request->getQueryParams();
122
        $options = isset($query['pretty']) ? JSON_PRETTY_PRINT : 0;
123
124
        $stream = new StreamIterator($cursor, function ($event) use ($request, $options) {
125
            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...
126
                echo  '[';
127
            } else {
128
                echo  ',';
129
            }
130
131
            echo json_encode([
132
                $event[0],
133
                $event[1]->decorate($request),
134
            ], $options);
135
136
            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...
137
                echo ']';
138
            }
139
140
            flush();
141 View Code Duplication
        }, function (\Throwable $e) {
0 ignored issues
show
Unused Code introduced by
The call to StreamIterator::__construct() has too many arguments starting with function (\Throwable $e)...) . ']'; flush(); }.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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...
142
            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...
143
                echo  '[';
144
            } else {
145
                echo  ',';
146
            }
147
148
            echo json_encode(['ADDED', [
149
                'kind' => 'StreamError',
150
                'error' => $e->getMessage(),
151
                'class' => get_class($e),
152
            ]]).']';
153
154
            flush();
155
        });
156
157
        return (new Response($stream))
158
            ->withHeader('X-Accel-Buffering', 'no')
159
            ->withHeader('Content-Type', 'application/json;stream=watch')
160
            ->withStatus(StatusCodeInterface::STATUS_OK);
161
    }
162
}
163