Skip to content

Commit 884284f

Browse files
author
Ulf Tietze
committed
Some Link Tag to Route stuff
1 parent 2c5894e commit 884284f

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Firegento\CacheWarmup\Model\RoutesTagsLink\Management;
4+
5+
use Magento\Framework\App\ResourceConnection;
6+
use Magento\Framework\DB\Adapter\Pdo\Mysql;
7+
8+
class LinkRouteToTags
9+
{
10+
/**
11+
* @var ResourceConnection
12+
*/
13+
protected $resourceConnection;
14+
15+
public function __construct(
16+
ResourceConnection $resourceConnection
17+
) {
18+
$this->resourceConnection = $resourceConnection;
19+
}
20+
21+
/**
22+
* @param int $routeId
23+
* @param int[] $tagIds
24+
* @throws \Zend_Db_Exception
25+
*/
26+
public function execute(int $routeId, array $tagIds): void
27+
{
28+
$routeToTags = [];
29+
array_walk($tagIds, function ($tagId) use ($routeId, $routeToTags) {
30+
$routeToTags[] = [
31+
'tag_id' => $tagId,
32+
'route_id' => $routeId
33+
];
34+
});
35+
36+
/** @var Mysql $connection */
37+
$connection = $this->resourceConnection->getConnection();
38+
$connection->insertArray(
39+
'cache_routes_tags',
40+
['route_id', 'tag_id'],
41+
$routeToTags,
42+
$connection::INSERT_IGNORE
43+
);
44+
}
45+
}

Model/RoutesTagsLinkManagement.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Firegento\CacheWarmup\Model;
4+
5+
6+
use Firegento\CacheWarmup\Api\Data\CacheRouteInterface;
7+
use Firegento\CacheWarmup\Api\Data\CacheTagInterface;
8+
use Firegento\CacheWarmup\Api\RoutesTagsLinkManagementInterface;
9+
use Firegento\CacheWarmup\Model\RoutesTagsLink\Management\LinkRouteToTags;
10+
11+
class RoutesTagsLinkManagement implements RoutesTagsLinkManagementInterface
12+
{
13+
/**
14+
* @var LinkRouteToTags
15+
*/
16+
protected $linkRouteToTags;
17+
18+
public function __construct(
19+
LinkRouteToTags $linkRouteToTags
20+
) {
21+
$this->linkRouteToTags = $linkRouteToTags;
22+
}
23+
24+
/**
25+
* Take care, that this only works if both (route & tags) exists
26+
*
27+
* @param CacheRouteInterface $cacheRoute
28+
* @param CacheTagInterface[] $tags
29+
*
30+
* @throws \Zend_Db_Exception
31+
*/
32+
public function linkRouteToTags(CacheRouteInterface $cacheRoute, array $tags): void
33+
{
34+
array_walk($tags, function (&$value) {return $value->getId();});
35+
$this->linkRouteToTags->execute($cacheRoute->getId(), $tags);
36+
}
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: utietze
5+
* Date: 10.02.19
6+
* Time: 14:40
7+
*/
8+
9+
namespace Firegento\CacheWarmup\Service\Route;
10+
11+
12+
use Firegento\CacheWarmup\Api\CacheRouteRepositoryInterface;
13+
use Firegento\CacheWarmup\Api\Data\CacheRouteInterface;
14+
15+
class GetExistingOrNewRouteModel
16+
{
17+
/**
18+
* @var CacheRouteRepositoryInterface
19+
*/
20+
protected $cacheRouteRepository;
21+
/**
22+
* @var NewRouteModelProvider
23+
*/
24+
protected $newRouteModelProvider;
25+
26+
public function __construct(
27+
CacheRouteRepositoryInterface $cacheRouteRepository,
28+
NewRouteModelProvider $newRouteModelProvider
29+
) {
30+
$this->cacheRouteRepository = $cacheRouteRepository;
31+
$this->newRouteModelProvider = $newRouteModelProvider;
32+
}
33+
34+
public function getByRoute(string $route): CacheRouteInterface
35+
{
36+
$routeModel = $this->cacheRouteRepository->getByRoute($route);
37+
38+
if (!$routeModel) {
39+
$routeModel = $this->newRouteModelProvider->createForRoute($route);
40+
$this->cacheRouteRepository->save($routeModel);
41+
$routeModel = $this->cacheRouteRepository->getByRoute($route);
42+
}
43+
44+
return $routeModel;
45+
}
46+
}

Service/Route/NewRouteModelProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(CacheRouteInterfaceFactory $cacheRouteInterfaceFacto
2525
$this->cacheRouteInterfaceFactory = $cacheRouteInterfaceFactory;
2626
}
2727

28-
public function createForRoute(string $route, int $cacheStatus = 0, int $lifetime = 86400, int $popularity): CacheRouteInterface
28+
public function createForRoute(string $route, int $cacheStatus = 0, int $lifetime = 86400, int $popularity = 0): CacheRouteInterface
2929
{
3030
/** @var CacheRouteInterface $freshCacheRoute */
3131
$freshCacheRoute = $this->cacheRouteInterfaceFactory->create();

etc/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
for="Firegento\CacheWarmup\Api\CacheTagManagementInterface"
2121
type="Firegento\CacheWarmup\Model\CacheTagManagement"
2222
/>
23+
<preference
24+
for="Firegento\CacheWarmup\Api\RoutesTagsLinkManagement"
25+
type="Firegento\CacheWarmup\Model\RoutesTagsLinkManagement"
26+
/>
2327
<preference
2428
for="Firegento\CacheWarmup\Api\CacheTagRepositoryInterface"
2529
type="Firegento\CacheWarmup\Model\CacheTagRepository"

0 commit comments

Comments
 (0)