Skip to content

Commit 241ea85

Browse files
committed
[php][step_shotgun_surgery-01_base] Add Shotgun Surgery example
1 parent d5f2b62 commit 241ea85

14 files changed

+321
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Application;
6+
7+
use CodelyTv\StepShotgunSurgery\Domain\StepDurationCalculatorFactory;
8+
use CodelyTv\StepShotgunSurgery\Domain\StepRepository;
9+
10+
class GetStepDuration
11+
{
12+
private StepRepository $steps;
13+
14+
public function __construct(StepRepository $steps)
15+
{
16+
$this->steps = $steps;
17+
}
18+
19+
public function __invoke(string $stepId): float
20+
{
21+
$step = $this->steps->find($stepId);
22+
return StepDurationCalculatorFactory::build()->calculate($step);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
class DurationMultiplier
8+
{
9+
public static function multiplierFor(Step $step): float
10+
{
11+
if ($step->type() === StepEnums::STEP_TYPE_VIDEO) {
12+
return StepEnums::STEP_DURATION_MULTIPLIER_VIDEO;
13+
}
14+
15+
if ($step->type() === StepEnums::STEP_TYPE_QUIZ) {
16+
return StepEnums::STEP_DURATION_MULTIPLIER_QUIZ;
17+
}
18+
19+
return 1.0;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
class Question
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
final class QuizStep extends Step
8+
{
9+
private array $questions;
10+
11+
public function __construct(string $id, Question ...$questions)
12+
{
13+
parent::__construct($id);
14+
$this->questions = $questions;
15+
}
16+
17+
public function type(): string
18+
{
19+
return StepEnums::STEP_TYPE_QUIZ;
20+
}
21+
22+
public function questionCount(): int
23+
{
24+
return count($this->questions);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
abstract class Step
8+
{
9+
private string $id;
10+
11+
public function __construct(string $id)
12+
{
13+
$this->id = $id;
14+
}
15+
16+
public function id(): string
17+
{
18+
return $this->id;
19+
}
20+
21+
abstract public function type(): string;
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
interface StepDurationCalculator
8+
{
9+
public function supports(Step $step): bool;
10+
11+
public function calculate(Step $step): float;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
use RuntimeException;
8+
9+
class StepDurationCalculatorChain implements StepDurationCalculator
10+
{
11+
/** @var StepDurationCalculator[] */
12+
private array $calculators;
13+
14+
public function __construct(StepDurationCalculator ...$calculators)
15+
{
16+
$this->calculators = $calculators;
17+
}
18+
19+
public function supports(Step $step): bool
20+
{
21+
return in_array($step->type(), StepEnums::STEP_TYPES);
22+
}
23+
24+
public function calculate(Step $step): float
25+
{
26+
if (!$this->supports($step)) {
27+
throw new RuntimeException("Missing calculator for step type {$step->type()}");
28+
}
29+
30+
foreach ($this->calculators as $calculator) {
31+
if ($calculator->supports($step)) {
32+
return $calculator->calculate($step);
33+
}
34+
}
35+
36+
throw new RuntimeException("Missing calculator for step type {$step->type()}");
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
class StepDurationCalculatorFactory
8+
{
9+
public static function build(): StepDurationCalculator
10+
{
11+
// Remember to add the calculator!!
12+
return new StepDurationCalculatorChain(
13+
new StepDurationCalculatorVideo(),
14+
new StepDurationCalculatorQuiz(),
15+
);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
class StepDurationCalculatorQuiz implements StepDurationCalculator
8+
{
9+
public function supports(Step $step): bool
10+
{
11+
return $step instanceof QuizStep;
12+
}
13+
14+
public function calculate(Step $step): float
15+
{
16+
return $step->questionCount() * StepEnums::QUIZ_QUESTION_DURATION * DurationMultiplier::multiplierFor($step);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
class StepDurationCalculatorVideo implements StepDurationCalculator
8+
{
9+
public function supports(Step $step): bool
10+
{
11+
return $step instanceof VideoStep;
12+
}
13+
14+
public function calculate(Step $step): float
15+
{
16+
return $step->videoDuration() * DurationMultiplier::multiplierFor($step);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
final class StepEnums
8+
{
9+
public const STEP_TYPE_VIDEO = 'video';
10+
public const STEP_TYPE_QUIZ = 'quiz';
11+
12+
public const STEP_DURATION_MULTIPLIER_VIDEO = 1.1;
13+
public const STEP_DURATION_MULTIPLIER_QUIZ = 1.5;
14+
15+
public const QUIZ_QUESTION_DURATION = 5;
16+
17+
# Important: don't forget to add here the type!!
18+
public const STEP_TYPES = [
19+
self::STEP_TYPE_VIDEO,
20+
self::STEP_TYPE_QUIZ
21+
];
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
interface StepRepository
8+
{
9+
public function find(string $id): Step;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
final class VideoStep extends Step
8+
{
9+
private int $videoDuration;
10+
11+
public function __construct(string $id, int $videoDuration)
12+
{
13+
parent::__construct($id);
14+
$this->videoDuration = $videoDuration;
15+
}
16+
17+
public function type(): string
18+
{
19+
return StepEnums::STEP_TYPE_VIDEO;
20+
}
21+
22+
public function videoDuration(): int
23+
{
24+
return $this->videoDuration;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Tests\Application;
6+
7+
use CodelyTv\StepShotgunSurgery\Application\GetStepDuration;
8+
use CodelyTv\StepShotgunSurgery\Domain\Question;
9+
use CodelyTv\StepShotgunSurgery\Domain\QuizStep;
10+
use CodelyTv\StepShotgunSurgery\Domain\Step;
11+
use CodelyTv\StepShotgunSurgery\Domain\StepRepository;
12+
use CodelyTv\StepShotgunSurgery\Domain\VideoStep;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class GetStepDurationTest extends TestCase
16+
{
17+
private StepRepository $stepRepository;
18+
private GetStepDuration $getStepDuration;
19+
20+
protected function setUp(): void
21+
{
22+
$this->stepRepository = $this->createMock(StepRepository::class);
23+
$this->getStepDuration = new GetStepDuration($this->stepRepository);
24+
}
25+
26+
/** @test */
27+
public function shouldReturnVideoStepDuration(): void
28+
{
29+
$videoStep = new VideoStep('videoId', 10);
30+
$this->givenStepRepositoryHas($videoStep);
31+
32+
$duration = ($this->getStepDuration)('videoId');
33+
34+
self::assertSame(11.0, $duration);
35+
}
36+
37+
/** @test */
38+
public function shouldReturnQuizStepDuration(): void
39+
{
40+
$questions = [
41+
new Question(),
42+
new Question(),
43+
];
44+
$quizStep = new QuizStep('videoId', ...$questions);
45+
$this->givenStepRepositoryHas($quizStep);
46+
47+
$duration = ($this->getStepDuration)('videoId');
48+
49+
self::assertSame(15.0, $duration);
50+
}
51+
52+
public function givenStepRepositoryHas(Step $step): void
53+
{
54+
$this->stepRepository
55+
->method('find')
56+
->willReturn($step);
57+
}
58+
}

0 commit comments

Comments
 (0)