Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
recentaddressesservice.php
1<?php
2
4
12
14{
16 protected static $instance;
17
18 private const MAX_CNT = 20;
19
20 private int $currentUserId;
21
22 protected function __construct(Container $config)
23 {
24 parent::__construct($config);
25
26 $this->currentUserId = (int)CurrentUser::get()?->getId();
27 }
28
29 public function add(Address $address): void
30 {
31 $normalizedAddress = $this->getNormalizedAddress($address);
32
33 $recentAddressList = RecentAddressTable::query()
34 ->setSelect(['ID', 'ADDRESS'])
35 ->where('USER_ID', $this->currentUserId)
36 ->setOrder(['USED_AT' => 'DESC'])
37 ->setLimit(self::MAX_CNT)
38 ->fetchAll()
39 ;
40
41 $isExisting = false;
42 foreach ($recentAddressList as $recentAddressListItem)
43 {
44 $recentAddress = null;
45 try
46 {
47 $recentAddress = Address::fromJson($recentAddressListItem['ADDRESS']);
48 }
49 catch (\Exception $e) {}
50
51 if ($recentAddress === null)
52 {
53 continue;
54 }
55
56 if ($this->areAddressesEqual($normalizedAddress, $recentAddress))
57 {
59 (int)$recentAddressListItem['ID'],
60 [
61 'ADDRESS' => $normalizedAddress->toJson(),
62 'USED_AT' => new DateTime(),
63 ]
64 );
65 $isExisting = true;
66
67 break;
68 }
69 }
70
71 if (!$isExisting)
72 {
74 'USER_ID' => $this->currentUserId,
75 'ADDRESS' => $normalizedAddress->toJson(),
76 ]);
77 }
78 }
79
80 public function get(int $limit = self::MAX_CNT): array
81 {
82 $result = [];
83
84 $recentAddressList = RecentAddressTable::query()
85 ->setSelect(['ADDRESS'])
86 ->where('USER_ID', $this->currentUserId)
87 ->setLimit(min($limit, self::MAX_CNT))
88 ->setOrder(['USED_AT' => 'DESC'])
89 ->fetchAll()
90 ;
91 foreach ($recentAddressList as $recentAddressListItem)
92 {
93 $recentAddressJson = $recentAddressListItem['ADDRESS'];
94
95 $address = Address::fromJson($recentAddressJson);
96 if (!$address)
97 {
98 continue;
99 }
100
101 $result[] = $address;
102 }
103
104 return $result;
105 }
106
107 public static function cleanUp(): string
108 {
110 DELETE FROM b_location_recent_address
111 WHERE
112 USED_AT < DATE_SUB(NOW(), INTERVAL 30 DAY)
113 ");
114
115 return '\\Bitrix\\Location\\Infrastructure\\Service\\RecentAddressesService::cleanUp();';
116 }
117
118 private function areAddressesEqual(Address $address1, Address $address2): bool
119 {
120 return $this->getAddressFields($address1) === $this->getAddressFields($address2);
121 }
122
123 private function getAddressFields(Address $address): array
124 {
125 $result = [];
126
128 foreach ($address->getFieldCollection() as $field)
129 {
130 $result[$field->getType()] = $field->getValue();
131 }
132
133 ksort($result);
134
135 return $result;
136 }
137
138 private function getNormalizedAddress(Address $address): Address
139 {
140 $result = new Address(
141 $address->getLanguageId()
142 );
143 $fieldCollection = new Address\FieldCollection();
144
145 $result->setLatitude($address->getLatitude());
146 $result->setLongitude($address->getLongitude());
147
149 foreach ($address->getFieldCollection() as $field)
150 {
151 $fieldCollection->addItem(
152 new Address\Field($field->getType(), $field->getValue())
153 );
154 }
155
156 $result->setFieldCollection($fieldCollection);
157
158 return $result;
159 }
160}
static fromJson(string $jsonData)
Definition address.php:287
static getConnection($name="")
static update($primary, array $data)