|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Blackmine\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use Blackmine\Client\Client; |
|
8
|
|
|
use Blackmine\Model\Enumeration\DocumentCategory; |
|
9
|
|
|
use Blackmine\Model\Enumeration\IssuePriority; |
|
10
|
|
|
use Blackmine\Model\Enumeration\TimeEntryActivity; |
|
11
|
|
|
use Blackmine\Model\Issue\Status; |
|
12
|
|
|
use Blackmine\Model\ModelInterface; |
|
13
|
|
|
use Blackmine\Model\Project\Tracker; |
|
14
|
|
|
use Blackmine\Repository\Enumerations; |
|
15
|
|
|
use JsonException; |
|
16
|
|
|
|
|
17
|
|
|
class MetaFactory |
|
18
|
|
|
{ |
|
19
|
|
|
private Client $client; |
|
20
|
|
|
|
|
21
|
|
|
private array $factories = []; |
|
22
|
|
|
private array $factory_definitions = [ |
|
23
|
|
|
Status::class => null, |
|
24
|
|
|
Tracker::class => null, |
|
25
|
|
|
IssuePriority::class => Enumerations::ENUM_ISSUE_PRIORITIES, |
|
26
|
|
|
TimeEntryActivity::class => Enumerations::ENUM_TIME_ENTRY_ACTIVITIES, |
|
27
|
|
|
DocumentCategory::class => Enumerations::ENUM_TYPE_DOCUMENT_CATEGORIES |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(Client $client) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->client = $client; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @throws JsonException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function make(string $model_class, int $id): ?ModelInterface |
|
39
|
|
|
{ |
|
40
|
|
|
if (!isset($this->factories[$model_class])) { |
|
41
|
|
|
$this->factories[$model_class] = $this->getFactoryFor( |
|
42
|
|
|
$model_class, |
|
43
|
|
|
$this->factory_definitions[$model_class] |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($this->factories[$model_class] !== null) { |
|
48
|
|
|
return $this->factories[$model_class]::make($id); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @throws JsonException |
|
56
|
|
|
*/ |
|
57
|
|
|
private function getFactoryFor(string $model_class, ?string $custom_endpoint): FactoryInterface |
|
58
|
|
|
{ |
|
59
|
|
|
$repository_class = $model_class::getRepositoryClass(); |
|
60
|
|
|
$values = $this->client->getRepository($repository_class::API_ROOT)?->all($custom_endpoint)->toArray(); |
|
61
|
|
|
|
|
62
|
|
|
return new NamedIdentityFactory($values, $model_class); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|