Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
vkexporteddata.php
1<?php
2
4
8
9Loc::loadMessages(__FILE__);
10
19{
20
21 const CACHE_DIR = '/sale/vkexport/';
22 const CACHE_TTL = 3600; // one hour, for update during long export
23 const CACHE_ID_PREFIX = "vkexporteddata_cache";
24 private $exportId;
25 private $type;
26 private $cacheId;
27
33 public function __construct($exportId, $type)
34 {
35 $this->exportId = intval($exportId);
36
37 if (in_array($type, array('PRODUCTS', 'ALBUMS')))
38 $this->type = $type;
39 else
40 throw new ArgumentNullException("EXPORT_ID");
41
42 $this->cacheId = $this->getCacheId();
43 }
44
45
51 public function getData()
52 {
53 $cacheManager = Application::getInstance()->getManagedCache();
54 $result = NULL;
55
56 if ($cacheManager->read(self::CACHE_TTL, $this->cacheId))
57 {
58 $result = $cacheManager->get($this->cacheId);
59 }
60 else
61 {
62 $result = $this->getDataFromVk();
63
64 $cacheManager->set($this->cacheId, $result);
65 }
66
67 return $result;
68 }
69
70
75 public function removeData()
76 {
77 $cacheManager = Application::getInstance()->getManagedCache();
78 $cacheManager->clean($this->cacheId);
79 }
80
81
88 public function addData($newData)
89 {
90 $cacheManager = Application::getInstance()->getManagedCache();
91
92// get saved data from cache, if exist...
93 if ($cacheManager->read(self::CACHE_TTL, $this->cacheId))
94 $savedData = $cacheManager->get($this->cacheId);
95
96// ...or from VK
97 else
98 $savedData = $this->getDataFromVk();
99
100// add new data to existing
101 $dataToSave = $savedData + $newData;
102 $cacheManager->clean($this->cacheId);
103 $cacheManager->read(self::CACHE_TTL, $this->cacheId);
104 $cacheManager->set($this->cacheId, $dataToSave);
105 }
106
107
113 private function getCacheId()
114 {
115 return self::CACHE_ID_PREFIX . '_' . $this->exportId . '_' . $this->type;
116 }
117
118
124 private function getDataFromVk()
125 {
126 $apiHelper = new Api\ApiHelper($this->exportId);
127 $data = false;
128
129 switch ($this->type)
130 {
131 case 'ALBUMS':
132 $data = $apiHelper->getALbumsFromVk($this->getVkGroupId());
133 break;
134
135 case 'PRODUCTS':
136 $data = $apiHelper->getProductsFromVk($this->getVkGroupId());
137 break;
138
139 default:
140 break;
141 }
142
143 return $data;
144 }
145
151 private function getVkGroupId()
152 {
153 $vk = Vk::getInstance();
154
155 return $vk->getGroupId($this->exportId);
156 }
157}
static loadMessages($file)
Definition loc.php:64