Passed
Pull Request — master (#464)
by Kirill
04:42
created

Queue   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A serialize() 0 3 1
A jobName() 0 8 1
A pushAsync() 0 10 2
A push() 0 10 2
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Jobs;
13
14
use Spiral\Core\Container\SingletonInterface;
15
use Spiral\Goridge\RPC;
16
use Spiral\Jobs\Exception\JobException;
17
use Spiral\RoadRunner\Exception\RoadRunnerException;
18
19
final class Queue implements QueueInterface, SingletonInterface
20
{
21
    // RoadRunner jobs service
22
    private const RR_SERVICE = 'jobs';
23
24
    /** @var RPC */
25
    private $rpc;
26
27
    /** @var SerializerRegistryInterface */
0 ignored issues
show
Bug introduced by
The type Spiral\Jobs\SerializerRegistryInterface 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...
28
    private $serializerRegistry;
29
30
    /** @var \Doctrine\Inflector\Inflector */
31
    private $inflector;
32
33
    /**
34
     * @param RPC                         $rpc
35
     * @param SerializerRegistryInterface $registry
36
     */
37
    public function __construct(RPC $rpc, SerializerRegistryInterface $registry)
38
    {
39
        $this->rpc = $rpc;
40
        $this->serializerRegistry = $registry;
41
        $this->inflector = (new \Doctrine\Inflector\Rules\English\InflectorFactory())->build();
42
    }
43
44
    /**
45
     * Schedule job of a given type.
46
     *
47
     * @param string       $jobType
48
     * @param array        $payload
49
     * @param Options|null $options
50
     * @return string
51
     *
52
     * @throws JobException
53
     */
54
    public function push(string $jobType, array $payload = [], Options $options = null): string
0 ignored issues
show
Bug introduced by
The type Spiral\Jobs\Options 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...
55
    {
56
        try {
57
            return $this->rpc->call(self::RR_SERVICE . '.Push', [
58
                'job'     => $this->jobName($jobType),
59
                'payload' => $this->serialize($jobType, $payload),
60
                'options' => $options ?? new Options(),
61
            ]);
62
        } catch (RoadRunnerException | \Throwable $e) {
63
            throw new JobException($e->getMessage(), $e->getCode(), $e);
64
        }
65
    }
66
67
    /**
68
     * Schedule job of a given type.
69
     *
70
     * @param string       $jobType
71
     * @param array        $payload
72
     * @param Options|null $options
73
     * @return bool
74
     *
75
     * @throws JobException
76
     */
77
    public function pushAsync(string $jobType, array $payload = [], Options $options = null): bool
78
    {
79
        try {
80
            return $this->rpc->call(self::RR_SERVICE . '.PushAsync', [
81
                'job'     => $this->jobName($jobType),
82
                'payload' => $this->serialize($jobType, $payload),
83
                'options' => $options ?? new Options(),
84
            ]);
85
        } catch (RoadRunnerException | \Throwable $e) {
86
            throw new JobException($e->getMessage(), $e->getCode(), $e);
87
        }
88
    }
89
90
    /**
91
     * @param string $job
92
     * @return string
93
     */
94
    private function jobName(string $job): string
95
    {
96
        $names = explode('\\', $job);
97
        $names = array_map(function (string $value) {
98
            return $this->inflector->camelize($value);
99
        }, $names);
100
101
        return join('.', $names);
102
    }
103
104
    /**
105
     * @param string $jobType
106
     * @param array  $payload
107
     * @return string
108
     */
109
    private function serialize(string $jobType, array $payload): string
110
    {
111
        return $this->serializerRegistry->getSerializer($jobType)->serialize($jobType, $payload);
112
    }
113
}
114