Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find last #257

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Mooc/Videos/Application/Find/FindLastVideoQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace CodelyTv\Mooc\Videos\Application\Find;


use CodelyTv\Shared\Domain\Bus\Query\Query;

final class FindLastVideoQuery implements Query
{
public function __construct()
{
}


}
21 changes: 21 additions & 0 deletions src/Mooc/Videos/Application/Find/FindLastVideoQueryHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace CodelyTv\Mooc\Videos\Application\Find;


use CodelyTv\Mooc\Videos\Domain\Video;
use CodelyTv\Mooc\Videos\Domain\VideoRepository;

final class FindLastVideoQueryHandler
{
public function __construct(private VideoRepository $videoRepository)
{
}

public function __invoke(FindLastVideoQuery $findLastVideoQuery): ?Video
{
return $this->videoRepository->findLastVideo();
}
}
2 changes: 2 additions & 0 deletions src/Mooc/Videos/Domain/VideoRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public function save(Video $video): void;

public function search(VideoId $id): ?Video;

public function findLastVideo(): ?Video;

public function searchByCriteria(Criteria $criteria): Videos;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public function search(VideoId $id): ?Video
return $this->repository(Video::class)->find($id);
}

public function findLastVideo(): ?Video
{
return $this->repository(Video::class)->findOneBy([], ['id' => 'DESC']);
}


public function searchByCriteria(Criteria $criteria): Videos
{
$doctrineCriteria = DoctrineCriteriaConverter::convert($criteria, self::$criteriaToDoctrineFields);
Expand Down
45 changes: 45 additions & 0 deletions src/Mooc/Videos/Infrastructure/VideoRepositoryInMemory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace CodelyTv\Mooc\Videos\Infrastructure;


use CodelyTv\Mooc\Videos\Domain\Video;
use CodelyTv\Mooc\Videos\Domain\VideoId;
use CodelyTv\Mooc\Videos\Domain\VideoRepository;
use CodelyTv\Mooc\Videos\Domain\Videos;
use CodelyTv\Shared\Domain\Criteria\Criteria;

final class VideoRepositoryInMemory implements VideoRepository
{
private array $videos = [];

public function save(Video $video): void
{
$this->videos[] = $video;
}

public function search(VideoId $id): ?Video
{
foreach ($this->videos as $video) {
if ($video->id == $id) {
return $video;
}
}
return null;
}

public function findLastVideo(): ?Video
{
$lastVideo = max($this->videos);

return ($lastVideo) ? $lastVideo : null;
}

public function searchByCriteria(Criteria $criteria): Videos
{
// TODO Criteria
return new Videos($this->videos);
}
}
60 changes: 60 additions & 0 deletions tests/Mooc/Videos/Application/FindLastVideoQueryHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace CodelyTv\Tests\Mooc\Videos\Application;


use CodelyTv\Mooc\Shared\Domain\Courses\CourseId;
use CodelyTv\Mooc\Shared\Domain\Videos\VideoUrl;
use CodelyTv\Mooc\Videos\Application\Find\FindLastVideoQuery;
use CodelyTv\Mooc\Videos\Application\Find\FindLastVideoQueryHandler;
use CodelyTv\Mooc\Videos\Domain\Video;
use CodelyTv\Mooc\Videos\Domain\VideoId;
use CodelyTv\Mooc\Videos\Domain\VideoTitle;
use CodelyTv\Mooc\Videos\Domain\VideoType;
use CodelyTv\Mooc\Videos\Infrastructure\VideoRepositoryInMemory;
use CodelyTv\Tests\Shared\Domain\UuidMother;
use CodelyTv\Tests\Shared\Infrastructure\PhpUnit\UnitTestCase;
use Ramsey\Uuid\Uuid as RamseyUuid;

final class FindLastVideoQueryHandlerTest extends UnitTestCase
{

/** @test */
public function findLastVideo(): void
{
$videoRepositoryInMemory = new VideoRepositoryInMemory();
$videos = $this->createVideos($videoRepositoryInMemory);

$query = new FindLastVideoQuery();
$handler = new FindLastVideoQueryHandler($videoRepositoryInMemory);

$videoReturned = $handler->__invoke($query);

$this->assertEquals($videos[1]->id(), $videoReturned->id());
}

public function createVideos(VideoRepositoryInMemory $videoRepositoryInMemory): array
{
$videos[0] = Video::create(
new VideoId(RamseyUuid::uuid4()->toString()),
new VideoType(VideoType::SCREENCAST),
new VideoTitle('Video 1'),
new VideoUrl('http://www.videos.com/video-1'),
new CourseId(RamseyUuid::uuid4()->toString())
);
$videoRepositoryInMemory->save($videos[0]);

$videos[1] = Video::create(
new VideoId(RamseyUuid::uuid4()->toString()),
new VideoType(VideoType::SCREENCAST),
new VideoTitle('Video 2'),
new VideoUrl('http://www.videos.com/video-2'),
new CourseId(RamseyUuid::uuid4()->toString())
);
$videoRepositoryInMemory->save($videos[1]);

return $videos;
}
}