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