Completed
Pull Request — master (#54)
by Anton
01:37
created

CollectAction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 64
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 6 1
A resolveQueueStatus() 0 29 4
A resolveHorizonQueueStatus() 0 10 3
A resolveIlluminateQueueStatus() 0 13 2
1
<?php
2
3
/*
4
 * This file is part of Laravel Paket.
5
 *
6
 * (c) Anton Komarev <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cog\Laravel\Paket\Http\Controllers\Api\Statuses;
15
16
use Cog\Laravel\Paket\Support\Queue\Events\QueueHasBeenPinged;
17
use Cog\Laravel\Paket\Support\Queue\Listeners\QueuePingListener;
18
use Illuminate\Contracts\Events\Dispatcher;
19
use Illuminate\Support\Facades\File;
20
use Laravel\Horizon\Contracts\MasterSupervisorRepository;
21
use Ramsey\Uuid\Uuid;
22
23
final class CollectAction
24
{
25
    public function __invoke()
26
    {
27
        return [
28
            'queue' => $this->resolveQueueStatus(),
29
        ];
30
    }
31
32
    private function resolveQueueStatus(): array
33
    {
34
        $queueConnection = config('queue.default');
35
36
        if ($queueConnection === 'sync') {
37
            return [
38
                'handler' => 'Illuminate',
39
                'connection' => $queueConnection,
40
                'status' => 'inactive',
41
            ];
42
        }
43
44
        if (interface_exists(MasterSupervisorRepository::class)) {
45
            $horizonQueueStatus = $this->resolveHorizonQueueStatus();
46
            if ($horizonQueueStatus !== 'inactive') {
47
                return [
48
                    'handler' => 'Horizon',
49
                    'connection' => $queueConnection,
50
                    'status' => $horizonQueueStatus,
51
                ];
52
            }
53
        }
54
55
        return [
56
            'handler' => 'Illuminate',
57
            'connection' => $queueConnection,
58
            'status' => $this->resolveIlluminateQueueStatus(),
59
        ];
60
    }
61
62
    private function resolveHorizonQueueStatus(): string
63
    {
64
        if (!$masters = app(MasterSupervisorRepository::class)->all()) {
65
            return 'inactive';
66
        }
67
68
        return collect($masters)->contains(function ($master) {
69
            return $master->status === 'paused';
70
        }) ? 'paused' : 'running';
71
    }
72
73
    private function resolveIlluminateQueueStatus(): string
74
    {
75
        $uuid = Uuid::uuid4()->toString();
0 ignored issues
show
Deprecated Code introduced by
The method Ramsey\Uuid\UuidInterface::toString() has been deprecated with message: In ramsey/uuid 4.0.0, this method will be replaced with the __toString() magic method, which is currently available in the Uuid concrete class. The new recommendation is to cast Uuid objects to string, rather than calling `toString()`.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
76
77
        $dispatcher = app(Dispatcher::class);
78
        $dispatcher->listen(QueueHasBeenPinged::class, QueuePingListener::class);
79
        $dispatcher->dispatch(new QueueHasBeenPinged($uuid));
80
        sleep(2);
81
82
        $storedUuid = File::get(storage_path('paket/queue-ping.log'));
83
84
        return $uuid === $storedUuid ? 'running' : 'inactive';
85
    }
86
}
87