Skip to content

Commit 2dd7546

Browse files
author
Ulf Tietze
committed
Some TagRepository Stuff
1 parent d9da0e2 commit 2dd7546

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

Api/CacheTagRepositoryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ interface CacheTagRepositoryInterface
99
public function getById(int $id):? CacheTagInterface;
1010

1111
public function getByTag(string $tag):? CacheTagInterface;
12+
13+
public function save(CacheTagInterface $cacheTag): void;
1214
}

Model/CacheTagRepository.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Firegento\CacheWarmup\Api\CacheTagRepositoryInterface;
66
use Firegento\CacheWarmup\Api\Data\CacheTagInterface;
77
use Firegento\CacheWarmup\Api\Data\CacheTagInterfaceFactory;
8+
use Firegento\CacheWarmup\Model\Route\Query\SaveTag;
89
use Firegento\CacheWarmup\Model\Tag\Query\GetById;
910
use Firegento\CacheWarmup\Model\Tag\Query\GetByTag;
1011

@@ -19,13 +20,19 @@ class CacheTagRepository implements CacheTagRepositoryInterface
1920
* @var GetByTag
2021
*/
2122
protected $getByTag;
23+
/**
24+
* @var SaveTag
25+
*/
26+
protected $saveCommand;
2227

2328
public function __construct(
2429
GetById $getById,
25-
GetByTag $getByTag
30+
GetByTag $getByTag,
31+
SaveTag $saveCommand
2632
) {
2733
$this->getById = $getById;
2834
$this->getByTag = $getByTag;
35+
$this->saveCommand = $saveCommand;
2936
}
3037

3138
public function getById(int $id):? CacheTagInterface
@@ -37,4 +44,9 @@ public function getByTag(string $cacheTag):? CacheTagInterface
3744
{
3845
return $this->getByTag->execute($cacheTag);
3946
}
47+
48+
public function save(CacheTagInterface $cacheTag): void
49+
{
50+
$this->saveCommand->execute($cacheTag);
51+
}
4052
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Firegento\CacheWarmup\Service\Tag;
4+
5+
6+
use Firegento\CacheWarmup\Api\CacheTagRepositoryInterface;
7+
use Firegento\CacheWarmup\Api\Data\CacheTagInterface;
8+
9+
class GetExistingOrNewTagModel
10+
{
11+
/**
12+
* @var CacheTagRepositoryInterface
13+
*/
14+
protected $cacheTagRepository;
15+
/**
16+
* @var NewTagModelProvider
17+
*/
18+
protected $newTagModelProvider;
19+
20+
public function __construct(
21+
CacheTagRepositoryInterface $cacheTagRepository,
22+
NewTagModelProvider $newTagModelProvider
23+
) {
24+
$this->cacheTagRepository = $cacheTagRepository;
25+
$this->newTagModelProvider = $newTagModelProvider;
26+
}
27+
28+
public function getByTag(string $cacheTag): CacheTagInterface
29+
{
30+
$cacheTag = $this->cacheTagRepository->getByTag($cacheTag);
31+
32+
if (!$cacheTag) {
33+
$cacheTag = $this->newTagModelProvider->createForTag($cacheTag);
34+
$this->cacheTagRepository->save($cacheTag);
35+
$cacheTag = $this->cacheTagRepository->getByTag($cacheTag);
36+
}
37+
38+
return $cacheTag;
39+
}
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Firegento\CacheWarmup\Service\Tag;
4+
5+
6+
use Firegento\CacheWarmup\Api\Data\CacheTagInterface;
7+
use Firegento\CacheWarmup\Api\Data\CacheTagInterfaceFactory;
8+
9+
class NewTagModelProvider
10+
{
11+
/**
12+
* @var CacheTagInterfaceFactory
13+
*/
14+
protected $cacheTagInterfaceFactory;
15+
16+
public function __construct(CacheTagInterfaceFactory $cacheTagInterfaceFactory)
17+
{
18+
$this->cacheTagInterfaceFactory = $cacheTagInterfaceFactory;
19+
}
20+
21+
public function createForTag(string $cacheTag): CacheTagInterface
22+
{
23+
/** @var CacheTagInterface $tagModel */
24+
$tagModel = $this->cacheTagInterfaceFactory->create();
25+
$tagModel->setCacheTag($cacheTag);
26+
27+
return $tagModel;
28+
}
29+
}

0 commit comments

Comments
 (0)