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
|
|
|
/** |
30
|
|
|
* @param null $path |
|
|
|
|
31
|
|
|
* @param null $storeId |
|
|
|
|
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function getConfig($path = null, $storeId = null) |
35
|
|
|
{ |
36
|
|
|
return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeId) ?: ''; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param null $path |
|
|
|
|
41
|
|
|
* @param null $storeId |
|
|
|
|
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function getConfigFlag($path = null, $storeId = null): bool |
45
|
|
|
{ |
46
|
|
|
return (bool)$this->getConfig($path, $storeId); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $scopeConfig |
51
|
|
|
*/ |
52
|
|
|
public function setScopeConfig($scopeConfig) |
53
|
|
|
{ |
54
|
|
|
$this->scopeConfig = $scopeConfig; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param ResourceConfig $resourceConfig |
59
|
|
|
*/ |
60
|
|
|
public function setResourceConfig(ResourceConfig $resourceConfig) |
61
|
|
|
{ |
62
|
|
|
$this->resourceConfig = $resourceConfig; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $path |
67
|
|
|
* @param $value |
68
|
|
|
* @param null $storeId |
|
|
|
|
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function setConfig($path, $value, $storeId = null): self |
72
|
|
|
{ |
73
|
|
|
$storeId = $storeId ?: Store::DEFAULT_STORE_ID; |
|
|
|
|
74
|
|
|
$scope = $storeId == Store::DEFAULT_STORE_ID ? ScopeConfigInterface::SCOPE_TYPE_DEFAULT : ScopeInterface::SCOPE_STORES; |
75
|
|
|
$this->resourceConfig->saveConfig($path, $value, $scope, $storeId); |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $field |
81
|
|
|
* @param $value |
82
|
|
|
* @param null $storeId |
|
|
|
|
83
|
|
|
* @return Base |
84
|
|
|
*/ |
85
|
|
|
public function setConfigFlag($field, $value, $storeId = null): self |
86
|
|
|
{ |
87
|
|
|
$value = (string)(int)(bool)$value; |
88
|
|
|
return $this->setConfig($field, $value, $storeId); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritDoc |
93
|
|
|
*/ |
94
|
|
|
public function setValue($path, $value, $storeId = null) |
95
|
|
|
{ |
96
|
|
|
return $this->setConfig($path, $value, $storeId); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|