Skip to content

Commit cbc3d83

Browse files
committed
[PHP] [StepStrategy] Implement the StepType hirearchy with a temporary approach to be able to finally pivot to the desired state
1 parent d61b106 commit cbc3d83

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepStrategy;
6+
7+
final class ExerciseStepType
8+
{
9+
public function code(): int
10+
{
11+
return Step::EXERCISE_STEP_TYPE;
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepStrategy;
6+
7+
final class QuizStepType
8+
{
9+
private array $quizQuestions;
10+
11+
public function __construct(array $quizQuestions)
12+
{
13+
$this->quizQuestions = $quizQuestions;
14+
}
15+
16+
public function code(): int
17+
{
18+
return Step::QUIZ_STEP_TYPE;
19+
}
20+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ final class Step
99
private const EXERCISE_STEP_DURATION_ESTIMATION_IN_MINUTES = 30;
1010
private const QUIZ_STEP_QUESTION_DURATION_ESTIMATION_IN_MINUTES = 3;
1111

12-
private const VIDEO_STEP_TYPE = 0;
13-
private const QUIZ_STEP_TYPE = 1;
14-
private const EXERCISE_STEP_TYPE = 2;
12+
public const VIDEO_STEP_TYPE = 0;
13+
public const QUIZ_STEP_TYPE = 1;
14+
public const EXERCISE_STEP_TYPE = 2;
1515

16-
private string $title;
17-
private int $type;
16+
private string $title;
17+
private int $type;
1818
private ?int $videoDurationInMinutes;
1919
private ?array $quizQuestions;
2020

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepStrategy;
6+
7+
use InvalidArgumentException;
8+
9+
abstract class StepType
10+
{
11+
public static function fromPrimitive(int $code, ?int $videoDurationInMinutes, ?array $quizQuestions)
12+
{
13+
switch ($code) {
14+
case Step::VIDEO_STEP_TYPE:
15+
return new VideoStepType($videoDurationInMinutes);
16+
case Step::QUIZ_STEP_TYPE:
17+
return new QuizStepType($quizQuestions);
18+
case Step::EXERCISE_STEP_TYPE:
19+
return new ExerciseStepType();
20+
default:
21+
throw new InvalidArgumentException("Unrecognized Step type code");
22+
}
23+
}
24+
25+
abstract public function code(): int;
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepStrategy;
6+
7+
final class VideoStepType extends StepType
8+
{
9+
private int $videoDurationInMinutes;
10+
11+
public function __construct(int $videoDurationInMinutes)
12+
{
13+
$this->videoDurationInMinutes = $videoDurationInMinutes;
14+
}
15+
16+
public function code(): int
17+
{
18+
return Step::VIDEO_STEP_TYPE;
19+
}
20+
}

0 commit comments

Comments
 (0)