1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Base Configuration Trait |
6
|
|
|
* @category Ticaje |
7
|
|
|
* @package Ticaje_Setting |
8
|
|
|
* @author Hector Luis Barrientos <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Ticaje\Configuration\Traits; |
12
|
|
|
|
13
|
|
|
use Magento\Config\Model\ResourceModel\Config as ResourceConfig; |
14
|
|
|
use Magento\Framework\App\Config\ScopeConfigInterface; |
15
|
|
|
use Magento\Store\Model\ScopeInterface; |
16
|
|
|
use Magento\Store\Model\Store; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Trait Base |
20
|
|
|
* @package Ticaje\Configuration\Traits |
21
|
|
|
* Classes using this trait must implement Ticaje\Configuration\Setting\ConfigInterface |
22
|
|
|
*/ |
23
|
|
|
trait Base |
24
|
|
|
{ |
25
|
|
|
protected $scopeConfig; |
26
|
|
|
|
27
|
|
|
protected $resourceConfig; |
28
|
|
|
|
29
|
|
|
protected $xmlGroupPath; |
30
|
|
|
|
31
|
|
|
protected $xmlBasePath; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param null $field |
|
|
|
|
35
|
|
|
* @param null $storeId |
|
|
|
|
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function getConfig($field = null, $storeId = null): string |
39
|
|
|
{ |
40
|
|
|
return $this->scopeConfig->getValue($this->getXmlPathForField($field), ScopeInterface::SCOPE_STORE, $storeId) ?: ''; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param null $field |
|
|
|
|
45
|
|
|
* @param null $storeId |
|
|
|
|
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function getConfigFlag($field = null, $storeId = null): bool |
49
|
|
|
{ |
50
|
|
|
return (bool)$this->getConfig($field, $storeId); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $scopeConfig |
55
|
|
|
*/ |
56
|
|
|
public function setScopeConfig($scopeConfig) |
57
|
|
|
{ |
58
|
|
|
$this->scopeConfig = $scopeConfig; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param ResourceConfig $resourceConfig |
63
|
|
|
*/ |
64
|
|
|
public function setResourceConfig(ResourceConfig $resourceConfig) |
65
|
|
|
{ |
66
|
|
|
$this->resourceConfig = $resourceConfig; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $field |
71
|
|
|
* @param $value |
72
|
|
|
* @param null $storeId |
|
|
|
|
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function setConfig($field, $value, $storeId = null): self |
76
|
|
|
{ |
77
|
|
|
$storeId = $storeId ?: Store::DEFAULT_STORE_ID; |
|
|
|
|
78
|
|
|
$scope = $storeId == Store::DEFAULT_STORE_ID ? ScopeConfigInterface::SCOPE_TYPE_DEFAULT : ScopeInterface::SCOPE_STORES; |
79
|
|
|
$this->resourceConfig->saveConfig($this->getXmlPathForField($field), $value, $scope, $storeId); |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $field |
85
|
|
|
* @param $value |
86
|
|
|
* @param null $storeId |
|
|
|
|
87
|
|
|
* @return Base |
88
|
|
|
*/ |
89
|
|
|
public function setConfigFlag($field, $value, $storeId = null): self |
90
|
|
|
{ |
91
|
|
|
$value = (string)(int)(bool)$value; |
92
|
|
|
return $this->setConfig($field, $value, $storeId); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getXmlBasePath(): string |
99
|
|
|
{ |
100
|
|
|
return $this->xmlBasePath; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param null $field |
|
|
|
|
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getXmlPathForField($field = null): string |
108
|
|
|
{ |
109
|
|
|
$xmlPath = $this->getXmlBasePath() . '/' . $this->getXmlGroupPath(); |
|
|
|
|
110
|
|
|
if ($field) { |
|
|
|
|
111
|
|
|
$xmlPath .= '/' . $field; |
112
|
|
|
} |
113
|
|
|
return $xmlPath; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|