Passed
Pull Request — master (#577)
by Dmitriy
09:18
created

WorkflowController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 106
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A asynchronousStatusAction() 0 17 1
A simpleAction() 0 16 1
A asynchronousAction() 0 21 1
A __construct() 0 8 1
A response() 0 12 2
A complicatedAction() 0 16 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Temporal\Workflow\FastWorkflow;
8
use App\Temporal\Workflow\LongWorkflow;
9
use Psr\Http\Message\ResponseFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseFactoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Psr\Http\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Psr\Http\Message\StreamFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\StreamFactoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Temporal\Client\WorkflowClientInterface;
0 ignored issues
show
Bug introduced by
The type Temporal\Client\WorkflowClientInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Router\CurrentRoute;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Router\CurrentRoute was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\Router\UrlGeneratorInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Router\UrlGeneratorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
final class WorkflowController
17
{
18
    private WorkflowClientInterface $workflowClient;
19
    private ResponseFactoryInterface $responseFactory;
20
    private StreamFactoryInterface $streamFactory;
21
22
    public function __construct(
23
        WorkflowClientInterface $workflowClient,
24
        ResponseFactoryInterface $responseFactory,
25
        StreamFactoryInterface $streamFactory,
26
    ) {
27
        $this->workflowClient = $workflowClient;
28
        $this->responseFactory = $responseFactory;
29
        $this->streamFactory = $streamFactory;
30
    }
31
32
    public function simpleAction(CurrentRoute $route): ResponseInterface
33
    {
34
        $name = (string)$route->getArgument('name');
35
        $start = microtime(true);
36
37
        $wf = $this->workflowClient->newWorkflowStub(FastWorkflow::class);
38
        $result = $wf->run($name, 5);
39
40
        $end = microtime(true);
41
42
        $response = [
43
            'microtime' => $end - $start,
44
            'result' => $result,
45
        ];
46
47
        return $this->response($response);
48
    }
49
50
    public function complicatedAction(CurrentRoute $route): ResponseInterface
51
    {
52
        $name = (string)$route->getArgument('name');
53
        $start = microtime(true);
54
55
        $wf = $this->workflowClient->newWorkflowStub(LongWorkflow::class);
56
        $result = $wf->run($name, 5);
57
58
        $end = microtime(true);
59
60
        $response = [
61
            'microtime' => $end - $start,
62
            'result' => $result,
63
        ];
64
65
        return $this->response($response);
66
    }
67
68
    public function asynchronousAction(UrlGeneratorInterface $urlGenerator, CurrentRoute $route): ResponseInterface
69
    {
70
        $name = (string)$route->getArgument('name');
71
        $start = microtime(true);
72
73
        $wf = $this->workflowClient->newWorkflowStub(LongWorkflow::class);
74
        $process = $this->workflowClient->start($wf, $name, 10);
75
        $id = $process->getExecution()->getID();
76
77
        $end = microtime(true);
78
79
        $url = $urlGenerator->generate('temporal/asynchronous-status', ['id' => $id]);
80
81
        $delay = $end - $start;
82
        $response = <<<HTML
83
        Microtime: {$delay} <br>
84
        Job ID: {$id} <br>
85
        To see status of this job open <a href="{$url}" target="_blank">click here</a>.
86
        HTML;
87
88
        return $this->response($response);
89
    }
90
91
    public function asynchronousStatusAction(CurrentRoute $route)
92
    {
93
        $id = (string)$route->getArgument('id');
94
        $start = microtime(true);
95
96
        $wf = $this->workflowClient->newRunningWorkflowStub(LongWorkflow::class, $id);
97
        $result = $wf->getStatus();
98
99
        $end = microtime(true);
100
101
        $response = [
102
            'Note' => 'Please update the page to see results',
103
            'microtime' => $end - $start,
104
            'result' => $result,
105
        ];
106
107
        return $this->response($response);
108
    }
109
110
    private function response($data): ResponseInterface
111
    {
112
        $response = $this->responseFactory->createResponse(200);
113
        if (is_string($data)) {
114
            $content = $data;
115
            $response = $response->withHeader('Content-Type', 'text/html');
116
        } else {
117
            $content = json_encode($data, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
118
        }
119
120
        return $response->withBody(
121
            $this->streamFactory->createStream($content)
122
        );
123
    }
124
}
125