Passed
Push — master ( c5079b...007c74 )
by Hector Luis
14:53 queued 07:23
created

Base::getXmlGroupPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $field is correct as it would always require null to be passed?
Loading history...
35
     * @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...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $field is correct as it would always require null to be passed?
Loading history...
45
     * @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...
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
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...
73
     * @return $this
74
     */
75
    public function setConfig($field, $value, $storeId = null): self
76
    {
77
        $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...
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
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...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $field is correct as it would always require null to be passed?
Loading history...
105
     * @return string
106
     */
107
    public function getXmlPathForField($field = null): string
108
    {
109
        $xmlPath = $this->getXmlBasePath() . '/' . $this->getXmlGroupPath();
0 ignored issues
show
Bug introduced by
It seems like getXmlGroupPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        $xmlPath = $this->getXmlBasePath() . '/' . $this->/** @scrutinizer ignore-call */ getXmlGroupPath();
Loading history...
110
        if ($field) {
0 ignored issues
show
introduced by
$field is of type null, thus it always evaluated to false.
Loading history...
111
            $xmlPath .= '/' . $field;
112
        }
113
        return $xmlPath;
114
    }
115
}
116