Base   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
eloc 14
c 4
b 0
f 0
dl 0
loc 74
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setResourceConfig() 0 3 1
A getConfig() 0 3 2
A setConfig() 0 6 3
A setValue() 0 3 1
A setScopeConfig() 0 3 1
A getConfigFlag() 0 3 1
A setConfigFlag() 0 4 1
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $path is correct as it would always require null to be passed?
Loading history...
31
     * @param null $storeId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $storeId is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $path is correct as it would always require null to be passed?
Loading history...
41
     * @param null $storeId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $storeId is correct as it would always require null to be passed?
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $storeId is correct as it would always require null to be passed?
Loading history...
69
     * @return $this
70
     */
71
    public function setConfig($path, $value, $storeId = null): self
72
    {
73
        $storeId = $storeId ?: Store::DEFAULT_STORE_ID;
0 ignored issues
show
introduced by
$storeId is of type null, thus it always evaluated to false.
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $storeId is correct as it would always require null to be passed?
Loading history...
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