Passed
Push — master ( dca89f...91c2eb )
by Anton
02:26
created

JobRegistry::getPipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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 Psr\Container\ContainerInterface;
15
16
/**
17
 * Provides the ability to associate custom handlers and serializes with the specific job name.
18
 */
19
final class JobRegistry implements HandlerRegistryInterface, SerializerRegistryInterface
20
{
21
    /** @var array */
22
    private $handlers = [];
23
24
    /** @var array */
25
    private $serializers = [];
26
27
    /** @var array */
28
    private $pipelines = [];
29
30
    /** @var ContainerInterface */
31
    private $container;
32
33
    /** @var HandlerRegistryInterface */
34
    private $fallbackHandlers;
35
36
    /** @var SerializerRegistryInterface */
37
    private $fallbackSerializers;
38
39
    /**
40
     * @param ContainerInterface $container
41
     * @param HandlerRegistryInterface $handlers
42
     * @param SerializerRegistryInterface $serializers
43
     */
44
    public function __construct(
45
        ContainerInterface $container,
46
        HandlerRegistryInterface $handlers,
47
        SerializerRegistryInterface $serializers
48
    ) {
49
        $this->container = $container;
50
        $this->fallbackHandlers = $handlers;
51
        $this->fallbackSerializers = $serializers;
52
    }
53
54
    /**
55
     * @param string $jobType
56
     * @param HandlerInterface|string $handler
57
     */
58
    public function setHandler(string $jobType, $handler): void
59
    {
60
        $this->handlers[$jobType] = $handler;
61
    }
62
63
    /**
64
     * @param string $jobType
65
     * @return HandlerInterface
66
     */
67
    public function getHandler(string $jobType): HandlerInterface
68
    {
69
        if (isset($this->handlers[$jobType])) {
70
            if ($this->handlers[$jobType] instanceof HandlerInterface) {
71
                return $this->handlers[$jobType];
72
            }
73
74
            return $this->container->get($this->handlers[$jobType]);
75
        }
76
77
        return $this->fallbackHandlers->getHandler($jobType);
78
    }
79
80
    /**
81
     * @param string $jobType
82
     * @param SerializerInterface|string $serializer
83
     */
84
    public function setSerializer(string $jobType, $serializer): void
85
    {
86
        $this->serializers[$jobType] = $serializer;
87
    }
88
89
    /**
90
     * @param string $jobType
91
     * @return SerializerInterface
92
     */
93
    public function getSerializer(string $jobType): SerializerInterface
94
    {
95
        if (isset($this->serializers[$jobType])) {
96
            if ($this->serializers[$jobType] instanceof SerializerInterface) {
97
                return $this->serializers[$jobType];
98
            }
99
100
            return $this->container->get($this->serializers[$jobType]);
101
        }
102
103
        if (!isset($this->handlers[$jobType])) {
104
            return $this->fallbackSerializers->getSerializer($jobType);
105
        }
106
107
        $handler = $this->getHandler($jobType);
108
        if ($handler instanceof SerializerInterface) {
109
            return $handler;
110
        }
111
112
        return $this->fallbackSerializers->getSerializer($jobType);
113
    }
114
115
    /**
116
     * @param string $jobType
117
     * @param string $pipeline
118
     */
119
    public function setPipeline(string $jobType, string $pipeline): void
120
    {
121
        $this->pipelines[$jobType] = $pipeline;
122
    }
123
124
    /**
125
     * @param string $jobType
126
     * @return string|null
127
     */
128
    public function getPipeline(string $jobType): ?string
129
    {
130
        return $this->pipelines[$jobType] ?? null;
131
    }
132
}
133