From 065cfce68ac0e09e6dcc71f2db6e0da6c42b3ff3 Mon Sep 17 00:00:00 2001 From: yeneastgate Date: Thu, 10 Aug 2023 16:22:16 +0700 Subject: [PATCH] 38234 refactor code --- .../Field/DefaultValue/DefaultValueRead.php | 103 ------------------ .../DefaultValueModelToOutputConverter.php | 6 +- tests/TestClassDefaultValueRead.php | 76 ------------- 3 files changed, 3 insertions(+), 182 deletions(-) diff --git a/plugin/Field/DefaultValue/DefaultValueRead.php b/plugin/Field/DefaultValue/DefaultValueRead.php index 4dde0f9e0..ad85cbac3 100644 --- a/plugin/Field/DefaultValue/DefaultValueRead.php +++ b/plugin/Field/DefaultValue/DefaultValueRead.php @@ -23,7 +23,6 @@ namespace onOffice\WPlugin\Field\DefaultValue; -use onOffice\WPlugin\Types\Field; use wpdb; use const OBJECT; use function esc_sql; @@ -49,21 +48,6 @@ public function __construct(wpdb $pWPDB) $this->_pWPDB = $pWPDB; } - /** - * @param int $formId - * @param Field $pField - * @return DefaultValueModelSingleselect - */ - public function readDefaultValuesSingleselect(int $formId, Field $pField): DefaultValueModelSingleselect - { - $query = $this->createBaseQuery($formId, $pField); - $row = $this->_pWPDB->get_row($query, ARRAY_A); - $pDataModel = new DefaultValueModelSingleselect($formId, $pField); - $pDataModel->setDefaultsId(!empty($row['defaults_id']) ? (int)$row['defaults_id'] : 0); - $pDataModel->setValue(!empty($row['value']) ? $row['value'] : ''); - return $pDataModel; - } - /** * @param int $formId * @param array $pFields @@ -78,93 +62,6 @@ public function readDefaultMultiValuesSingleSelect(int $formId, array $pFields): return $this->_pWPDB->get_results($query, OBJECT); } - /** - * @param int $formId - * @param Field $pField - * @return DefaultValueModelMultiselect - */ - public function readDefaultValuesMultiSelect(int $formId, Field $pField): DefaultValueModelMultiselect - { - $query = $this->createBaseQuery($formId, $pField); - $rows = $this->_pWPDB->get_results($query, ARRAY_A); - $values = array_column($rows, 'value'); - $pDataModel = new DefaultValueModelMultiselect($formId, $pField); - $pDataModel->setValues($values); - return $pDataModel; - } - - /** - * @param int $formId - * @param Field $pField - * @return DefaultValueModelBool - */ - public function readDefaultValuesBool(int $formId, Field $pField): DefaultValueModelBool - { - $query = $this->createBaseQuery($formId, $pField); - $row = $this->_pWPDB->get_row($query, ARRAY_A); - $pDataModel = new DefaultValueModelBool($formId, $pField); - $pDataModel->setDefaultsId(!empty($row['defaults_id']) ? (int)$row['defaults_id'] : 0); - $pDataModel->setValue(!empty($row['value']) ? (bool)intval($row['value']) : false); - - return $pDataModel; - } - - /** - * @param int $formId - * @param Field $pField - * @return DefaultValueModelText - */ - public function readDefaultValuesText(int $formId, Field $pField): DefaultValueModelText - { - $query = $this->createBaseQuery($formId, $pField); - $rows = $this->_pWPDB->get_results($query, OBJECT); - $pDataModel = new DefaultValueModelText($formId, $pField); - - foreach ($rows as $pRow) { - $pDataModel->addValueByLocale($pRow->locale, $pRow->value); - } - - return $pDataModel; - } - - /** - * @param int $formId - * @param Field $pField - * @return DefaultValueModelNumericRange - */ - public function readDefaultValuesNumericRange(int $formId, Field $pField): DefaultValueModelNumericRange - { - $query = $this->createBaseQuery($formId, $pField); - $rows = $this->_pWPDB->get_results($query, OBJECT); - $pDataModel = new DefaultValueModelNumericRange($formId, $pField); - - if (count($rows) === 2) { - $pDataModel->setValueFrom((float)$rows[0]->value); - $pDataModel->setValueTo((float)$rows[1]->value); - } - return $pDataModel; - } - - /** - * @param int $formId - * @param Field $pField - * @return string - */ - private function createBaseQuery(int $formId, Field $pField): string - { - $prefix = $this->_pWPDB->prefix; - $query = "SELECT {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id," - ."{$prefix}oo_plugin_fieldconfig_form_defaults_values.locale,\n" - ."{$prefix}oo_plugin_fieldconfig_form_defaults_values.value\n" - ."FROM {$prefix}oo_plugin_fieldconfig_form_defaults\n" - ."INNER JOIN {$prefix}oo_plugin_fieldconfig_form_defaults_values\n" - ."ON {$prefix}oo_plugin_fieldconfig_form_defaults.defaults_id = " - ." {$prefix}oo_plugin_fieldconfig_form_defaults_values.defaults_id\n" - ."WHERE {$prefix}oo_plugin_fieldconfig_form_defaults.fieldname = '".esc_sql($pField->getName())."' AND\n" - ." {$prefix}oo_plugin_fieldconfig_form_defaults.form_id = ".esc_sql($formId); - return $query; - } - /** * @param int $formId * @param array $pFieldNames diff --git a/plugin/Field/DefaultValue/ModelToOutputConverter/DefaultValueModelToOutputConverter.php b/plugin/Field/DefaultValue/ModelToOutputConverter/DefaultValueModelToOutputConverter.php index f39af6067..d4bfc8e5a 100644 --- a/plugin/Field/DefaultValue/ModelToOutputConverter/DefaultValueModelToOutputConverter.php +++ b/plugin/Field/DefaultValue/ModelToOutputConverter/DefaultValueModelToOutputConverter.php @@ -90,7 +90,7 @@ public function getConvertedMultiFields(int $formId, array $pFields): array case FieldTypes::isDateOrDateTime($pField->getType()): case FieldTypes::isNumericType($pField->getType()): case $pField->getType() === FieldTypes::FIELD_TYPE_SINGLESELECT: - $pDataModel = $this->createDefaultValueModelSingleSelect($formId, $pField, $rowData); + $pDataModel = $this->createDefaultValueSingleSelect($formId, $pField, $rowData); $pDataModels[ $pField->getName() ] = $pDataModel[0] ?? ''; break; case $pField->getType() === FieldTypes::FIELD_TYPE_MULTISELECT; @@ -139,7 +139,7 @@ public function getConvertedMultiFieldsForAdmin(int $formId, array $pFields): ar case FieldTypes::isDateOrDateTime($pField->getType()): case FieldTypes::isNumericType($pField->getType()): case $pField->getType() === FieldTypes::FIELD_TYPE_SINGLESELECT: - $pDataModel = $this->createDefaultValueModelSingleSelect($formId, $pField, $rowData); + $pDataModel = $this->createDefaultValueSingleSelect($formId, $pField, $rowData); break; case $pField->getType() === FieldTypes::FIELD_TYPE_MULTISELECT; $pDataModel = $this->createDefaultValuesMultiSelect($formId, $pField, $rowData); @@ -191,7 +191,7 @@ private function createDefaultValuesNumericRange(int $formId, Field $pField, arr * @throws DependencyException * @throws NotFoundException */ - private function createDefaultValueModelSingleSelect(int $formId, Field $pField, array $rows): array + private function createDefaultValueSingleSelect(int $formId, Field $pField, array $rows): array { $pDataModel = new DefaultValueModelSingleselect($formId, $pField); $pDataModel->setDefaultsId(isset($rows[0]->defaults_id) ? (int) $rows[0]->defaults_id : 0); diff --git a/tests/TestClassDefaultValueRead.php b/tests/TestClassDefaultValueRead.php index 6630a3e7c..ff28b7d9b 100644 --- a/tests/TestClassDefaultValueRead.php +++ b/tests/TestClassDefaultValueRead.php @@ -74,23 +74,6 @@ public function prepare() * */ - public function testReadDefaultValuesSingleselect(int $formId, int $defaultValueId, string $value) - { - $row = [ - 'defaults_id' => $defaultValueId, - 'value' => $value, - ]; - $this->_pWPDBMock->expects($this->once())->method('get_row')->will($this->returnValue($row)); - $pField = new Field('testField', 'testModule'); - $pExpectedDataModel = new DefaultValueModelSingleselect($formId, $pField); - $pExpectedDataModel->setValue($value); - $pExpectedDataModel->setDefaultsId($defaultValueId); - $pResult = $this->_pSubject->readDefaultValuesSingleselect($formId, $pField); - $this->assertInstanceOf(DefaultValueModelSingleselect::class, $pResult); - $this->assertEquals($pExpectedDataModel, $pResult); - } - - /** * * @return array @@ -105,21 +88,6 @@ public function dataProviderSingleSelect(): array ]; } - /** - * @dataProvider dataProviderMultiSelect - * @param int $formId - * @param array $rows - * @param DefaultValueModelMultiselect $pReference - */ - public function testReadDefaultValuesMultiSelect(int $formId, array $rows, DefaultValueModelMultiselect $pReference) - { - $this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue($rows)); - $pField = new Field('testField', 'testModule'); - $pResult = $this->_pSubject->readDefaultValuesMultiSelect($formId, $pField); - $this->assertInstanceOf(DefaultValueModelMultiselect::class, $pResult); - $this->assertEquals($pReference, $pResult); - } - /** * @return Generator */ @@ -160,16 +128,6 @@ public function dataProviderMultiSelect(): Generator * */ - public function testReadDefaultValuesText(int $formId, array $rows, DefaultValueModelText $pReference) - { - $this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue($rows)); - $pField = new Field('testField', 'testModule'); - - $pResult = $this->_pSubject->readDefaultValuesText($formId, $pField); - $this->assertInstanceOf(DefaultValueModelText::class, $pResult); - $this->assertEquals($pReference, $pResult); - } - /** * * @return Generator @@ -206,22 +164,6 @@ public function dataProviderText(): Generator yield [13, $rows, $pReference2]; } - /** - * @dataProvider dataProviderNumericRange - * @param int $formId - * @param array $rows - * @param DefaultValueModelNumericRange $pReference - */ - public function testReadDefaultValuesNumericRange(int $formId, array $rows, DefaultValueModelNumericRange $pReference) - { - $this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue($rows)); - $pField = new Field('testField', 'testModule'); - - $pResult = $this->_pSubject->readDefaultValuesNumericRange($formId, $pField); - $this->assertInstanceOf(DefaultValueModelNumericRange::class, $pResult); - $this->assertEquals($pReference, $pResult); - } - /** * @return array */ @@ -247,24 +189,6 @@ public function dataProviderNumericRange(): array ]; } - /** - * @dataProvider dataProviderBool - * @param int $formId - * @param array $row - * @param bool $expectedResult - */ - public function testReadDefaultValuesBool(int $formId, array $row, bool $expectedResult) - { - $this->_pWPDBMock->expects($this->once())->method('get_row')->will($this->returnValue($row)); - $pField = new Field('testField', 'testModule'); - $pExpectedDataModel = new DefaultValueModelBool($formId, $pField); - $pExpectedDataModel->setValue($expectedResult); - $pExpectedDataModel->setDefaultsId((int)$row['defaults_id']); - $pResult = $this->_pSubject->readDefaultValuesBool($formId, $pField); - $this->assertInstanceOf(DefaultValueModelBool::class, $pResult); - $this->assertEquals($pExpectedDataModel, $pResult); - } - /** * @return array */