Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply Split Phase refactoring #26

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace CodelyTv\DivergentChange\Tests\Controller;

interface CourseStepRepository
{
public function search(CourseId $id): CourseSteps;
}