Skip to content

Commit e699523

Browse files
committed
[PHP] [StepStrategy] Replace type code constant by abstract function making it mandatory to define codes for each type
1 parent a28d8ad commit e699523

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

examples/php/php-step_strategy-01_base/src/ExerciseStepType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ public function estimatedCompletionMinutes(): int
1212
{
1313
return self::EXERCISE_STEP_DURATION_ESTIMATION_IN_MINUTES;
1414
}
15+
16+
protected static function code(): int
17+
{
18+
return 2;
19+
}
1520
}

examples/php/php-step_strategy-01_base/src/QuizStepType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public function estimatedCompletionMinutes(): int
1919
{
2020
return self::QUIZ_STEP_QUESTION_DURATION_ESTIMATION_IN_MINUTES * count($this->quizQuestions);
2121
}
22+
23+
protected static function code(): int
24+
{
25+
return 1;
26+
}
2227
}

examples/php/php-step_strategy-01_base/src/StepType.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@
88

99
abstract class StepType
1010
{
11-
private const VIDEO_STEP_TYPE = 0;
12-
private const QUIZ_STEP_TYPE = 1;
13-
private const EXERCISE_STEP_TYPE = 2;
14-
1511
public static function fromPrimitive(int $code, ?int $videoDurationInMinutes, ?array $quizQuestions)
1612
{
1713
switch ($code) {
18-
case self::VIDEO_STEP_TYPE:
14+
case VideoStepType::code():
1915
return new VideoStepType($videoDurationInMinutes);
20-
case self::QUIZ_STEP_TYPE:
16+
case QuizStepType::code():
2117
return new QuizStepType($quizQuestions);
22-
case self::EXERCISE_STEP_TYPE:
18+
case ExerciseStepType::code():
2319
return new ExerciseStepType();
2420
default:
2521
throw new InvalidArgumentException("Unrecognized Step type code");
2622
}
2723
}
2824

25+
abstract protected static function code(): int;
26+
2927
abstract public function estimatedCompletionMinutes(): int;
3028
}

examples/php/php-step_strategy-01_base/src/VideoStepType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ public function estimatedCompletionMinutes(): int
1717
{
1818
return $this->videoDurationInMinutes;
1919
}
20+
21+
protected static function code(): int
22+
{
23+
return 0;
24+
}
2025
}

0 commit comments

Comments
 (0)