-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsService.php
68 lines (57 loc) · 2.03 KB
/
SettingsService.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
<?php
declare(strict_types=1);
namespace Garagist\Mautic\Service;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Configuration\ConfigurationManager;
use Garagist\Mautic\Service\NodeService;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Garagist\Mautic\Domain\Model\MauticEmail;
#[Flow\Scope('singleton')]
class SettingsService
{
#[Flow\Inject]
protected ConfigurationManager $configurationManager;
#[Flow\Inject]
protected NodeService $nodeService;
/**
* Get the path from the configuration
*
* @param string $settingPath
* @param string|NodeInterface|MauticEmail|null $siteNameOrSite
* @param string $rootPackage
* @return string
*/
public function path(string $settingPath, $sub = null, $rootPackage = 'Garagist.Mautic'): string
{
if ($sub instanceof MauticEmail) {
$sub = $this->nodeService->getNodeById($sub->getNodeIdentifier());
}
if ($sub instanceof NodeInterface) {
$sub = $this->nodeService->getSiteNameBasedOnNode($sub);
}
$siteName = is_string($sub) ? $sub : null;
$siteSettings = $this->getSetting($rootPackage, $settingPath, $siteName);
if (isset($siteSettings)) {
return $siteSettings;
}
return $this->getSetting($rootPackage, $settingPath);
}
/**
* Get the setting from the configuration
*
* @param string $settingPath
* @param string|null $siteName
* @return mixed
*/
protected function getSetting(string $rootPackage, string $settingPath, $siteName = null): mixed
{
if ($siteName && is_string($siteName)) {
$settingPath = sprintf('siteSettings.%s.%s', $siteName, $settingPath);
}
if (!str_starts_with($settingPath, $rootPackage)) {
$settingPath = sprintf('%s.%s', $rootPackage, $settingPath);
}
$setting = $this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, $settingPath);
return $setting ?? null;
}
}