• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tochka-developers / queue-promises / 9935584685

15 Jul 2024 07:44AM UTC coverage: 47.259% (-18.3%) from 65.524%
9935584685

push

github

darkdarin
fix: tests

4 of 4 new or added lines in 2 files covered. (100.0%)

431 existing lines in 40 files now uncovered.

612 of 1295 relevant lines covered (47.26%)

2.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Core/Support/PromiseQueueJob.php
1
<?php
2

3
namespace Tochka\Promises\Core\Support;
4

5
use Illuminate\Bus\Queueable;
6
use Illuminate\Container\Container;
7
use Illuminate\Contracts\Container\BindingResolutionException;
8
use Illuminate\Contracts\Queue\ShouldQueue;
9
use Illuminate\Support\Collection;
10
use Tochka\Promises\Contracts\JobFacadeContract;
11
use Tochka\Promises\Contracts\JobStateContract;
12
use Tochka\Promises\Contracts\MayPromised;
13
use Tochka\Promises\Contracts\PromisedEvent;
14
use Tochka\Promises\Contracts\PromiseHandler;
15
use Tochka\Promises\Enums\StateEnum;
16
use Tochka\Promises\Models\PromiseJob;
17
use Tochka\Promises\Support\PromisedJob;
18
use Tochka\Promises\Support\WaitEvent;
19

20
/**
21
 * Задача, выполняющая обработку результата промиса
22
 * @api
23
 */
24
class PromiseQueueJob implements ShouldQueue, MayPromised, JobStateContract, JobFacadeContract
25
{
26
    use Queueable;
27
    use PromisedJob;
28

29
    public function __construct(
30
        private readonly int $promise_id,
31
        private readonly PromiseHandler $promise_handler,
32
        private readonly StateEnum $state,
33
    ) {
UNCOV
34
        $this->base_job_id = $this->promise_handler->getBaseJobId();
×
35
    }
36

37
    /**
38
     * @throws \ReflectionException
39
     * @throws BindingResolutionException
40
     */
41
    public function handle(): void
42
    {
UNCOV
43
        $this->promise_handler->setPromiseId($this->promise_id);
×
44

UNCOV
45
        $results = $this->getResults();
×
UNCOV
46
        $handleResult = $this->dispatchMethodWithParams('before', $results);
×
47

UNCOV
48
        if ($handleResult !== false) {
×
UNCOV
49
            switch ($this->state->value) {
×
50
                case StateEnum::SUCCESS:
51
                    $this->dispatchMethodWithParams('success', $results);
×
UNCOV
52
                    break;
×
53
                case StateEnum::FAILED:
UNCOV
54
                    $this->dispatchMethodWithParams('failed', $results);
×
55
                    break;
×
56
                case StateEnum::TIMEOUT:
UNCOV
57
                    $this->dispatchMethodWithParams('timeout', $results);
×
58
                    break;
×
59
                default:
UNCOV
60
                    break;
×
61
            }
62

UNCOV
63
            $this->dispatchMethodWithParams('handle', $results);
×
64
        }
65

UNCOV
66
        $this->dispatchMethodWithParams('after', $results);
×
67
    }
68

69
    /**
70
     * @param array<class-string<MayPromised|PromisedEvent>, non-empty-list<MayPromised|PromisedEvent>> $results
71
     *
72
     * @throws \ReflectionException
73
     * @throws BindingResolutionException
74
     */
75
    protected function dispatchMethodWithParams(string $method, array $results): mixed
76
    {
UNCOV
77
        if (!method_exists($this->promise_handler, $method)) {
×
UNCOV
78
            return true;
×
79
        }
80

UNCOV
81
        $params = [];
×
82
        // подготавливаем аргументы для вызова метода
UNCOV
83
        $reflectionMethod = new \ReflectionMethod($this->promise_handler, $method);
×
84

85
        foreach ($reflectionMethod->getParameters() as $parameter) {
×
86
            $type = $this->getParamType($parameter);
×
87
            if (
UNCOV
88
                \in_array(MayPromised::class, class_implements($type), true)
×
89
                || \in_array(PromisedEvent::class, class_implements($type), true)
×
90
            ) {
91
                if (!empty($results[$type])) {
×
UNCOV
92
                    if ($parameter->isVariadic()) {
×
93
                        array_push($params, ...$results[$type]);
×
UNCOV
94
                        unset($results[$type]);
×
95
                        continue;
×
96
                    }
97

98
                    $params[] = array_shift($results[$type]);
×
99
                    continue;
×
100
                }
101

102
                if ($parameter->isVariadic()) {
×
103
                    continue;
×
104
                }
105

UNCOV
106
                if ($parameter->allowsNull()) {
×
UNCOV
107
                    $params[] = null;
×
108
                } else {
109
                    throw new \RuntimeException(
×
UNCOV
110
                        sprintf(
×
UNCOV
111
                            'Error while dispatch promise handler method [%s]. Parameter [%s] not allow null value, but result for this parameter is empty',
×
112
                            $reflectionMethod->getDeclaringClass()->getName() . '::' . $method,
×
113
                            $parameter->getName(),
×
UNCOV
114
                        ),
×
UNCOV
115
                    );
×
116
                }
117

UNCOV
118
                continue;
×
119
            }
120

121
            $params[] = Container::getInstance()->make($type);
×
122
        }
123

124
        return $this->promise_handler->$method(...$params);
×
125
    }
126

127
    /**
128
     * @return array<class-string<MayPromised|PromisedEvent>, non-empty-list<MayPromised|PromisedEvent>>
129
     */
130
    private function getResults(): array
131
    {
UNCOV
132
        $results = [];
×
133

134
        /** @var Collection<array-key,PromiseJob> $jobs */
UNCOV
135
        $jobs = PromiseJob::byPromise($this->getPromiseId())
×
UNCOV
136
            ->orderBy('id')
×
UNCOV
137
            ->get();
×
138

UNCOV
139
        foreach ($jobs as $job) {
×
UNCOV
140
            $resultJob = $job->getBaseJob()->getResultJob();
×
UNCOV
141
            $results[$resultJob::class][] = $resultJob;
×
142
            if ($resultJob instanceof WaitEvent) {
×
UNCOV
143
                $resultEvent = $resultJob->getEvent();
×
UNCOV
144
                if ($resultEvent !== null) {
×
145
                    $results[$resultEvent::class][] = $resultEvent;
×
146
                }
147
            }
148
        }
149

150
        return $results;
×
151
    }
152

153
    private function getParamType(\ReflectionParameter $parameter): string
154
    {
155
        $paramType = $parameter->getType();
×
UNCOV
156
        if (!$paramType instanceof \ReflectionNamedType) {
×
UNCOV
157
            return (string) $paramType;
×
158
        }
159

160
        return $paramType->getName();
×
161
    }
162

163
    public function getState(): StateEnum
164
    {
165
        return $this->state;
×
166
    }
167

168
    public function getPromiseId(): int
169
    {
170
        return $this->promise_id;
×
171
    }
172

173
    public function getJobHandler(): MayPromised
174
    {
175
        return $this->promise_handler;
×
176
    }
177

178
    public function getPromiseHandler(): PromiseHandler
179
    {
180
        return $this->promise_handler;
×
181
    }
182

183
    public function displayName(): string
184
    {
185
        return get_class($this->promise_handler);
×
186
    }
187

188
    public function tags(): array
189
    {
190
        return [
×
UNCOV
191
            $this->displayName() . ':' . $this->promise_id,
×
UNCOV
192
        ];
×
193
    }
194
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc