-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[php][step_shotgun_surgery-01_base] Add Shotgun Surgery example
- Loading branch information
1 parent
d5f2b62
commit 241ea85
Showing
14 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
examples/php/php-step_shotgun_surgery-01_base/src/Application/GetStepDuration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Application; | ||
|
||
use CodelyTv\StepShotgunSurgery\Domain\StepDurationCalculatorFactory; | ||
use CodelyTv\StepShotgunSurgery\Domain\StepRepository; | ||
|
||
class GetStepDuration | ||
{ | ||
private StepRepository $steps; | ||
|
||
public function __construct(StepRepository $steps) | ||
{ | ||
$this->steps = $steps; | ||
} | ||
|
||
public function __invoke(string $stepId): float | ||
{ | ||
$step = $this->steps->find($stepId); | ||
return StepDurationCalculatorFactory::build()->calculate($step); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
examples/php/php-step_shotgun_surgery-01_base/src/Domain/DurationMultiplier.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
class DurationMultiplier | ||
{ | ||
public static function multiplierFor(Step $step): float | ||
{ | ||
if ($step->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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/php/php-step_shotgun_surgery-01_base/src/Domain/Question.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
class Question | ||
{ | ||
} |
26 changes: 26 additions & 0 deletions
26
examples/php/php-step_shotgun_surgery-01_base/src/Domain/QuizStep.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
final class QuizStep extends Step | ||
{ | ||
private array $questions; | ||
|
||
public function __construct(string $id, Question ...$questions) | ||
{ | ||
parent::__construct($id); | ||
$this->questions = $questions; | ||
} | ||
|
||
public function type(): string | ||
{ | ||
return StepEnums::STEP_TYPE_QUIZ; | ||
} | ||
|
||
public function questionCount(): int | ||
{ | ||
return count($this->questions); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
examples/php/php-step_shotgun_surgery-01_base/src/Domain/Step.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
abstract class Step | ||
{ | ||
private string $id; | ||
|
||
public function __construct(string $id) | ||
{ | ||
$this->id = $id; | ||
} | ||
|
||
public function id(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
abstract public function type(): string; | ||
} |
12 changes: 12 additions & 0 deletions
12
examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
interface StepDurationCalculator | ||
{ | ||
public function supports(Step $step): bool; | ||
|
||
public function calculate(Step $step): float; | ||
} |
38 changes: 38 additions & 0 deletions
38
examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorChain.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
use RuntimeException; | ||
|
||
class StepDurationCalculatorChain implements StepDurationCalculator | ||
{ | ||
/** @var StepDurationCalculator[] */ | ||
private array $calculators; | ||
|
||
public function __construct(StepDurationCalculator ...$calculators) | ||
{ | ||
$this->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()}"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
class StepDurationCalculatorFactory | ||
{ | ||
public static function build(): StepDurationCalculator | ||
{ | ||
// Remember to add the calculator!! | ||
return new StepDurationCalculatorChain( | ||
new StepDurationCalculatorVideo(), | ||
new StepDurationCalculatorQuiz(), | ||
); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorQuiz.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
class StepDurationCalculatorQuiz implements StepDurationCalculator | ||
{ | ||
public function supports(Step $step): bool | ||
{ | ||
return $step instanceof QuizStep; | ||
} | ||
|
||
public function calculate(Step $step): float | ||
{ | ||
return $step->questionCount() * StepEnums::QUIZ_QUESTION_DURATION * DurationMultiplier::multiplierFor($step); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepDurationCalculatorVideo.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
class StepDurationCalculatorVideo implements StepDurationCalculator | ||
{ | ||
public function supports(Step $step): bool | ||
{ | ||
return $step instanceof VideoStep; | ||
} | ||
|
||
public function calculate(Step $step): float | ||
{ | ||
return $step->videoDuration() * DurationMultiplier::multiplierFor($step); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepEnums.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
final class StepEnums | ||
{ | ||
public const STEP_TYPE_VIDEO = 'video'; | ||
public const STEP_TYPE_QUIZ = 'quiz'; | ||
|
||
public const STEP_DURATION_MULTIPLIER_VIDEO = 1.1; | ||
public const STEP_DURATION_MULTIPLIER_QUIZ = 1.5; | ||
|
||
public const QUIZ_QUESTION_DURATION = 5; | ||
|
||
# Important: don't forget to add here the type!! | ||
public const STEP_TYPES = [ | ||
self::STEP_TYPE_VIDEO, | ||
self::STEP_TYPE_QUIZ | ||
]; | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/php/php-step_shotgun_surgery-01_base/src/Domain/StepRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
interface StepRepository | ||
{ | ||
public function find(string $id): Step; | ||
} |
26 changes: 26 additions & 0 deletions
26
examples/php/php-step_shotgun_surgery-01_base/src/Domain/VideoStep.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Domain; | ||
|
||
final class VideoStep extends Step | ||
{ | ||
private int $videoDuration; | ||
|
||
public function __construct(string $id, int $videoDuration) | ||
{ | ||
parent::__construct($id); | ||
$this->videoDuration = $videoDuration; | ||
} | ||
|
||
public function type(): string | ||
{ | ||
return StepEnums::STEP_TYPE_VIDEO; | ||
} | ||
|
||
public function videoDuration(): int | ||
{ | ||
return $this->videoDuration; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
examples/php/php-step_shotgun_surgery-01_base/tests/Application/GetStepDurationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace CodelyTv\StepShotgunSurgery\Tests\Application; | ||
|
||
use CodelyTv\StepShotgunSurgery\Application\GetStepDuration; | ||
use CodelyTv\StepShotgunSurgery\Domain\Question; | ||
use CodelyTv\StepShotgunSurgery\Domain\QuizStep; | ||
use CodelyTv\StepShotgunSurgery\Domain\Step; | ||
use CodelyTv\StepShotgunSurgery\Domain\StepRepository; | ||
use CodelyTv\StepShotgunSurgery\Domain\VideoStep; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class GetStepDurationTest extends TestCase | ||
{ | ||
private StepRepository $stepRepository; | ||
private GetStepDuration $getStepDuration; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->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); | ||
} | ||
} |