forked from zepgram/module-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigRequest.php
executable file
·109 lines (91 loc) · 2.81 KB
/
ConfigRequest.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
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
<?php
/**
* This file is part of Zepgram\Rest\Model
*
* @package Zepgram\Rest\Model
* @file ConfigRequest.php
* @date 04 11 2021 23:28
*
* @author Benjamin Calef <[email protected]>
* @copyright 2021 Zepgram Copyright (c) (https://github.com/zepgram)
* @license MIT License
**/
declare(strict_types=1);
namespace Zepgram\Rest\Model;
use Magento\Framework\App\Config\ScopeConfigInterface;
class ConfigRequest
{
/** @var string */
public const REST_API_SERVICE_BASE_URI = 'rest_api/%s/base_uri';
/** @var string */
public const REST_API_SERVICE_TIMEOUT = 'rest_api/%s/timeout';
/** @var string */
public const REST_API_SERVICE_IS_DEBUG = 'rest_api/%s/is_debug';
/** @var string */
public const REST_API_SERVICE_CACHE_TTL = 'rest_api/%s/cache_ttl';
/** @var string */
public const REST_API_DEFAULT_IS_FORCE_DEBUG = 'rest_api/default/is_force_debug';
/** @var string */
public const REST_API_CONFIG_DEFAULT = 'general';
public function __construct(
private ScopeConfigInterface $scopeConfig,
private string $configName
) {}
/**
* @return string
*/
public function getBaseUri(): string
{
return $this->scopeConfig->getValue(
sprintf(self::REST_API_SERVICE_BASE_URI, $this->configName)
);
}
/**
* @return int
*/
public function getTimeout(): int
{
$timeout = $this->scopeConfig->getValue(
sprintf(self::REST_API_SERVICE_TIMEOUT, $this->configName)
);
if ($timeout === null) {
$timeout = $this->scopeConfig->getValue(
sprintf(self::REST_API_SERVICE_TIMEOUT, self::REST_API_CONFIG_DEFAULT)
);
}
return (int) $timeout;
}
/**
* @return int
*/
public function getCacheLifetime(): int
{
$cacheLifetime = $this->scopeConfig->getValue(
sprintf(self::REST_API_SERVICE_CACHE_TTL, $this->configName)
);
if ($cacheLifetime === null) {
$cacheLifetime = $this->scopeConfig->getValue(
sprintf(self::REST_API_SERVICE_CACHE_TTL, self::REST_API_CONFIG_DEFAULT)
);
}
return (int) $cacheLifetime;
}
/**
* @return bool
*/
public function isDebugEnabled(): bool
{
if ($this->scopeConfig->isSetFlag(self::REST_API_DEFAULT_IS_FORCE_DEBUG)) {
return true;
}
$debugEnabled = $this->scopeConfig->getValue(
sprintf(self::REST_API_SERVICE_IS_DEBUG, $this->configName)
);
if ($debugEnabled === null) {
$debugEnabled = $this->scopeConfig->getValue(
sprintf(self::REST_API_SERVICE_IS_DEBUG, self::REST_API_CONFIG_DEFAULT)
);
}
return (bool) $debugEnabled;
}
}