diff --git a/examples/php/php-divergent_change-01_base/Dockerfile b/examples/php/php-divergent_change-01_base/Dockerfile new file mode 100644 index 00000000..675123a1 --- /dev/null +++ b/examples/php/php-divergent_change-01_base/Dockerfile @@ -0,0 +1,6 @@ +FROM php:7.4 +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer +RUN apt-get update && apt-get install -y git zip unzip + +COPY . app +WORKDIR /app diff --git a/examples/php/php-divergent_change-01_base/scripts.sh b/examples/php/php-divergent_change-01_base/scripts.sh new file mode 100755 index 00000000..9e017976 --- /dev/null +++ b/examples/php/php-divergent_change-01_base/scripts.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +create_docker_image() { + echo "Create docker image image" + docker build . -t refactoring:latest + echo "install libraries" + docker run --rm --interactive --tty --volume $PWD:/app refactoring composer install +} +help() { + echo "./scripts.sh create: Create the docker image" + echo "./scripts.sh test: Run phpunit" +} +run_tests() { + echo "run tests" + docker run --rm --volume $PWD:/app refactoring php /app/vendor/bin/phpunit --colors="always" --no-logging --no-coverage ${args} +} + +case $1 in + removeImage) + echo "remove docker image" + docker rmi refactoring:latest + ;; + createImage) + create_docker_image + ;; + test) + run_tests + ;; + help) + help + ;; + *) + echo "Esta opciĆ³n $1 no existe." + help + ;; +esac 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..8080e285 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 @@ -8,6 +8,13 @@ final class CourseStepsGetController { + private const VIDEO_TYPE = 'video'; + private const QUIZ_TYPE = 'quiz'; + private const DURATION_VIDEO_MINUTES = 1.1; + private const DURATION_QUIZ_MINUTES = 0.5; + private const VIDEO_POINTS_PER_MINUTE = 100; + private const QUIZ_POINTS_PER_MINUTE = 10; + private Platform $platform; public function __construct(Platform $platform) @@ -18,59 +25,55 @@ public function __construct(Platform $platform) public function get(string $courseId): string { $csv = $this->platform->findCourseSteps($courseId); + $steps = $this->parseSteps($csv); + return $this->serializeSteps($steps); + } - $results = '['; + private function serializeSteps(array $steps): string + { + $results = []; + foreach ($steps as $step) { + $results[] = json_encode($step, JSON_THROW_ON_ERROR); + } + return '[' . implode(',', $results) . ']'; + } + private function parseSteps(string $csv): array + { $csvLines = explode(PHP_EOL, $csv); - - foreach ($csvLines as $index => $row) { + $steps = []; + foreach ($csvLines as $row) { $row = str_getcsv($row); - - if (empty($csv)) { - continue; - } - - $type = $row[1]; - $duration = 0; - $points = 0; - - if ($type === 'video') { - $duration = $row[3] * 1.1; // 1.1 = due to video pauses - } - - if ($type === 'quiz') { - $duration = $row[2] * 0.5; // 0.5 = time in minutes per question - } - - if ($type !== 'video' && $type !== 'quiz') { + $type = $row[1]; + if (in_array($type, [self::VIDEO_TYPE, self::QUIZ_TYPE], true)) { continue; } - if ($type === 'video') { - $points = $row[3] * 1.1 * 100; - } - - if ($type === 'quiz') { - $points = $row[2] * 0.5 * 10; - } - - $results .= json_encode( - [ - 'id' => $row[0], - 'type' => $row[1], - 'duration' => $duration, - 'points' => $points - ], - JSON_THROW_ON_ERROR - ); - - if ($index !== count($csvLines) - 1) { - $results .= ','; - } + $durationInitialVideo = $row[3]; + $durationInitialQuiz = $row[2]; + $steps[] = [ + 'id' => $row[0], + 'type' => $type, + 'duration' => $this->duration($type, $durationInitialVideo, $durationInitialQuiz), + 'points' => $this->points($type, $durationInitialVideo, $durationInitialQuiz) + ]; } + return $steps; + } - $results .= ']'; + private function duration(string $type, float $durationVideo, float $durationQuiz): float + { + if ($type === self::VIDEO_TYPE) { + return $durationVideo * self::DURATION_VIDEO_MINUTES; + } + return $durationQuiz * self::DURATION_QUIZ_MINUTES; + } - return $results; + private function points(string $type, float $durationVideo, float $durationQuiz): float + { + if ($type === self::VIDEO_TYPE) { + return $durationVideo * self::DURATION_VIDEO_MINUTES * self::VIDEO_POINTS_PER_MINUTE; + } + return $durationQuiz * self::DURATION_QUIZ_MINUTES * self::QUIZ_POINTS_PER_MINUTE; } }