-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathInMemoryCacheBackofficeCourseRepository.php
More file actions
54 lines (42 loc) · 1.58 KB
/
InMemoryCacheBackofficeCourseRepository.php
File metadata and controls
54 lines (42 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
declare(strict_types=1);
namespace CodelyTv\Backoffice\Courses\Infrastructure\Persistence;
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;
final class InMemoryCacheBackofficeCourseRepository implements BackofficeCourseRepository
{
private static array $allCoursesCache = [];
private static array $matchingCache = [];
public function __construct(private BackofficeCourseRepository $repository)
{
}
public function save(BackofficeCourse $course): void
{
$this->repository->save($course);
}
public function searchAll(): array
{
return empty(self::$allCoursesCache) ? $this->searchAllAndFillCache() : self::$allCoursesCache;
}
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();
}
private function searchMatchingAndFillCache(Criteria $criteria): array
{
return self::$matchingCache[$criteria->serialize()] = $this->repository->matching($criteria);
}
}