diff --git a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php index cd144d72..d1c07d04 100644 --- a/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php +++ b/examples/php/php-divergent_change-01_base/src/Controller/CourseStepsGetController.php @@ -5,72 +5,117 @@ namespace CodelyTv\DivergentChange\Controller; use CodelyTv\DivergentChange\Platform; +use CodelyTv\DivergentChange\Tests\Controller\CourseStepRepository; final class CourseStepsGetController { - private Platform $platform; - - public function __construct(Platform $platform) + private const VIDEO_DURATION_PAUSES_MULTIPLIER = 1.1; + private const QUIZ_TIME_PER_QUESTION_MULTIPLIER = 0.5; + private const STEP_TYPE_VIDEO = 'video'; + private const STEP_TYPE_QUIZ = 'quiz'; + private const VIDEO_POINTS_PER_MINUTE = 100; + private const QUIZ_POINTS_PER_MINUTE = 10; + private Platform $platform; + private CourseStepRepository $repository; + + public function __construct(Platform $platform, CourseStepRepository $repository) { $this->platform = $platform; + $this->repository = $repository; } public function get(string $courseId): string { - $csv = $this->platform->findCourseSteps($courseId); + $csv = $this->platform->findCourseSteps($courseId); + $parsedCsv = $this->parseCsv($csv); + $steps = $this->createStepsFromPrimitives($parsedCsv); - $results = '['; + return $this->toJson($steps); + } + + private function parseCsv(string $csv): array + { + if (empty($csv)) { + return []; + } $csvLines = explode(PHP_EOL, $csv); - foreach ($csvLines as $index => $row) { + $parsedCsv = []; + foreach ($csvLines as $row) { $row = str_getcsv($row); - if (empty($csv)) { + [$stepId, $type, $quizTotalQuestions, $videoDuration] = $row; + + $isRecognizedStepType = $type !== self::STEP_TYPE_VIDEO && $type !== self::STEP_TYPE_QUIZ; + if ($isRecognizedStepType) { continue; } - $type = $row[1]; - $duration = 0; - $points = 0; + $parsedCsv[] = [ + 'stepId' => $stepId, + 'type' => $type, + 'quizTotalQuestions' => $quizTotalQuestions, + 'videoDuration' => $videoDuration, + ]; + } - if ($type === 'video') { - $duration = $row[3] * 1.1; // 1.1 = due to video pauses - } + return $parsedCsv; + } - if ($type === 'quiz') { - $duration = $row[2] * 0.5; // 0.5 = time in minutes per question + private function createStepsFromPrimitives(array $parsedCsv): array + { + $steps = []; + foreach ($parsedCsv as $row) { + $stepId = $row['stepId']; + $type = $row['type']; + $quizTotalQuestions = $row['quizTotalQuestions']; + $videoDuration = $row['videoDuration']; + + $stepDurationInMinutes = 0; + $points = 0; + + if ($type === self::STEP_TYPE_VIDEO) { + $stepDurationInMinutes = $videoDuration * self::VIDEO_DURATION_PAUSES_MULTIPLIER; } - if ($type !== 'video' && $type !== 'quiz') { - continue; + if ($type === self::STEP_TYPE_QUIZ) { + $stepDurationInMinutes = $quizTotalQuestions * self::QUIZ_TIME_PER_QUESTION_MULTIPLIER; } - if ($type === 'video') { - $points = $row[3] * 1.1 * 100; + if ($type === self::STEP_TYPE_VIDEO) { + $points = $stepDurationInMinutes * self::VIDEO_POINTS_PER_MINUTE; } - if ($type === 'quiz') { - $points = $row[2] * 0.5 * 10; + if ($type === self::STEP_TYPE_QUIZ) { + $points = $stepDurationInMinutes * self::QUIZ_POINTS_PER_MINUTE; } - $results .= json_encode( - [ - 'id' => $row[0], - 'type' => $row[1], - 'duration' => $duration, - 'points' => $points - ], - JSON_THROW_ON_ERROR - ); - - if ($index !== count($csvLines) - 1) { + $steps[] = [ + 'id' => $stepId, + 'type' => $type, + 'duration' => $stepDurationInMinutes, + 'points' => $points, + ]; + } + + return $steps; + } + + private function toJson(array $steps): string + { + $results = '['; + + foreach ($steps as $index => $step) { + $results .= json_encode($step, JSON_THROW_ON_ERROR); + + $hasMoreRows = $index !== count($steps) - 1; + if ($hasMoreRows) { $results .= ','; } } $results .= ']'; - return $results; } } diff --git a/examples/php/php-divergent_change-01_base/tests/Controller/CourseStepRepository.php b/examples/php/php-divergent_change-01_base/tests/Controller/CourseStepRepository.php new file mode 100644 index 00000000..f0acb747 --- /dev/null +++ b/examples/php/php-divergent_change-01_base/tests/Controller/CourseStepRepository.php @@ -0,0 +1,10 @@ +