Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 241ea85

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

File tree

14 files changed

+321
-0
lines changed

14 files changed

+321
-0
lines changed
Lines changed: 24 additions & 0 deletions
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+
}
Lines changed: 21 additions & 0 deletions
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+
}
Lines changed: 9 additions & 0 deletions
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+
}
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\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+
}
Lines changed: 22 additions & 0 deletions
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+
}
Lines changed: 12 additions & 0 deletions
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+
}
Lines changed: 38 additions & 0 deletions
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+
}
Lines changed: 17 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /