Skip to content

Commit 12ea6eb

Browse files
author
Dani Santamaría
committed
Add base code for Shotgun Surgery illustration
1 parent e372902 commit 12ea6eb

18 files changed

+1009
-342
lines changed

examples/php/php-step_shotgun_surgery-01_base/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
}
2828
],
2929
"require": {
30-
"php": "^7.2"
30+
"php": "^7.4"
3131
},
3232
"require-dev": {
3333
"jakub-onderka/php-parallel-lint": "^1.0",

examples/php/php-step_shotgun_surgery-01_base/composer.lock

Lines changed: 708 additions & 262 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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+
}

examples/php/php-step_shotgun_surgery-01_base/src/Codelyber.php

Lines changed: 0 additions & 29 deletions
This file was deleted.
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: 10 additions & 0 deletions
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+
class Question
8+
{
9+
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 videoDuration(): int
23+
{
24+
throw new Exception('Ques doesn\'t contain any video.');
25+
}
26+
27+
public function questionCount(): int
28+
{
29+
return count($this->questions);
30+
}
31+
}
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+
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 function type(): string;
22+
23+
abstract function videoDuration(): int;
24+
25+
abstract function questionCount(): int;
26+
}
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\StepShotgunSurgery\Domain;
6+
7+
class StepDurationCalculatorChain implements StepDurationCalculator
8+
{
9+
/** @var StepDurationCalculator[] */
10+
private array $calculators;
11+
12+
public function __construct(StepDurationCalculator ...$calculators)
13+
{
14+
$this->calculators = $calculators;
15+
}
16+
17+
public function supports(Step $step): bool
18+
{
19+
return in_array($step->type(), StepEnums::STEP_TYPES);
20+
}
21+
22+
public function calculate(Step $step): float
23+
{
24+
foreach ($this->calculators as $calculator) {
25+
if ($calculator->supports($step)) {
26+
return $calculator->calculate($step);
27+
}
28+
}
29+
30+
throw new RuntimeException("Missing calculator for step type {$step->type()}");
31+
}
32+
}
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+
}
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+
final class StepEnums
8+
{
9+
const STEP_TYPE_VIDEO = 'video';
10+
const STEP_TYPE_QUIZ = 'quiz';
11+
12+
const STEP_DURATION_MULTIPLIER_VIDEO = 1.1;
13+
const STEP_DURATION_MULTIPLIER_QUIZ = 1.5;
14+
15+
const QUIZ_QUESTION_DURATION = 5;
16+
17+
# Important: don't forget to add here the type!!
18+
const STEP_TYPES = [
19+
self::STEP_TYPE_VIDEO,
20+
self::STEP_TYPE_QUIZ
21+
];
22+
}
Lines changed: 10 additions & 0 deletions
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
function type(): string
18+
{
19+
return StepEnums::STEP_TYPE_VIDEO;
20+
}
21+
22+
function videoDuration(): int
23+
{
24+
return $this->videoDuration;
25+
}
26+
27+
function questionCount(): int
28+
{
29+
throw new Exception('Video doesn\'t contain any quiz questions.');
30+
}
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\StepRepository;
9+
use CodelyTv\StepShotgunSurgery\Domain\VideoStep;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class GetStepDurationTest extends TestCase
13+
{
14+
/** @test */
15+
public function shouldReturnVideoStepDuration()
16+
{
17+
$steps = $this->createMock(StepRepository::class);
18+
$steps
19+
->method('find')
20+
->willReturn(new VideoStep('videoId', 10));
21+
22+
$getStepDuration = new GetStepDuration($steps);
23+
24+
$duration = $getStepDuration->__invoke('videoId');
25+
26+
$this->assertSame(11.0, $duration);
27+
}
28+
}

0 commit comments

Comments
 (0)