|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* File: CategoryUrlProvider.php |
|
7
|
|
|
* |
|
8
|
|
|
* @author Maciej Sławik <[email protected]> |
|
9
|
|
|
* @copyright Copyright (C) 2019 Lizard Media (http://lizardmedia.pl) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace LizardMedia\VarnishWarmer\Model\UrlProvider; |
|
13
|
|
|
|
|
14
|
|
|
use LizardMedia\VarnishWarmer\Api\UrlProvider\CategoryUrlProviderInterface; |
|
15
|
|
|
use Magento\Catalog\Model\ResourceModel\Category\Collection; |
|
16
|
|
|
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory; |
|
17
|
|
|
use Magento\Framework\App\ResourceConnection; |
|
18
|
|
|
use Magento\Framework\App\ResourceConnectionFactory; |
|
19
|
|
|
use Magento\Framework\DB\Adapter\AdapterInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class CategoryUrlProvider |
|
23
|
|
|
* @package LizardMedia\VarnishWarmer\Model\UrlProvider |
|
24
|
|
|
* @SuppressWarnings(PHPMD.LongVariable) |
|
25
|
|
|
*/ |
|
26
|
|
|
class CategoryUrlProvider implements CategoryUrlProviderInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var CategoryCollectionFactory |
|
30
|
|
|
*/ |
|
31
|
|
|
private $categoryCollectionFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var ResourceConnectionFactory |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $resourceConnectionFactory; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* CategoryUrlProvider constructor. |
|
40
|
|
|
* @param CategoryCollectionFactory $categoryCollectionFactory |
|
41
|
|
|
* @param ResourceConnectionFactory $resourceConnectionFactory |
|
42
|
|
|
* @SuppressWarnings(PHPMD.LongVariable) |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
CategoryCollectionFactory $categoryCollectionFactory, |
|
46
|
|
|
ResourceConnectionFactory $resourceConnectionFactory |
|
47
|
|
|
) { |
|
48
|
|
|
$this->categoryCollectionFactory = $categoryCollectionFactory; |
|
49
|
|
|
$this->resourceConnectionFactory = $resourceConnectionFactory; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getActiveCategoriesUrls(): array |
|
56
|
|
|
{ |
|
57
|
|
|
/** @var ResourceConnection $connection */ |
|
58
|
|
|
$connection = $this->resourceConnectionFactory->create(); |
|
59
|
|
|
/** @var AdapterInterface $conn */ |
|
60
|
|
|
$conn = $connection->getConnection(); |
|
61
|
|
|
|
|
62
|
|
|
$categoryIds = $this->getAvailableCategoriesIds(); |
|
63
|
|
|
$select = $conn |
|
64
|
|
|
->select() |
|
65
|
|
|
->from( |
|
66
|
|
|
[ |
|
67
|
|
|
'u' => 'url_rewrite' |
|
68
|
|
|
], |
|
69
|
|
|
'request_path' |
|
70
|
|
|
)->where( |
|
71
|
|
|
'u.entity_type=?', |
|
72
|
|
|
'category' |
|
73
|
|
|
)->where( |
|
74
|
|
|
'u.entity_id IN (' . implode(',', $categoryIds) . ')' |
|
75
|
|
|
); |
|
76
|
|
|
return $conn->fetchAll($select); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getAvailableCategoriesIds(): array |
|
83
|
|
|
{ |
|
84
|
|
|
/** @var Collection $categoryCollection */ |
|
85
|
|
|
$categoryCollection = $this->categoryCollectionFactory->create(); |
|
86
|
|
|
$categoryCollection->addFieldToFilter( |
|
87
|
|
|
'is_active', |
|
88
|
|
|
1 |
|
89
|
|
|
); |
|
90
|
|
|
return $categoryCollection->getAllIds(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|