Passed
Push — master ( c00f72...cf3945 )
by Arthur
04:27
created

OptionsTrait   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 26
eloc 62
dl 0
loc 144
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B setSqlOptions() 0 23 7
A setOptionsOnNotDelete() 0 7 3
A setOptionsOnCreateUpdate() 0 13 4
A setDefaults() 0 6 1
A setCacheOpts() 0 15 5
A setConfigOptions() 0 19 5
A setMetaPageInfo() 0 5 1
1
<?php
2
3
namespace SoliDry\Extension;
4
5
use Illuminate\Http\Request;
6
use SoliDry\Helpers\ConfigHelper;
7
use SoliDry\Helpers\ConfigOptions;
8
use SoliDry\Helpers\Json;
9
use SoliDry\Helpers\MigrationsHelper;
10
use SoliDry\Helpers\SqlOptions;
11
use SoliDry\Types\ConfigInterface;
12
use SoliDry\Types\ModelsInterface;
13
use SoliDry\Types\ApiInterface;
14
15
/**
16
 * Trait OptionsTrait
17
 * @package SoliDry\Extension
18
 *
19
 * @property ConfigOptions configOptions
20
 */
21
trait OptionsTrait
22
{
23
    // default query params value
24
    private $defaultPage = ModelsInterface::DEFAULT_PAGE;
25
    private $defaultLimit = ModelsInterface::DEFAULT_LIMIT;
26
    private $defaultSort = '';
27
    private $isTree = false;
28
29
    /**
30
     * Sets SqlOptions params
31
     * @param Request $request
32
     * @return SqlOptions
33
     */
34
    private function setSqlOptions(Request $request) : SqlOptions
35
    {
36
        $sqlOptions = new SqlOptions();
37
        $page = ($request->input(ModelsInterface::PARAM_PAGE) === null) ? $this->defaultPage :
38
            $request->input(ModelsInterface::PARAM_PAGE);
39
        $limit = ($request->input(ModelsInterface::PARAM_LIMIT) === null) ? $this->defaultLimit :
40
            $request->input(ModelsInterface::PARAM_LIMIT);
41
        $sort = ($request->input(ModelsInterface::PARAM_SORT) === null) ? $this->defaultSort :
42
            $request->input(ModelsInterface::PARAM_SORT);
43
44
        $data = ($request->input(ModelsInterface::PARAM_DATA) === null) ? ModelsInterface::DEFAULT_DATA
45
            : Json::decode($request->input(ModelsInterface::PARAM_DATA));
46
        $orderBy = ($request->input(ModelsInterface::PARAM_ORDER_BY) === null) ? [ApiInterface::RAML_ID => $sort]
47
            : Json::decode($request->input(ModelsInterface::PARAM_ORDER_BY));
48
        $filter = ($request->input(ModelsInterface::PARAM_FILTER) === null) ? [] : Json::decode($request->input(ModelsInterface::PARAM_FILTER));
49
50
        $sqlOptions->setLimit($limit);
51
        $sqlOptions->setPage($page);
52
        $sqlOptions->setData($data);
53
        $sqlOptions->setOrderBy($orderBy);
54
        $sqlOptions->setFilter($filter);
55
56
        return $sqlOptions;
57
    }
58
59
    /**
60
     * Sets options based on config.php settings
61
     * @param string $calledMethod
62
     */
63
    private function setConfigOptions(string $calledMethod) : void
64
    {
65
        $this->configOptions = new ConfigOptions();
66
        $this->configOptions->setCalledMethod($calledMethod);
67
        $this->configOptions->setJwtIsEnabled(ConfigHelper::getNestedParam(ConfigInterface::JWT, ConfigInterface::ENABLED));
68
        $this->configOptions->setJwtTable(ConfigHelper::getNestedParam(ConfigInterface::JWT, ModelsInterface::MIGRATION_TABLE));
69
        if ($this->configOptions->getJwtIsEnabled() === true && $this->configOptions->getJwtTable() === MigrationsHelper::getTableName($this->entity)) {// if jwt enabled=true and tables are equal
70
            $this->configOptions->setIsJwtAction(true);
71
        }
72
73
        $this->setOptionsOnNotDelete($calledMethod);
74
75
        // set those only for create/update
76
        $this->setOptionsOnCreateUpdate($calledMethod);
77
78
        // set those only for index
79
        if ($calledMethod === JSONApiInterface::URI_METHOD_INDEX || $calledMethod === JSONApiInterface::URI_METHOD_VIEW) {
80
            $this->customSql = new CustomSql($this->entity);
0 ignored issues
show
Bug Best Practice introduced by
The property customSql does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
            $this->setCacheOpts();
82
        }
83
    }
84
85
    /**
86
     *  Sets cache options: xFetch, cacheBeta, time to live etc
87
     */
88
    private function setCacheOpts() : void
89
    {
90
        $entityCache = ConfigHelper::getNestedParam(ConfigInterface::CACHE, strtolower($this->entity));
91
        if ($entityCache !== null) {
92
            $this->configOptions->setIsCached($entityCache[ConfigInterface::ENABLED]);
93
            if (empty($entityCache[ConfigInterface::CACHE_STAMPEDE_XFETCH]) === false) {
94
                $this->configOptions->setIsXFetch($entityCache[ConfigInterface::CACHE_STAMPEDE_XFETCH]);
95
            }
96
97
            if (empty($entityCache[ConfigInterface::CACHE_STAMPEDE_BETA]) === false) {
98
                $this->configOptions->setCacheBeta($entityCache[ConfigInterface::CACHE_STAMPEDE_BETA]);
99
            }
100
101
            if (empty($entityCache[ConfigInterface::CACHE_TTL]) === false) {
102
                $this->configOptions->setCacheTtl($entityCache[ConfigInterface::CACHE_TTL]);
103
            }
104
        }
105
    }
106
107
    /**
108
     * @param string $calledMethod
109
     */
110
    private function setOptionsOnNotDelete(string $calledMethod) : void
111
    {
112
        if ($calledMethod !== JSONApiInterface::URI_METHOD_DELETE) {
113
            $bitMaskParams = ConfigHelper::getNestedParam(ConfigInterface::BIT_MASK, MigrationsHelper::getTableName($this->entity));
114
            if ($bitMaskParams !== null) {
115
                $this->configOptions->setBitMask(true);
116
                $this->bitMask = new BitMask($bitMaskParams);
0 ignored issues
show
Bug Best Practice introduced by
The property bitMask does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
117
            }
118
        }
119
    }
120
121
    /**
122
     * @param string $calledMethod
123
     */
124
    private function setOptionsOnCreateUpdate(string $calledMethod) : void
125
    {
126
        if (in_array($calledMethod, [JSONApiInterface::URI_METHOD_CREATE, JSONApiInterface::URI_METHOD_UPDATE]) === true) {
127
            // state machine for concrete entity == table
128
            $stateMachine = ConfigHelper::getNestedParam(ConfigInterface::STATE_MACHINE, MigrationsHelper::getTableName($this->entity));
129
            if ($stateMachine !== null) {
130
                $this->configOptions->setStateMachine(true);
131
            }
132
133
            // spell check if enabled
134
            $spellCheck = ConfigHelper::getNestedParam(ConfigInterface::SPELL_CHECK, MigrationsHelper::getTableName($this->entity));
135
            if ($spellCheck !== null) {
136
                $this->configOptions->setSpellCheck(true);
137
            }
138
        }
139
    }
140
141
    /**
142
     *  Sets the default config based parameters
143
     */
144
    private function setDefaults() : void
145
    {
146
        $this->defaultPage = ConfigHelper::getQueryParam(ModelsInterface::PARAM_PAGE);
147
        $this->defaultLimit = ConfigHelper::getQueryParam(ModelsInterface::PARAM_LIMIT);
148
        $this->defaultSort = ConfigHelper::getQueryParam(ModelsInterface::PARAM_SORT);
149
        $this->isTree = ConfigHelper::getNestedParam(ConfigInterface::TREES, $this->entity, true);
150
    }
151
152
    /**
153
     * Sets meta page info requested with index http request
154
     * for page, limit, count
155
     *
156
     * @param array $meta
157
     * @param SqlOptions $sqlOptions
158
     * @param int $count
159
     */
160
    private function setMetaPageInfo(array &$meta, SqlOptions $sqlOptions, int $count): void
161
    {
162
        $meta[ModelsInterface::PARAM_PAGE] = $sqlOptions->getPage();
163
        $meta[ModelsInterface::PARAM_LIMIT] = $sqlOptions->getLimit();
164
        $meta[ModelsInterface::COUNT] = $count;
165
    }
166
}