-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGatewayResourceService.php
More file actions
175 lines (148 loc) · 5.43 KB
/
GatewayResourceService.php
File metadata and controls
175 lines (148 loc) · 5.43 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Service to find gateway resources by reference.
*
* This service provides methods to find resources from the gateway by their reference.
*
* @Author Robert Zondervan <robert@conduction.nl>, Wilco Louwerse <wilco@conduction.nl>
*
* @license EUPL <https://github.com/ConductionNL/contactcatalogus/blob/master/LICENSE.md>
*
* @package commongateway/corebundle
*
* @category Service
*/
namespace CommonGateway\CoreBundle\Service;
use App\Entity\Action;
use App\Entity\Endpoint;
use App\Entity\Entity;
use App\Entity\Gateway as Source;
use App\Entity\Mapping;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
class GatewayResourceService
{
/**
* @var EntityManagerInterface
*/
private EntityManagerInterface $entityManager;
/**
* @var LoggerInterface
*/
private LoggerInterface $pluginLogger;
/**
* The constructor sets al needed variables.
*
* @param EntityManagerInterface $entityManager
* @param LoggerInterface $pluginLogger
*/
public function __construct(EntityManagerInterface $entityManager, LoggerInterface $pluginLogger)
{
$this->entityManager = $entityManager;
$this->pluginLogger = $pluginLogger;
}//end __construct()
/**
* Get a schema by reference.
*
* @param string $reference The reference to look for.
* @param string $pluginName The name of the plugin that requests the resource.
*
* @return Entity|null
*/
public function getSchema(string $reference, string $pluginName): ?Entity
{
$entity = $this->entityManager->getRepository('App:Entity')->findOneBy(['reference' => $reference]);
if ($entity === null) {
$this->pluginLogger->error("No entity found for $reference.", ['plugin' => $pluginName]);
}//end if
return $entity;
}//end getSchema()
/**
* Get a mapping by reference.
*
* @param string $reference The reference to look for.
* @param string $pluginName The name of the plugin that requests the resource.
*
* @return Mapping|null
*/
public function getMapping(string $reference, string $pluginName): ?Mapping
{
$mapping = $this->entityManager->getRepository('App:Mapping')->findOneBy(['reference' => $reference]);
if ($mapping === null) {
$this->pluginLogger->error("No mapping found for $reference.", ['plugin' => $pluginName]);
}//end if
return $mapping;
}//end getMapping()
/**
* Get a source by reference.
*
* @param string $reference The reference to look for.
* @param string $pluginName The name of the plugin that requests the resource.
*
* @return Source|null
*/
public function getSource(string $reference, string $pluginName): ?Source
{
$source = $this->entityManager->getRepository('App:Gateway')->findOneBy(['reference' => $reference]);
if ($source === null) {
$this->pluginLogger->error("No source found for $reference.", ['plugin' => $pluginName]);
}//end if
return $source;
}//end getSource()
/**
* Find all sources that have a location that match the specified url.
* Todo: we should use a mongoDB filter instead of this, sources should exist in MongoDB.
*
* @param string $url The url we are trying to find a matching source for.
* @param string $pluginName The name of the plugin that requests these resources.
*
* @return array|null
*/
public function findSourcesForUrl(string $url, string $pluginName): ?array
{
$sources = [];
$allSources = $this->entityManager->getRepository('App:Gateway')->findAll();
foreach ($allSources as $source) {
// Todo: This works, we should go to php 8.0 later.
if (empty($source->getLocation()) === false && str_contains($url, $source->getLocation()) === true) {
$sources[] = $source;
}
}
if (empty($sources) === true) {
$this->pluginLogger->error("No sources found for $url.", ['plugin' => $pluginName]);
}//end if
return $sources;
}//end findSourcesForUrl()
/**
* Get a endpoint by reference.
*
* @param string $reference The location to look for.
* @param string $pluginName The name of the plugin that requests the resource.
*
* @return Endpoint|null
*/
public function getEndpoint(string $reference, string $pluginName): ?Endpoint
{
$endpoint = $this->entityManager->getRepository('App:Endpoint')->findOneBy(['reference' => $reference]);
if ($endpoint === null) {
$this->pluginLogger->error("No endpoint found for $reference.", ['plugin' => $pluginName]);
}//end if
return $endpoint;
}//end getEndpoint()
/**
* Get an action by reference.
*
* @param string $reference The reference to look for
* @param string $pluginName The name of the plugin that requests the resource.
*
* @return Action|null
*/
public function getAction(string $reference, string $pluginName): ?Action
{
$action = $this->entityManager->getRepository('App:Action')->findOneBy(['reference' => $reference]);
if ($action === null) {
$this->pluginLogger->error("No action found for $reference.", ['plugin' => $pluginName]);
}//end if
return $action;
}//end getAction()
}//end class