-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfbaseFactory.php
More file actions
60 lines (46 loc) · 1.3 KB
/
PerfbaseFactory.php
File metadata and controls
60 lines (46 loc) · 1.3 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
<?php
namespace Perfbase\Drupal\Runtime;
use Perfbase\Drupal\Config\ConfigResolver;
use Perfbase\Drupal\Support\ErrorHandler;
use Perfbase\SDK\Config;
use Perfbase\SDK\Perfbase;
/**
*
*/
class PerfbaseFactory implements PerfbaseFactoryInterface {
private ConfigResolver $configResolver;
private ErrorHandler $errorHandler;
private ?Perfbase $client = NULL;
private bool $attempted = FALSE;
public function __construct(ConfigResolver $configResolver, ErrorHandler $errorHandler) {
$this->configResolver = $configResolver;
$this->errorHandler = $errorHandler;
}
/**
*
*/
public function getClient(): ?Perfbase {
if ($this->attempted) {
return $this->client;
}
$this->attempted = TRUE;
$config = $this->configResolver->getConfig();
if (!$this->configResolver->isEnabled($config)) {
return NULL;
}
try {
$this->client = $this->instantiateClient($this->configResolver->getSdkConfig());
}
catch (\Throwable $exception) {
$this->errorHandler->handle($exception, $config, 'factory');
$this->client = NULL;
}
return $this->client;
}
/**
* @param array<string, mixed> $sdkConfig
*/
protected function instantiateClient(array $sdkConfig): Perfbase {
return new Perfbase(Config::fromArray($sdkConfig));
}
}