Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
connection.php
1<?php
2
4
5use Bitrix\Calendar\Core;
7use Bitrix\Dav\Internals\DavConnectionTable;
8use Bitrix\Dav\Internals\EO_DavConnection;
17use Exception;
18
22class Connection extends Mapper implements BaseMapperInterface
23{
27 public function patch(Sync\Connection\Connection $connection, array $fields): Sync\Connection\Connection
28 {
29 Loader::includeModule('dav');
30 DavConnectionTable::update($connection->getId(), $fields);
31
32 return $connection;
33 }
34
42 public function getByName(string $name): ?Sync\Connection\Connection
43 {
44 return $this->getOneEntityByFilter(['=NAME' => $name]);
45 }
46
56 protected function getOneEntityByFilter(array $filter): ?object
57 {
58 if (!Loader::includeModule('dav'))
59 {
60 return null;
61 }
62
63 $connection = DavConnectionTable::query()
64 ->setFilter($filter)
65 ->setSelect(['*'])
66 ->exec()
67 ->fetchObject()
68 ;
69
70 if ($connection)
71 {
72 return $this->convertToObject($connection);
73 }
74
75 return null;
76 }
77
83 protected function convertToObject($objectEO): Core\Base\EntityInterface
84 {
85 return (new Sync\Builders\BuilderConnectionFromDM($objectEO))->build();
86 }
87
91 protected function getEntityName(): string
92 {
93 return 'Dav connection';
94 }
95
104 protected function createEntity($entity, array $params = []): ?Core\Base\EntityInterface
105 {
106 Loader::includeModule('dav');
107 $data = $this->convertToArray($entity);
108
109 $result = DavConnectionTable::add($data);
110 if ($result->isSuccess())
111 {
112 return $entity->setId($result->getId());
113 }
114
115 throw new Core\Base\BaseException('Error of create Dav connection', 400);
116 }
117
126 protected function updateEntity($entity, array $params = []): ?Core\Base\EntityInterface
127 {
128 Loader::includeModule('dav');
129 $data = $this->convertToArray($entity);
130 unset($data['CREATED']);
131
132 $result = DavConnectionTable::update($entity->getId(), $data);
133 if ($result->isSuccess())
134 {
135 return $entity;
136 }
137
138 throw new Core\Base\BaseException('Error of update Dav connection', 400);
139 }
140
141 private function convertToArray($connection): array
142 {
143 return [
144 'SYNC_TOKEN' => $connection->getToken(),
145 'NAME' => $connection->getName(),
146 'ENTITY_TYPE' => $connection->getOwner()->getType(),
147 'ENTITY_ID' => $connection->getOwner()->getId(),
148 'SERVER_SCHEME' => $connection->getServer()->getScheme(),
149 'SERVER_HOST' => $connection->getServer()->getHost(),
150 'SERVER_PORT' => $connection->getServer()->getPort(),
151 'SERVER_USERNAME' => $connection->getServer()->getUserName(),
152 'SERVER_PASSWORD' => $connection->getServer()->getPassword(),
153 'SERVER_PATH' => $connection->getServer()->getBasePath(),
154 'LAST_RESULT' => $connection->getStatus(),
155 'IS_DELETED' => $connection->isDeleted() ? 'Y' : 'N',
156 'SYNCHRONIZED' => ($lastSyncTime = $connection->getLastSyncTime())
157 ? $lastSyncTime->getDate()
158 : new DateTime()
159 ,
160 'CREATED' => new DateTime(),
161 'MODIFIED' => new DateTime(),
162 'ACCOUNT_TYPE' => $connection->getAccountType(),
163 'NEXT_SYNC_TRY' => ($nextSyncTry = $connection->getNextSyncTry())
164 ? $nextSyncTry->getDate()
165 : new DateTime(),
166 ];
167 }
168
172 protected function getMapClass(): string
173 {
174 return Sync\Connection\ConnectionMap::class;
175 }
176
184 protected function deleteEntity(
185 Core\Base\EntityInterface $entity,
186 array $params = ['softDelete' => true]
187 ): ?Core\Base\EntityInterface
188 {
189 if (!Loader::includeModule('dav'))
190 {
191 return null;
192 }
193
194 if (!empty($params['softDelete']))
195 {
196 $entity->setIsActive(true);
197
198 return $this->updateEntity($entity, $params);
199 }
200
201 // TODO: change it to SectionTable::delete() after implementation all logic
202 $result = DavConnectionTable::delete($entity->getId());
203 if ($result->isSuccess())
204 {
205 $entity->setDeleted(true);
206
207 return null;
208 }
209
210 throw new Core\Base\BaseException('Error of delete Dav connection');
211 }
212
223 protected function getDataManagerResult(array $params): Result
224 {
225 Loader::includeModule('dav');
226
227 return DavConnectionTable::getList($params);
228 }
229
233 protected function getEntityClass(): string
234 {
235 return Sync\Connection\Connection::class;
236 }
237}
updateEntity($entity, array $params=[])
patch(Sync\Connection\Connection $connection, array $fields)
deleteEntity(Core\Base\EntityInterface $entity, array $params=['softDelete'=> true])
createEntity($entity, array $params=[])