Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
FacebookProductRepository.php
1<?php
2
4
10
12{
13 private const SERVICE_ID = 'facebook';
14
15 public function save(array $data)
16 {
17 $collection = $this->loadCollection(array_keys($data));
18 foreach ($collection as $exportedProduct)
19 {
20 $productId = $exportedProduct->getProductId();
21 $exportedProduct->setTimestampX(new DateTime());
22 $error = $data[$productId]['ERROR'] ?? null;
23 $exportedProduct->setError($error);
24 unset($data[$productId]);
25 }
26
27 foreach ($data as $productId => $productData)
28 {
29 $exportedProduct = new ExportedProduct();
30 $exportedProduct
31 ->setServiceId(self::SERVICE_ID)
32 ->setProductId($productId)
33 ->setError($productData['ERROR'] ?? null)
34 ;
35 $collection[] = $exportedProduct;
36 }
37
38 return $collection->save(true);
39 }
40
41 public function delete(array $ids): Result
42 {
43 $result = new Result();
44
45 $collection = $this->loadCollection($ids);
46 foreach ($collection as $exportedProduct)
47 {
48 $res = $exportedProduct->delete();
49 if (!$res->isSuccess())
50 {
51 $result->addErrors($res->getErrors());
52 }
53 }
54
55 return $result;
56 }
57
58 public function getProductsByIds(array $ids): array
59 {
60 $products = [];
61 $collection = $this->loadCollection($ids);
62 foreach ($collection as $exportedProduct)
63 {
64 $products[] = [
65 'ID' => $exportedProduct->getProductId(),
66 'ERROR' => $exportedProduct->getError(),
67 ];
68 }
69
70 return $products;
71 }
72
73 public function loadCollection(array $productIds): ExportedProductCollection
74 {
75 if (empty($productIds))
76 {
77 return ExportedProductTable::createCollection();
78 }
79
80 return
81 ExportedProductTable::getList([
82 'filter' => [
83 '=SERVICE_ID' => self::SERVICE_ID,
84 '@PRODUCT_ID' => $productIds,
85 ],
86 ])
87 ->fetchCollection()
88 ;
89 }
90}