1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File: ProductUrlProvider.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\ProductUrlProviderInterface; |
15
|
|
|
use Magento\Catalog\Api\Data\ProductInterface; |
16
|
|
|
use Magento\Catalog\Model\Product\Attribute\Source\Status; |
17
|
|
|
use Magento\Catalog\Model\Product\Visibility; |
18
|
|
|
use Magento\Catalog\Model\ResourceModel\Product\Collection; |
19
|
|
|
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory; |
20
|
|
|
use Magento\Framework\App\ResourceConnection; |
21
|
|
|
use Magento\Framework\App\ResourceConnectionFactory; |
22
|
|
|
use Magento\Framework\DB\Adapter\AdapterInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class ProductUrlProvider |
26
|
|
|
* @package LizardMedia\VarnishWarmer\Model\UrlProvider |
27
|
|
|
* @SuppressWarnings(PHPMD.LongVariable) |
28
|
|
|
*/ |
29
|
|
|
class ProductUrlProvider implements ProductUrlProviderInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var ProductCollectionFactory |
33
|
|
|
*/ |
34
|
|
|
protected $productCollectionFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ResourceConnectionFactory |
38
|
|
|
*/ |
39
|
|
|
protected $resourceConnectionFactory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* ProductUrlProvider constructor. |
43
|
|
|
* @param ProductCollectionFactory $productCollectionFactory |
44
|
|
|
* @param ResourceConnectionFactory $resourceConnectionFactory |
45
|
|
|
* @SuppressWarnings(PHPMD.LongVariable) |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
ProductCollectionFactory $productCollectionFactory, |
49
|
|
|
ResourceConnectionFactory $resourceConnectionFactory |
50
|
|
|
) { |
51
|
|
|
$this->productCollectionFactory = $productCollectionFactory; |
52
|
|
|
$this->resourceConnectionFactory = $resourceConnectionFactory; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param int $productId |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getProductUrls(int $productId): array |
60
|
|
|
{ |
61
|
|
|
/** @var ResourceConnection $connection */ |
62
|
|
|
$connection = $this->resourceConnectionFactory->create(); |
63
|
|
|
/** @var AdapterInterface $conn */ |
64
|
|
|
$conn = $connection->getConnection(); |
65
|
|
|
|
66
|
|
|
$select = $conn |
67
|
|
|
->select() |
68
|
|
|
->from( |
69
|
|
|
[ |
70
|
|
|
'u' => 'url_rewrite' |
71
|
|
|
], |
72
|
|
|
'request_path' |
73
|
|
|
)->where( |
74
|
|
|
'u.entity_type=?', |
75
|
|
|
'product' |
76
|
|
|
)->where( |
77
|
|
|
'u.entity_id=?', |
78
|
|
|
$productId |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return $conn->fetchAll($select); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getActiveProductsUrls(): array |
88
|
|
|
{ |
89
|
|
|
/** @var ResourceConnection $connection */ |
90
|
|
|
$connection = $this->resourceConnectionFactory->create(); |
91
|
|
|
/** @var AdapterInterface $conn */ |
92
|
|
|
$conn = $connection->getConnection(); |
93
|
|
|
|
94
|
|
|
$productIds = $this->getAvailableProductsIds(); |
95
|
|
|
$rewrites = []; |
96
|
|
|
if (!empty($productIds)) { |
97
|
|
|
$select = $conn |
98
|
|
|
->select() |
99
|
|
|
->from( |
100
|
|
|
[ |
101
|
|
|
'u' => 'url_rewrite' |
102
|
|
|
], |
103
|
|
|
'request_path' |
104
|
|
|
)->where( |
105
|
|
|
'u.entity_type=?', |
106
|
|
|
'product' |
107
|
|
|
)->where( |
108
|
|
|
'u.entity_id IN (' . implode(',', $productIds) . ')' |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$rewrites = $conn->fetchAll($select); |
112
|
|
|
} |
113
|
|
|
return $rewrites; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
protected function getAvailableProductsIds(): array |
120
|
|
|
{ |
121
|
|
|
/** @var Collection $productCollection */ |
122
|
|
|
$productCollection = $this->productCollectionFactory->create(); |
123
|
|
|
$productCollection->addFieldToFilter( |
124
|
|
|
ProductInterface::VISIBILITY, |
125
|
|
|
[ |
126
|
|
|
'in' => [ |
127
|
|
|
Visibility::VISIBILITY_IN_CATALOG, |
128
|
|
|
Visibility::VISIBILITY_IN_SEARCH, |
129
|
|
|
Visibility::VISIBILITY_BOTH |
130
|
|
|
] |
131
|
|
|
] |
132
|
|
|
); |
133
|
|
|
$productCollection->addFieldToFilter( |
134
|
|
|
ProductInterface::STATUS, |
135
|
|
|
Status::STATUS_ENABLED |
136
|
|
|
); |
137
|
|
|
return $productCollection->getAllIds(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|