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

First practice with adapter and ports #272

Open
wants to merge 1 commit 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
6 changes: 6 additions & 0 deletions apps/backoffice/backend/config/routes/courses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ courses_get:
controller: CodelyTv\Apps\Backoffice\Backend\Controller\Courses\CoursesGetController
defaults: { auth: false }
methods: [GET]

course_last:
path: /last-course
controller: CodelyTv\Apps\Backoffice\Backend\Controller\Courses\LastVideoController
defaults: { auth: false }
methods: [GET]
1 change: 1 addition & 0 deletions apps/backoffice/backend/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ services:
# -- IMPLEMENTATIONS SELECTOR --
CodelyTv\Shared\Domain\Bus\Event\EventBus: '@CodelyTv\Shared\Infrastructure\Bus\Event\WithMonitoring\WithPrometheusMonitoringEventBus'
CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository: '@CodelyTv\Backoffice\Courses\Infrastructure\Persistence\ElasticsearchBackofficeCourseRepository'
CodelyTv\Shared\Domain\Bus\Query\QueryHandler: '@CodelyTv\Backoffice\Courses\Application\LastVideo\SearchLastBackofficeCoursesQueryHandler'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace CodelyTv\Apps\Backoffice\Backend\Controller\Courses;

use CodelyTv\Shared\Domain\Bus\Query\QueryHandler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class LastVideoController
{
public function __construct(private QueryHandler $queryBus)
{
}

public function __invoke(Request $request): JsonResponse
{
return new JsonResponse(
$this->queryBus->__invoke()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just interesting. Why not $this->queryBus() ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both of them it's right since in my current job using the __invoke to be more explicit that is the reason that I used here, but i think it's ok use as function, maybe i don't have any particular reason, only by the definition on my teamwork. 😄

);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace CodelyTv\Backoffice\Courses\Application\LastVideo;

use CodelyTv\Backoffice\Courses\Application\BackofficeCourseResponse;
use CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository;

class LastBackofficeCourseSearcher
{
public function __construct(private BackofficeCourseRepository $repository)
{
}

public function __invoke(): ?BackofficeCourseResponse
{
$lastCourse = $this->repository->lastCourse();

if (!$lastCourse) {
return null;
}

return new BackofficeCourseResponse(
$lastCourse->id(),
$lastCourse->name(),
$lastCourse->duration()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace CodelyTv\Backoffice\Courses\Application\LastVideo;

use CodelyTv\Backoffice\Courses\Application\BackofficeCoursesResponse;
use CodelyTv\Shared\Domain\Bus\Query\QueryHandler;

final class SearchLastBackofficeCoursesQueryHandler implements QueryHandler
{
public function __construct(private LastBackofficeCourseSearcher $searcher)
{
}

public function __invoke(): ?BackofficeCoursesResponse
{
return $this->searcher->__invoke();
}
}
3 changes: 3 additions & 0 deletions src/Backoffice/Courses/Domain/BackofficeCourseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CodelyTv\Backoffice\Courses\Domain;

use CodelyTv\Mooc\Courses\Domain\Course;
use CodelyTv\Shared\Domain\Criteria\Criteria;

interface BackofficeCourseRepository
Expand All @@ -13,4 +14,6 @@ public function save(BackofficeCourse $course): void;
public function searchAll(): array;

public function matching(Criteria $criteria): array;

public function lastCourse(): ?Course;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CodelyTv\Backoffice\Courses\Domain\BackofficeCourse;
use CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository;
use CodelyTv\Mooc\Courses\Domain\Course;
use CodelyTv\Shared\Domain\Criteria\Criteria;
use CodelyTv\Shared\Infrastructure\Persistence\Elasticsearch\ElasticsearchRepository;
use function Lambdish\Phunctional\map;
Expand All @@ -27,6 +28,12 @@ public function matching(Criteria $criteria): array
return map($this->toCourse(), $this->searchByCriteria($criteria));
}

public function lastCourse(): ?Course
{
$courseInPrimitive = $this->searchlastInelastic();
return $courseInPrimitive ? BackofficeCourse::fromPrimitives($courseInPrimitive) : null;
}

protected function aggregateName(): string
{
return 'courses';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CodelyTv\Backoffice\Courses\Domain\BackofficeCourse;
use CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository;
use CodelyTv\Mooc\Courses\Domain\Course;
use CodelyTv\Shared\Domain\Criteria\Criteria;
use function Lambdish\Phunctional\get;

Expand Down Expand Up @@ -33,6 +34,14 @@ public function matching(Criteria $criteria): array
return get($criteria->serialize(), self::$matchingCache) ?: $this->searchMatchingAndFillCache($criteria);
}

public function lastCourse(): Course
{
$lastCourse = end(self::$allCoursesCache);
reset(self::$allCoursesCache);

return $lastCourse;
}

private function searchAllAndFillCache(): array
{
return self::$allCoursesCache = $this->repository->searchAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CodelyTv\Backoffice\Courses\Domain\BackofficeCourse;
use CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository;
use CodelyTv\Mooc\Courses\Domain\Course;
use CodelyTv\Shared\Domain\Criteria\Criteria;
use CodelyTv\Shared\Infrastructure\Persistence\Doctrine\DoctrineCriteriaConverter;
use CodelyTv\Shared\Infrastructure\Persistence\Doctrine\DoctrineRepository;
Expand All @@ -28,4 +29,10 @@ public function matching(Criteria $criteria): array

return $this->repository(BackofficeCourse::class)->matching($doctrineCriteria)->toArray();
}

public function lastCourse(): ?Course
{
return $this->repository(BackofficeCourse::class)
->findOneBy([], ['id' => 'DESC']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ protected function persist(string $id, array $plainBody): void
$this->client->persist($this->aggregateName(), $id, $plainBody);
}

protected function searchAllInElastic(): array
protected function searchlastInelastic(): array
{
return $this->searchRawElasticsearchQuery([]);
$courses = $this->searchRawElasticsearchQuery([
'body' => [
'size' => 1
]
]);

return $courses ? end($courses): [];
}

protected function searchRawElasticsearchQuery(array $params): array
Expand Down