Skip to content

Commit 98cee8f

Browse files
committed
[PHP] [StepStrategy] Move method to StepType hierarchy
1 parent 570fce6 commit 98cee8f

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

examples/php/php-step_strategy-02_replace_type_code_with_strategy/src/ExerciseStepType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
final class ExerciseStepType extends StepType
88
{
9+
public const EXERCISE_STEP_DURATION_ESTIMATION_IN_MINUTES = 30;
10+
911
public function code(): int
1012
{
1113
return StepType::EXERCISE_STEP_TYPE;

examples/php/php-step_strategy-02_replace_type_code_with_strategy/src/QuizStepType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
final class QuizStepType extends StepType
88
{
9+
public const QUIZ_STEP_QUESTION_DURATION_ESTIMATION_IN_MINUTES = 3;
910
private array $quizQuestions;
1011

1112
public function __construct(array $quizQuestions)

examples/php/php-step_strategy-02_replace_type_code_with_strategy/src/Step.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66

77
final class Step
88
{
9-
private const EXERCISE_STEP_DURATION_ESTIMATION_IN_MINUTES = 30;
10-
private const QUIZ_STEP_QUESTION_DURATION_ESTIMATION_IN_MINUTES = 3;
11-
129
private string $title;
13-
private StepType $type;
14-
private ?int $videoDurationInMinutes;
15-
private ?array $quizQuestions;
10+
public StepType $type;
11+
public ?int $videoDurationInMinutes;
12+
public ?array $quizQuestions;
1613

1714
public function __construct(string $title, int $typeCode, ?int $videoDurationInMinutes, ?array $quizQuestions)
1815
{
@@ -24,25 +21,6 @@ public function __construct(string $title, int $typeCode, ?int $videoDurationInM
2421

2522
public function estimatedCompletionMinutes(): int
2623
{
27-
return $this->stepEstimatedCompletionMinutes();
28-
}
29-
30-
public function stepEstimatedCompletionMinutes()
31-
{
32-
$estimation = 0;
33-
34-
switch ($this->type->code()) {
35-
case StepType::VIDEO_STEP_TYPE:
36-
$estimation = $this->videoDurationInMinutes;
37-
break;
38-
case StepType::QUIZ_STEP_TYPE:
39-
$estimation = self::QUIZ_STEP_QUESTION_DURATION_ESTIMATION_IN_MINUTES * count($this->quizQuestions);
40-
break;
41-
case StepType::EXERCISE_STEP_TYPE:
42-
$estimation = self::EXERCISE_STEP_DURATION_ESTIMATION_IN_MINUTES;
43-
break;
44-
}
45-
46-
return $estimation;
24+
return $this->type->stepEstimatedCompletionMinutes($this);
4725
}
4826
}

examples/php/php-step_strategy-02_replace_type_code_with_strategy/src/StepType.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,24 @@ public static function fromPrimitive(int $code, ?int $videoDurationInMinutes, ?a
2626
}
2727
}
2828

29+
public function stepEstimatedCompletionMinutes(Step $instance)
30+
{
31+
$estimation = 0;
32+
33+
switch ($instance->type->code()) {
34+
case self::VIDEO_STEP_TYPE:
35+
$estimation = $instance->videoDurationInMinutes;
36+
break;
37+
case self::QUIZ_STEP_TYPE:
38+
$estimation = QuizStepType::QUIZ_STEP_QUESTION_DURATION_ESTIMATION_IN_MINUTES * count($instance->quizQuestions);
39+
break;
40+
case self::EXERCISE_STEP_TYPE:
41+
$estimation = ExerciseStepType::EXERCISE_STEP_DURATION_ESTIMATION_IN_MINUTES;
42+
break;
43+
}
44+
45+
return $estimation;
46+
}
47+
2948
abstract public function code(): int;
3049
}

0 commit comments

Comments
 (0)