diff --git a/src/Commands/SurveyheroMapperCommand.php b/src/Commands/SurveyheroMapperCommand.php index cac7885..723dad3 100644 --- a/src/Commands/SurveyheroMapperCommand.php +++ b/src/Commands/SurveyheroMapperCommand.php @@ -49,7 +49,7 @@ public function handle(): int $fileName = 'surveyhero_mapping.php'; $myfile = fopen($fileName, 'w') or exit('Unable to open file!'); - fwrite($myfile, "var_export_short($mapping) . "; \n"); + fwrite($myfile, "var_export_short($mapping)."; \n"); fclose($myfile); $this->comment("Mapping complete! [$fileName]"); diff --git a/src/Exceptions/QuestionMapperNotImplementedException.php b/src/Exceptions/QuestionMapperNotImplementedException.php index e79055d..090d093 100644 --- a/src/Exceptions/QuestionMapperNotImplementedException.php +++ b/src/Exceptions/QuestionMapperNotImplementedException.php @@ -1,8 +1,9 @@ $questionId, 'type' => $questionType, diff --git a/src/Services/Factories/QuestionMapper/ChoiceListQuestionMapper.php b/src/Services/Factories/QuestionMapper/ChoiceListQuestionMapper.php index 9d67775..02d0cf4 100644 --- a/src/Services/Factories/QuestionMapper/ChoiceListQuestionMapper.php +++ b/src/Services/Factories/QuestionMapper/ChoiceListQuestionMapper.php @@ -1,14 +1,15 @@ createQuestionMap($question->element_id, $question->question->type, SurveyAnswer::CONVERTED_TYPE_INT, @@ -17,6 +18,7 @@ public function mapQuestion(\stdClass $question, int $questionCounter): array { foreach ($question->question->choice_list->choices as $choiceKey => $choice) { $questionData['answer_mapping'][$choice->choice_id] = $choiceKey + 1; } + return $questionData; } } diff --git a/src/Services/Factories/QuestionMapper/ChoiceTableQuestionMapper.php b/src/Services/Factories/QuestionMapper/ChoiceTableQuestionMapper.php index 0864ee0..ca19559 100644 --- a/src/Services/Factories/QuestionMapper/ChoiceTableQuestionMapper.php +++ b/src/Services/Factories/QuestionMapper/ChoiceTableQuestionMapper.php @@ -1,25 +1,27 @@ question->choice_table->choices as $questionChoice){ + foreach ($question->question->choice_table->choices as $questionChoice) { $answerMapping[$questionChoice->choice_id] = $choiceCounter; - $choiceCounter ++; + $choiceCounter++; } //create subquestions: - foreach ($question->question->choice_table->rows as $rowQuestion){ + foreach ($question->question->choice_table->rows as $rowQuestion) { $questionData = $this->createQuestionMap($rowQuestion->row_id, $question->question->type, SurveyAnswer::CONVERTED_TYPE_INT, diff --git a/src/Services/Factories/QuestionMapper/InputQuestionMapper.php b/src/Services/Factories/QuestionMapper/InputQuestionMapper.php index 46bdd5b..bf79e55 100644 --- a/src/Services/Factories/QuestionMapper/InputQuestionMapper.php +++ b/src/Services/Factories/QuestionMapper/InputQuestionMapper.php @@ -1,13 +1,15 @@ createQuestionMap($question->element_id, $question->question->type, SurveyAnswer::CONVERTED_TYPE_STRING, diff --git a/src/Services/Factories/QuestionMapper/QuestionMapper.php b/src/Services/Factories/QuestionMapper/QuestionMapper.php index 4be4b58..b3c7eb4 100644 --- a/src/Services/Factories/QuestionMapper/QuestionMapper.php +++ b/src/Services/Factories/QuestionMapper/QuestionMapper.php @@ -1,7 +1,8 @@ createQuestionMap($question->element_id, $question->question->type, SurveyAnswer::CONVERTED_TYPE_STRING, diff --git a/src/Services/Factories/ResponseCreator/AbstractQuestionResponseCreator.php b/src/Services/Factories/ResponseCreator/AbstractQuestionResponseCreator.php index 5753335..ec8abd0 100644 --- a/src/Services/Factories/ResponseCreator/AbstractQuestionResponseCreator.php +++ b/src/Services/Factories/ResponseCreator/AbstractQuestionResponseCreator.php @@ -36,33 +36,38 @@ protected function findExistingQuestionResponse(string|int $surveyheroQuestionId } /** - * @param string $surveyheroQuestionId + * @param string $surveyheroQuestionId * @return SurveyQuestion + * * @throws QuestionNotImportedException */ - protected function findSurveyQuestion(string $surveyheroQuestionId): SurveyQuestion { + protected function findSurveyQuestion(string $surveyheroQuestionId): SurveyQuestion + { $surveyQuestion = SurveyQuestion::where('surveyhero_question_id', $surveyheroQuestionId)->first(); - if(!$surveyQuestion){ + if (! $surveyQuestion) { throw QuestionNotImportedException::create($surveyheroQuestionId, 'The question is not imported'); + } else { + return $surveyQuestion; } - else return $surveyQuestion; } - protected function findSurveyAnswer(SurveyQuestion $question, string $surveyheroAnswerId): SurveyAnswer { + protected function findSurveyAnswer(SurveyQuestion $question, string $surveyheroAnswerId): SurveyAnswer + { $surveyAnswer = SurveyAnswer::where('survey_question_id', $question->id) ->where('surveyhero_answer_id', $surveyheroAnswerId) ->first(); if (! $surveyAnswer) { throw AnswerNotImportedException::create($surveyheroAnswerId, "Make sure to import survey answer with Surveyhero ID $surveyheroAnswerId in the survey_answers table"); + } else { + return $surveyAnswer; } - else return $surveyAnswer; } /** - * @param SurveyQuestion $question - * @param SurveyResponse $response - * @param SurveyAnswer|null $answer + * @param SurveyQuestion $question + * @param SurveyResponse $response + * @param SurveyAnswer|null $answer * @return array{ 'survey_question_id': int, 'survey_response_id': int } */ protected function createSurveyQuestionResponseData(SurveyQuestion $question, @@ -117,18 +122,18 @@ protected function setChoiceAndConvertToDataType(mixed $mappedChoice, } } - protected function fetchOrCreateInputAnswer(SurveyQuestion $surveyQuestion, string $answerDataType, mixed $inputAnswer): SurveyAnswer { + protected function fetchOrCreateInputAnswer(SurveyQuestion $surveyQuestion, string $answerDataType, mixed $inputAnswer): SurveyAnswer + { //fetch or create answer: $answerData = []; $this->setChoiceAndConvertToDataType($this->transformInputToDataType($inputAnswer, $answerDataType), - $answerDataType, - $answerData, - null); + $answerDataType, + $answerData, + null); $surveyAnswerQuery = SurveyAnswer::where('survey_question_id', $surveyQuestion->id); - if(isset($answerData['converted_int_value'])){ + if (isset($answerData['converted_int_value'])) { $surveyAnswerQuery->where('converted_int_value', $answerData['converted_int_value']); - } - else if(isset($answerData['converted_string_value'])){ + } elseif (isset($answerData['converted_string_value'])) { $surveyAnswerQuery->where('converted_string_value', $answerData['converted_string_value']); } $surveyAnswer = $surveyAnswerQuery->first(); @@ -142,8 +147,9 @@ protected function fetchOrCreateInputAnswer(SurveyQuestion $surveyQuestion, stri return $surveyAnswer; } - protected function transformInputToDataType(mixed $input, string $dataType){ - switch($dataType){ + protected function transformInputToDataType(mixed $input, string $dataType) + { + switch($dataType) { case SurveyAnswer::CONVERTED_TYPE_INT: return intval($input); case SurveyAnswer::CONVERTED_TYPE_STRING: diff --git a/src/Services/Factories/ResponseCreator/ChoiceTableResponseCreator.php b/src/Services/Factories/ResponseCreator/ChoiceTableResponseCreator.php index 8c9dc9f..73d37da 100644 --- a/src/Services/Factories/ResponseCreator/ChoiceTableResponseCreator.php +++ b/src/Services/Factories/ResponseCreator/ChoiceTableResponseCreator.php @@ -2,8 +2,6 @@ namespace Statikbe\Surveyhero\Services\Factories\ResponseCreator; -use Statikbe\Surveyhero\Exceptions\AnswerNotImportedException; -use Statikbe\Surveyhero\Models\SurveyAnswer; use Statikbe\Surveyhero\Models\SurveyQuestionResponse; use Statikbe\Surveyhero\Models\SurveyResponse; diff --git a/src/Services/Factories/ResponseCreator/ChoicesResponseCreator.php b/src/Services/Factories/ResponseCreator/ChoicesResponseCreator.php index 0bda130..707586e 100644 --- a/src/Services/Factories/ResponseCreator/ChoicesResponseCreator.php +++ b/src/Services/Factories/ResponseCreator/ChoicesResponseCreator.php @@ -2,10 +2,6 @@ namespace Statikbe\Surveyhero\Services\Factories\ResponseCreator; -use Statikbe\Surveyhero\Exceptions\AnswerNotImportedException; -use Statikbe\Surveyhero\Exceptions\QuestionNotImportedException; -use Statikbe\Surveyhero\Models\SurveyAnswer; -use Statikbe\Surveyhero\Models\SurveyQuestion; use Statikbe\Surveyhero\Models\SurveyQuestionResponse; use Statikbe\Surveyhero\Models\SurveyResponse; @@ -60,8 +56,8 @@ public function updateOrCreateQuestionResponse( $responseData = $this->createSurveyQuestionResponseData($surveyQuestion, $response, $surveyAnswer); $responseList[] = SurveyQuestionResponse::updateOrCreate([ - 'id' => $existingQuestionResponse->id ?? null, - ], $responseData); + 'id' => $existingQuestionResponse->id ?? null, + ], $responseData); } return $responseList; diff --git a/src/Services/Factories/ResponseCreator/NumberResponseCreator.php b/src/Services/Factories/ResponseCreator/NumberResponseCreator.php index 0f642f8..53d8dd4 100644 --- a/src/Services/Factories/ResponseCreator/NumberResponseCreator.php +++ b/src/Services/Factories/ResponseCreator/NumberResponseCreator.php @@ -2,7 +2,6 @@ namespace Statikbe\Surveyhero\Services\Factories\ResponseCreator; -use Statikbe\Surveyhero\Exceptions\AnswerNotImportedException; use Statikbe\Surveyhero\Models\SurveyAnswer; use Statikbe\Surveyhero\Models\SurveyQuestionResponse; use Statikbe\Surveyhero\Models\SurveyResponse; diff --git a/src/Services/Factories/ResponseCreator/TextResponseCreator.php b/src/Services/Factories/ResponseCreator/TextResponseCreator.php index 6d4df05..9cc92fe 100644 --- a/src/Services/Factories/ResponseCreator/TextResponseCreator.php +++ b/src/Services/Factories/ResponseCreator/TextResponseCreator.php @@ -2,7 +2,6 @@ namespace Statikbe\Surveyhero\Services\Factories\ResponseCreator; -use Statikbe\Surveyhero\Exceptions\AnswerNotImportedException; use Statikbe\Surveyhero\Models\SurveyAnswer; use Statikbe\Surveyhero\Models\SurveyQuestionResponse; use Statikbe\Surveyhero\Models\SurveyResponse; diff --git a/src/Services/SurveyMappingService.php b/src/Services/SurveyMappingService.php index eedb9a5..0623bb7 100644 --- a/src/Services/SurveyMappingService.php +++ b/src/Services/SurveyMappingService.php @@ -7,20 +7,11 @@ use Statikbe\Surveyhero\Exceptions\SurveyNotMappedException; use Statikbe\Surveyhero\Http\SurveyheroClient; use Statikbe\Surveyhero\Models\Survey; -use Statikbe\Surveyhero\Models\SurveyAnswer; -use Statikbe\Surveyhero\Services\Factories\QuestionAndAnswerCreator\ChoiceListQuestionAndAnswerCreator; -use Statikbe\Surveyhero\Services\Factories\QuestionAndAnswerCreator\ChoiceTableQuestionAndAnswerCreator; -use Statikbe\Surveyhero\Services\Factories\QuestionAndAnswerCreator\RatingScaleQuestionAndAnswerCreator; use Statikbe\Surveyhero\Services\Factories\QuestionMapper\ChoiceListQuestionMapper; use Statikbe\Surveyhero\Services\Factories\QuestionMapper\ChoiceTableQuestionMapper; use Statikbe\Surveyhero\Services\Factories\QuestionMapper\InputQuestionMapper; use Statikbe\Surveyhero\Services\Factories\QuestionMapper\QuestionMapper; use Statikbe\Surveyhero\Services\Factories\QuestionMapper\RatingScaleQuestionMapper; -use Statikbe\Surveyhero\Services\Factories\ResponseCreator\ChoicesResponseCreator; -use Statikbe\Surveyhero\Services\Factories\ResponseCreator\ChoiceTableResponseCreator; -use Statikbe\Surveyhero\Services\Factories\ResponseCreator\NumberResponseCreator; -use Statikbe\Surveyhero\Services\Factories\ResponseCreator\QuestionResponseCreator; -use Statikbe\Surveyhero\Services\Factories\ResponseCreator\TextResponseCreator; class SurveyMappingService { @@ -56,22 +47,20 @@ public function map(Survey $survey): array foreach ($questions as $question) { $mapper = $this->getQuestionMapper($question->question->type); - if($mapper) { + if ($mapper) { //a mapper can return one question or multiple. $mappedQuestions = $mapper->mapQuestion($question, $questionCounter); - if(!empty($mappedQuestions)){ - if(is_array(array_values($mappedQuestions)[0])){ + if (! empty($mappedQuestions)) { + if (is_array(array_values($mappedQuestions)[0])) { //multiple questions mapped: $mapping['questions'] = array_merge($mapping['questions'], $mappedQuestions); } - } - else { + } else { //only one question mapped: $mapping['questions'][] = $mappedQuestions; } $questionCounter++; - } - else { + } else { throw QuestionMapperNotImplementedException::create($question->question->type); } } diff --git a/src/Services/SurveyResponseImportService.php b/src/Services/SurveyResponseImportService.php index 9c4e481..a72ac3c 100644 --- a/src/Services/SurveyResponseImportService.php +++ b/src/Services/SurveyResponseImportService.php @@ -128,11 +128,11 @@ private function createOrUpdateSurveyResponse(\stdClass $surveyheroResponse, Sur ]; //map link parameters: - if(isset($surveyheroResponse->link_parameters)) { + if (isset($surveyheroResponse->link_parameters)) { $linkParametersConfig = config('surveyhero.surveyhero_link_parameters_mapping', []); - foreach($linkParametersConfig as $surveyheroLinkParameter => $settings) { - if(isset($surveyheroResponse->link_parameters->{$surveyheroLinkParameter})) { - if(isset($settings['entity']) && isset($settings['value']) && isset($settings['field'])) { + foreach ($linkParametersConfig as $surveyheroLinkParameter => $settings) { + if (isset($surveyheroResponse->link_parameters->{$surveyheroLinkParameter})) { + if (isset($settings['entity']) && isset($settings['value']) && isset($settings['field'])) { //Map parameter to value of associated model $responseData[$settings['name']] = optional($settings['entity']::where($settings['value'], $surveyheroResponse->link_parameters->{$surveyheroLinkParameter})->first())->id; } else {