diff --git a/examples/php/php-step_shotgun_surgery-01_base/src/Application/GetStepDuration.php b/examples/php/php-step_shotgun_surgery-01_base/src/Application/GetStepDuration.php new file mode 100644 index 00000000..b6b193da --- /dev/null +++ b/examples/php/php-step_shotgun_surgery-01_base/src/Application/GetStepDuration.php @@ -0,0 +1,24 @@ +steps = $steps; + } + + public function __invoke(string $stepId): float + { + $step = $this->steps->find($stepId); + return StepDurationCalculatorFactory::build()->calculate($step); + } +} diff --git a/examples/php/php-step_shotgun_surgery-01_base/src/Domain/DurationMultiplier.php b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/DurationMultiplier.php new file mode 100644 index 00000000..9124c5a8 --- /dev/null +++ b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/DurationMultiplier.php @@ -0,0 +1,21 @@ +type() === StepEnums::STEP_TYPE_VIDEO) { + return StepEnums::STEP_DURATION_MULTIPLIER_VIDEO; + } + + if ($step->type() === StepEnums::STEP_TYPE_QUIZ) { + return StepEnums::STEP_DURATION_MULTIPLIER_QUIZ; + } + + return 1.0; + } +} diff --git a/examples/php/php-step_shotgun_surgery-01_base/src/Domain/Question.php b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/Question.php new file mode 100644 index 00000000..b30eb4ab --- /dev/null +++ b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/Question.php @@ -0,0 +1,9 @@ +questions = $questions; + } + + public function type(): string + { + return StepEnums::STEP_TYPE_QUIZ; + } + + public function questionCount(): int + { + return count($this->questions); + } +} diff --git a/examples/php/php-step_shotgun_surgery-01_base/src/Domain/Step.php b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/Step.php new file mode 100644 index 00000000..ccde418c --- /dev/null +++ b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/Step.php @@ -0,0 +1,22 @@ +id = $id; + } + + public function id(): string + { + return $this->id; + } + + abstract public function type(): string; +} diff --git a/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculator.php b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculator.php new file mode 100644 index 00000000..34e2558b --- /dev/null +++ b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculator.php @@ -0,0 +1,12 @@ +calculators = $calculators; + } + + public function supports(Step $step): bool + { + return in_array($step->type(), StepEnums::STEP_TYPES); + } + + public function calculate(Step $step): float + { + if (!$this->supports($step)) { + throw new RuntimeException("Missing calculator for step type {$step->type()}"); + } + + foreach ($this->calculators as $calculator) { + if ($calculator->supports($step)) { + return $calculator->calculate($step); + } + } + + throw new RuntimeException("Missing calculator for step type {$step->type()}"); + } +} diff --git a/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorFactory.php b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorFactory.php new file mode 100644 index 00000000..bc80a0ba --- /dev/null +++ b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorFactory.php @@ -0,0 +1,17 @@ +questionCount() * StepEnums::QUIZ_QUESTION_DURATION * DurationMultiplier::multiplierFor($step); + } +} diff --git a/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorVideo.php b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorVideo.php new file mode 100644 index 00000000..16999699 --- /dev/null +++ b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorVideo.php @@ -0,0 +1,18 @@ +videoDuration() * DurationMultiplier::multiplierFor($step); + } +} diff --git a/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepEnums.php b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepEnums.php new file mode 100644 index 00000000..b9c78b9e --- /dev/null +++ b/examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepEnums.php @@ -0,0 +1,22 @@ +videoDuration = $videoDuration; + } + + public function type(): string + { + return StepEnums::STEP_TYPE_VIDEO; + } + + public function videoDuration(): int + { + return $this->videoDuration; + } +} diff --git a/examples/php/php-step_shotgun_surgery-01_base/tests/Application/GetStepDurationTest.php b/examples/php/php-step_shotgun_surgery-01_base/tests/Application/GetStepDurationTest.php new file mode 100644 index 00000000..123f76ee --- /dev/null +++ b/examples/php/php-step_shotgun_surgery-01_base/tests/Application/GetStepDurationTest.php @@ -0,0 +1,58 @@ +stepRepository = $this->createMock(StepRepository::class); + $this->getStepDuration = new GetStepDuration($this->stepRepository); + } + + /** @test */ + public function shouldReturnVideoStepDuration(): void + { + $videoStep = new VideoStep('videoId', 10); + $this->givenStepRepositoryHas($videoStep); + + $duration = ($this->getStepDuration)('videoId'); + + self::assertSame(11.0, $duration); + } + + /** @test */ + public function shouldReturnQuizStepDuration(): void + { + $questions = [ + new Question(), + new Question(), + ]; + $quizStep = new QuizStep('videoId', ...$questions); + $this->givenStepRepositoryHas($quizStep); + + $duration = ($this->getStepDuration)('videoId'); + + self::assertSame(15.0, $duration); + } + + public function givenStepRepositoryHas(Step $step): void + { + $this->stepRepository + ->method('find') + ->willReturn($step); + } +}