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

LongWorkflow   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 16 3
A run() 0 30 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Temporal\Workflow;
6
7
use App\Temporal\Activity\CommonActivity;
8
use Temporal\Activity\ActivityOptions;
0 ignored issues
show
Bug introduced by
The type Temporal\Activity\ActivityOptions 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...
9
use Temporal\Promise;
0 ignored issues
show
Bug introduced by
The type Temporal\Promise 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 Temporal\Workflow;
0 ignored issues
show
Bug introduced by
The type Temporal\Workflow 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
12
#[\Temporal\Workflow\WorkflowInterface]
13
final class LongWorkflow
14
{
15
    private array $done = [];
16
    private string $status = 'start';
17
    private float $start = 999999999999;
18
    private float $end = 0;
19
20
    #[Workflow\QueryMethod]
21
    public function getStatus(): array
22
    {
23
        $time = $this->end > 0 ? $this->end - $this->start : '---';
24
        $status = $this->status === 'done'
25
            ? sprintf(
26
                'Processed %d tasks in %f seconds.',
27
                count($this->done),
28
                $time
29
            )
30
            : $this->status;
31
32
        return [
33
            'status' => $status,
34
            'total time' => $time,
35
            'done' => $this->done,
36
        ];
37
    }
38
39
    #[\Temporal\Workflow\WorkflowMethod('long_workflow')]
40
    public function run(string $name, int $count): \Generator
41
    {
42
        $activity = Workflow::newActivityStub(
43
            CommonActivity::class,
44
            ActivityOptions::new()
45
                ->withStartToCloseTimeout(6)
46
        );
47
48
        $promises = [];
49
50
        foreach (range(1, $count) as $item) {
51
            $promises[] = $activity->slow($name)
52
                ->then(
53
                    function ($result) use ($item) {
54
                        $this->start = min($this->start, $result['start']);
55
                        $this->end = max($this->end, $result['end']);
56
                        $this->done['Task #' . $item] = $result;
57
                        return $result;
58
                    }
59
                );
60
        }
61
62
        $this->status = 'processing';
63
64
        $result = yield Promise::all($promises);
65
66
        $this->status = 'done';
67
68
        return $result;
69
    }
70
}
71