This repository was archived by the owner on May 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServiceFactory.php
More file actions
85 lines (72 loc) · 2.29 KB
/
ServiceFactory.php
File metadata and controls
85 lines (72 loc) · 2.29 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
<?php
namespace LiveIntent\Services;
class ServiceFactory
{
/**
* The default options to use when creating services.
*
* @var array
*/
protected $options;
/**
* The shared token service to use for new services.
*
* @var \LiveIntent\Services\TokenService
*/
protected $tokenService;
/**
* A mapping of getters to service classes. This allows developers
* to access individual services directly as getters on the
* client, rather than instantiating every single service.
* @var array<string, class-string>
*/
protected static $classMap = [
'adSlots' => AdSlotService::class,
'advertisers' => AdvertiserService::class,
'auth' => AuthService::class,
'gam' => GamService::class,
'campaigns' => CampaignService::class,
'insertionOrders' => InsertionOrderService::class,
'lineItems' => LineItemService::class,
'mediaGroups' => MediaGroupService::class,
'newsletters' => NewsletterService::class,
'publishers' => PublisherService::class,
'users' => UserService::class,
// We'll expose a 'special' request helper to allow quick calls
// into the API, or for occasional manual overrides
'request' => BaseService::class,
];
/**
* Create a new instance.
*
* @return void
*/
public function __construct(array $options = [])
{
$this->options = $options;
$this->tokenService = new TokenService($options);
}
/**
* Dynamically resolve a service instance.
*
* @param string $name
* @return null|\LiveIntent\Services\BaseService
*/
public function make($name, array $options = [])
{
$options = array_merge($this->options, $options);
if (! \array_key_exists($name, static::$classMap)) {
return null;
}
$service = static::$classMap[$name];
return tap(new $service($options), function ($service) use ($options) {
$service->setTokenService($this->tokenService);
if (data_get($options, 'shouldRecord')) {
$service->saveSnapshots();
}
if (data_get($options, 'shouldFake')) {
$service->fake();
}
});
}
}