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

Processes::getAllLogs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 35

Duplication

Lines 35
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 35
loc 35
ccs 0
cts 27
cp 0
rs 9.36
c 0
b 0
f 0
cc 4
nc 4
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\v1;
13
14
use Fig\Http\Message\StatusCodeInterface;
15
use Lcobucci\ContentNegotiation\UnformattedResponse;
16
use Micro\Auth\Identity;
17
use MongoDB\BSON\ObjectId;
18
use MongoDB\BSON\ObjectIdInterface;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use Tubee\Acl;
22
use Tubee\Log\Factory as LogFactory;
23
use Tubee\Process\Factory as ProcessFactory;
24
use Tubee\ResourceNamespace\Factory as ResourceNamespaceFactory;
25
use Tubee\Rest\Helper;
26
use Zend\Diactoros\Response;
27
28
class Processes
29
{
30
    /**
31
     * Namespace factory.
32
     *
33
     * @var ResourceNamespaceFactory
34
     */
35
    protected $namespace_factory;
36
37
    /**
38
     * Process factory.
39
     *
40
     * @var ProcessFactory
41
     */
42
    protected $process_factory;
43
44
    /**
45
     * Acl.
46
     *
47
     * @var Acl
48
     */
49
    protected $acl;
50
51
    /**
52
     * Log factory.
53
     *
54
     * @var LogFactory
55
     */
56
    protected $log_factory;
57
58
    /**
59
     * Init.
60
     */
61
    public function __construct(ProcessFactory $process_factory, Acl $acl, ResourceNamespaceFactory $namespace_factory, LogFactory $log_factory)
62
    {
63
        $this->process_factory = $process_factory;
64
        $this->acl = $acl;
65
        $this->namespace_factory = $namespace_factory;
66
        $this->log_factory = $log_factory;
67
    }
68
69
    /**
70
     * Entrypoint.
71
     */
72 View Code Duplication
    public function getAll(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
73
    {
74
        $query = array_merge([
75
            'offset' => 0,
76
            'limit' => 20,
77
        ], $request->getQueryParams());
78
79
        $namespace = $this->namespace_factory->getOne($namespace);
80
81
        if (isset($query['watch'])) {
82
            $cursor = $this->process_factory->watch($namespace, null, true, $query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']);
83
84
            return Helper::watchAll($request, $identity, $this->acl, $cursor);
85
        }
86
87
        $processes = $this->process_factory->getAll($namespace, $query['query'], $query['offset'], $query['limit'], $query['sort']);
88
89
        return Helper::getAll($request, $identity, $this->acl, $processes);
90
    }
91
92
    /**
93
     * Entrypoint.
94
     */
95 View Code Duplication
    public function getOne(ServerRequestInterface $request, Identity $identity, string $namespace, ObjectId $process): ResponseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
96
    {
97
        $namespace = $this->namespace_factory->getOne($namespace);
98
        $resource = $this->process_factory->getOne($namespace, $process);
99
100
        return Helper::getOne($request, $identity, $resource);
101
    }
102
103
    /**
104
     * Force trigger process.
105
     */
106 View Code Duplication
    public function post(ServerRequestInterface $request, Identity $identity, string $namespace): 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...
Duplication introduced by
This method seems to be duplicated in 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...
107
    {
108
        $body = $request->getParsedBody();
109
        $query = $request->getQueryParams();
110
111
        $namespace = $this->namespace_factory->getOne($namespace);
112
        $id = $this->process_factory->create($namespace, $body);
113
        $process = $this->process_factory->getOne($namespace, $id);
114
115
        return new UnformattedResponse(
116
            (new Response())->withStatus(StatusCodeInterface::STATUS_ACCEPTED),
117
            $process->decorate($request),
118
            ['pretty' => isset($query['pretty'])]
119
        );
120
    }
121
122
    /**
123
     * Stop process.
124
     */
125 View Code Duplication
    public function delete(ServerRequestInterface $request, Identity $identity, string $namespace, ObjectId $process): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $request 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 $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...
Duplication introduced by
This method seems to be duplicated in 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...
126
    {
127
        $namespace = $this->namespace_factory->getOne($namespace);
128
        $this->process_factory->deleteOne($namespace, $process);
129
130
        return (new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT);
131
    }
132
133
    /**
134
     * Entrypoint.
135
     */
136 View Code Duplication
    public function getAllLogs(ServerRequestInterface $request, Identity $identity, string $namespace, string $process): ResponseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
137
    {
138
        $query = array_merge([
139
            'offset' => 0,
140
            'limit' => 20,
141
        ], $request->getQueryParams());
142
143
        if (isset($query['watch'])) {
144
            $filter = [
145
                'fullDocument.context.namespace' => $namespace,
146
                'fullDocument.context.process' => $process,
147
            ];
148
149
            if (!empty($query['query'])) {
150
                $filter = ['$and' => [$filter, $query['query']]];
151
            }
152
153
            $logs = $this->log_factory->watch(null, true, $filter, (int) $query['offset'], (int) $query['limit'], $query['sort']);
154
155
            return Helper::watchAll($request, $identity, $this->acl, $logs);
156
        }
157
158
        $filter = [
159
            'context.namespace' => $namespace,
160
            'context.process' => $process,
161
        ];
162
163
        if (!empty($query['query'])) {
164
            $filter = ['$and' => [$filter, $query['query']]];
165
        }
166
167
        $logs = $this->log_factory->getAll($filter, (int) $query['offset'], (int) $query['limit'], $query['sort']);
168
169
        return Helper::getAll($request, $identity, $this->acl, $logs);
170
    }
171
172
    /**
173
     * Entrypoint.
174
     */
175
    public function getOneLog(ServerRequestInterface $request, Identity $identity, string $namespace, string $process, ObjectIdInterface $log): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $namespace 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 $process 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...
176
    {
177
        $resource = $this->log_factory->getOne($log);
0 ignored issues
show
Bug introduced by
The call to getOne() misses a required argument $log.

This check looks for function calls that miss required arguments.

Loading history...
178
179
        return Helper::getOne($request, $identity, $resource);
180
    }
181
}
182