Passed
Push — master ( 25d0a6...5eda98 )
by Arthur
03:22
created

ConfigHelper::getParam()   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 1
1
<?php
2
3
namespace SoliDry\Helpers;
4
5
6
use SoliDry\Types\ConfigInterface;
7
use SoliDry\Types\ModelsInterface;
8
use SoliDry\Types\ModulesInterface;
9
use SoliDry\Types\PhpInterface;
10
11
/**
12
 * Class ConfigHelper
13
 * helps get/set entities in application module config
14
 *
15
 * @package SoliDry\Helpers
16
 */
17
class ConfigHelper
18
{
19
    private static $availableQueryParams = [
20
        ModelsInterface::PARAM_PAGE => ModelsInterface::DEFAULT_PAGE,
21
        ModelsInterface::PARAM_LIMIT => ModelsInterface::DEFAULT_LIMIT,
22
        ModelsInterface::PARAM_SORT => ModelsInterface::DEFAULT_SORT,
23
    ];
24
25
    /**
26
     * @return string
27
     */
28
    public static function getConfigKey(): string
29
    {
30
        $conf = config();
31
        $arr = $conf[ModulesInterface::KEY_MODULE][ModulesInterface::KEY_MODULES];
32
33
        return end($arr);
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public static function getModuleName(): string
40
    {
41
        return config(self::getConfigKey() . PhpInterface::DOT . ModulesInterface::KEY_NAME);
42
    }
43
44
    /**
45
     * @param string $param
46
     * @return mixed|null
47
     */
48
    public static function getQueryParam(string $param)
49
    {
50
        if (array_key_exists($param, self::$availableQueryParams)) {
51
            $params = config(self::getConfigKey() . PhpInterface::DOT . ConfigInterface::QUERY_PARAMS);
52
53
            return empty($params[$param]) ? self::$availableQueryParams[$param] : $params[$param];
54
        }
55
56
        return null;
57
    }
58
59
    /**
60
     * @param string $entity
61
     * @param string $param
62
     * @param bool $lower
63
     * @return mixed|null
64
     */
65
    public static function getNestedParam(string $entity, string $param, bool $lower = false)
66
    {
67
        if ($lower === true) {
68
            $param = strtolower($param);
69
        }
70
        $params = self::getParam($entity);
71
72
        return empty($params[$param]) ? null : $params[$param];
73
    }
74
75
    /**
76
     * @param string $entity
77
     * @return \Illuminate\Config\Repository|mixed
78
     */
79
    public static function getParam(string $entity)
80
    {
81
        return config(self::getConfigKey() . PhpInterface::DOT . $entity);
82
    }
83
}
84