-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvider.php
84 lines (72 loc) · 2.1 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
namespace Upmind\ProvisionBase;
use InvalidArgumentException;
use RuntimeException;
use Upmind\ProvisionBase\Exception\InvalidProviderJob;
use Upmind\ProvisionBase\Provider\Contract\ProviderInterface;
use Upmind\ProvisionBase\Registry\Data\ProviderRegister;
/**
* Provider wrapper class encapsulating a provision provider register and instance.
*/
class Provider
{
/**
* @var ProviderRegister
*/
protected $register;
/**
* @var ProviderInterface
*/
protected $instance;
/**
* @param ProviderRegister $register Provider register
* @param ProviderInterface $instance Provider instance
*/
public function __construct(ProviderRegister $register, ProviderInterface $instance)
{
$this->register = $register;
$this->instance = $instance;
if ($register->getClass() !== get_class($instance)) {
throw new InvalidArgumentException(
'The given provider register class does not match the given provider instance'
);
}
}
/**
* Get the provider register.
*/
public function getRegister(): ProviderRegister
{
return $this->register;
}
/**
* Get the provider instance.
*/
public function getInstance(): ProviderInterface
{
if (!isset($this->instance)) {
throw new RuntimeException('Provider instance has been unset');
}
return $this->instance;
}
/**
* Unset the provider instance (e.g, to trigger destructors).
*/
public function unsetInstance(): void
{
unset($this->instance);
}
/**
* Create a provider job instance to execute the given provision function.
*
* @param string $function Provision function name
* @param array|DataSet $parameterData Provision function parameters
*
* @throws InvalidProviderJob If the requested function is not supported
*/
public function makeJob(string $function, $parameterData): ProviderJob
{
return new ProviderJob($this, $function, $parameterData);
}
}