Skip to content
6 changes: 6 additions & 0 deletions examples/php/php-divergent_change-01_base/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions examples/php/php-divergent_change-01_base/scripts.sh
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
}