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

Workflows::watchAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 0
cts 11
cp 0
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 2
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 Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Rs\Json\Patch;
20
use Tubee\Acl;
21
use Tubee\ResourceNamespace\Factory as ResourceNamespaceFactory;
22
use Tubee\Rest\Helper;
23
use Tubee\Workflow\Factory as WorkflowFactory;
24
use Zend\Diactoros\Response;
25
26
class Workflows
27
{
28
    /**
29
     * ResourceNamespace factory.
30
     *
31
     * @var ResourceNamespaceFactory
32
     */
33
    protected $namespace_factory;
34
35
    /**
36
     * Workflow factory.
37
     *
38
     * @var WorkflowFactory
39
     */
40
    protected $workflow_factory;
41
42
    /**
43
     * Acl.
44
     *
45
     * @var Acl
46
     */
47
    protected $acl;
48
49
    /**
50
     * Init.
51
     */
52
    public function __construct(ResourceNamespaceFactory $namespace_factory, WorkflowFactory $workflow_factory, Acl $acl)
53
    {
54
        $this->namespace_factory = $namespace_factory;
55
        $this->workflow_factory = $workflow_factory;
56
        $this->acl = $acl;
57
    }
58
59
    /**
60
     * Entrypoint.
61
     */
62
    public function getAll(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $endpoint): ResponseInterface
63
    {
64
        $query = array_merge([
65
            'offset' => 0,
66
            'limit' => 20,
67
        ], $request->getQueryParams());
68
69
        $namespace = $this->namespace_factory->getOne($namespace);
70
        $endpoint = $namespace->getCollection($collection)->getEndpoint($endpoint);
71
72
        if (isset($query['watch'])) {
73
            $cursor = $this->workflow_factory->watch($endpoint, null, true, $query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']);
74
75
            return Helper::watchAll($request, $identity, $this->acl, $cursor);
76
        }
77
78
        $workflows = $endpoint->getWorkflows($query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']);
0 ignored issues
show
Unused Code introduced by
The call to EndpointInterface::getWorkflows() has too many arguments starting with $query['sort'].

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...
79
80
        return Helper::getAll($request, $identity, $this->acl, $workflows);
81
    }
82
83
    /**
84
     * Entrypoint.
85
     */
86 View Code Duplication
    public function getOne(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $endpoint, string $workflow): 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...
87
    {
88
        $namespace = $this->namespace_factory->getOne($namespace);
89
        $workflow = $namespace->getCollection($collection)->getEndpoint($endpoint)->getWorkflow($workflow);
90
91
        return Helper::getOne($request, $identity, $workflow);
92
    }
93
94
    /**
95
     * Create.
96
     */
97 View Code Duplication
    public function post(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $endpoint): 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...
98
    {
99
        $body = $request->getParsedBody();
100
        $query = $request->getQueryParams();
101
102
        $namespace = $this->namespace_factory->getOne($namespace);
103
        $endpoint = $namespace->getCollection($collection)->getEndpoint($endpoint);
104
        $this->workflow_factory->add($endpoint, $body);
105
106
        return new UnformattedResponse(
107
            (new Response())->withStatus(StatusCodeInterface::STATUS_CREATED),
108
            $endpoint->getWorkflow($body['name'])->decorate($request),
109
            ['pretty' => isset($query['pretty'])]
110
        );
111
    }
112
113
    /**
114
     * Delete.
115
     */
116
    public function delete(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $endpoint, string $workflow): 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...
117
    {
118
        $namespace = $this->namespace_factory->getOne($namespace);
119
        $endpoint = $namespace->getCollection($collection)->getEndpoint($endpoint);
120
        $this->workflow_factory->deleteOne($endpoint, $workflow);
121
122
        return(new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT);
123
    }
124
125
    /**
126
     * Patch.
127
     */
128 View Code Duplication
    public function patch(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $endpoint, string $workflow): 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...
129
    {
130
        $body = $request->getParsedBody();
131
        $query = $request->getQueryParams();
132
        $namespace = $this->namespace_factory->getOne($namespace);
133
        $endpoint = $namespace->getCollection($collection)->getEndpoint($endpoint);
134
        $workflow = $endpoint->getWorkflow($workflow);
135
        $doc = ['data' => $workflow->getData()];
136
137
        $patch = new Patch(json_encode($doc), json_encode($body));
138
        $patched = $patch->apply();
139
        $update = json_decode($patched, true);
140
141
        $this->workflow_factory->update($workflow, $update);
142
143
        return new UnformattedResponse(
144
            (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
145
            $endpoint->getWorkflow($workflow->getName())->decorate($request),
146
            ['pretty' => isset($query['pretty'])]
147
        );
148
    }
149
}
150