Repository navigation
Expand file tree
/
Copy pathSynchronisationChecker.php
More file actions
122 lines (106 loc) · 3.86 KB
/
Copy pathSynchronisationChecker.php
File metadata and controls
122 lines (106 loc) · 3.86 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace Cloudinary\Cloudinary\Model;
use Cloudinary\Cloudinary\Api\SynchronisationRepositoryInterface;
use Cloudinary\Cloudinary\Core\AutoUploadMapping\AutoUploadConfigurationInterface;
use Cloudinary\Cloudinary\Core\ConfigurationInterface;
use Cloudinary\Cloudinary\Core\Image\SynchronizationCheck;
use Magento\Framework\Registry;
class SynchronisationChecker implements SynchronizationCheck
{
/**
* @var string
*/
private $imageNameCacheKey;
/**
* @var SynchronisationRepositoryInterface
*/
private $synchronisationRepository;
/**
* @var ConfigurationInterface
*/
private $configuration;
/**
* @var Configuration
*/
private $autoUploadConfiguration;
/**
* @var MediaLibraryMapFactory
*/
private $mediaLibraryMapFactory;
/**
* @var Registry
*/
private $coreRegistry;
/**
* @method __construct
* @param SynchronisationRepositoryInterface $synchronisationRepository
* @param ConfigurationInterface $configuration
* @param AutoUploadConfigurationInterface $autoUploadConfiguration
* @param MediaLibraryMapFactory $mediaLibraryMapFactory
* @param Registry $coreRegistry
*/
public function __construct(
SynchronisationRepositoryInterface $synchronisationRepository,
ConfigurationInterface $configuration,
AutoUploadConfigurationInterface $autoUploadConfiguration,
MediaLibraryMapFactory $mediaLibraryMapFactory,
Registry $coreRegistry
) {
$this->synchronisationRepository = $synchronisationRepository;
$this->configuration = $configuration;
$this->autoUploadConfiguration = $autoUploadConfiguration;
$this->mediaLibraryMapFactory = $mediaLibraryMapFactory;
$this->coreRegistry = $coreRegistry;
}
/**
* @method cacheResult
* @param bool $result
* @return mixed
*/
private function cacheResult($result)
{
$this->coreRegistry->unregister($this->imageNameCacheKey);
$this->coreRegistry->register($this->imageNameCacheKey, $result);
return $result;
}
/**
* @method cacheResult
* @return mixed
*/
private function getFromCache()
{
return $this->coreRegistry->registry($this->imageNameCacheKey);
}
/**
* @param string $imageName
* @param bool $refresh
* @return bool
*/
public function isSynchronized($imageName, $refresh = false)
{
if (!$imageName) {
return false;
}
// Auto-upload mapping makes any image URL resolvable on Cloudinary (lazy fetch),
// but only while image sync is enabled — with image sync off, images must fall
// through to the mapping/sync-table checks so unsynced ones are served locally.
if ($this->autoUploadConfiguration->isActive() && $this->configuration->isImageSyncEnabled()) {
return true;
}
$this->imageNameCacheKey = 'cldsynccheckcachekey_' . (string) $imageName;
if (!$refresh && ($cacheResult = $this->getFromCache()) !== null) {
return $cacheResult;
}
if ($this->configuration->isEnabledLocalMapping()) {
//Look for a match on the mapping table:
preg_match('/(cld_[A-Za-z0-9]{13}_).+$/i', $imageName, $cldUniqid);
if ($cldUniqid && isset($cldUniqid[1])) {
$mapped = $this->mediaLibraryMapFactory->create()->getCollection()->addFieldToFilter("cld_uniqid", $cldUniqid[1])->setPageSize(1)->getFirstItem();
if ($mapped && ($origPublicId = $mapped->getCldPublicId())) {
return $this->cacheResult(true);
}
}
}
return $this->cacheResult($this->synchronisationRepository->isSynchronizedImagePath($imageName));
}
}