Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
queueeventhandler.php
1<?php
2
4
15
17{
18
20 private $errorCollection;
21
23 private $service;
24
26 private $clientId;
27
29 private $engineCode;
30
32 private $type;
33
34
35 private static function getInstance() : self
36 {
37 return new self;
38 }
39
40 private function __construct()
41 {
42 $this->errorCollection = new ErrorCollection();
43 }
44
45 private function setClientId($clientId) : self
46 {
47 if(!$this->hasErrors())
48 {
49 if(isset($clientId,$this->service,$this->type) && is_int($clientId))
50 {
51 $authAdapter = $this->service::getAuthAdapter($this->type);
52 if($this->service instanceof IMultiClientService && $authAdapter->canUseMultipleClients())
53 {
54 $this->service->setClientId($clientId);
55 }
56 if($authAdapter->hasAuth())
57 {
58 $this->clientId = $clientId;
59 return $this;
60 }
61 }
62 $this->errorCollection->setError(new Main\Error("client has no auth"));
63 }
64 return $this;
65 }
66 private function setEngineCode($code) : self
67 {
68 if(!$this->hasErrors())
69 {
70 try
71 {
72 if(is_string($code) && $this->service = ServiceFactory::getServiceByEngineCode($code))
73 {
74 if($this->service instanceof IInternalService && $this->service::canUseAsInternal())
75 {
76 $this->engineCode = $code;
77 return $this->setType($this->service::getTypeByEngine($this->engineCode));
78 }
79 }
80 }
81 catch (\Throwable $exception)
82 {
83 $this->errorCollection->setError(new Main\Error($exception->getMessage()));
84 }
85 }
86 return $this;
87 }
88 private function setType($type) : self
89 {
90 if(!$this->hasErrors())
91 {
92 if(isset($type) && in_array($type,$this->service::getTypes()) && in_array($type,Service::getTypes()))
93 {
94 $this->type = $type;
95 return $this;
96 }
97 $this->errorCollection->setError(new Main\Error("service not support type"));
98 }
99 return $this;
100 }
101 private function run() : self
102 {
103 if(!$this->hasErrors())
104 {
105 $result = Internals\ServiceQueueTable::add([
106 'CLIENT_ID' => $this->clientId,
107 'SERVICE_TYPE' => $this->engineCode,
108 'TYPE' => $this->type
109 ]);
110 $result->isSuccess()?:$this->errorCollection->add($result->getErrors());
111 }
112 return $this;
113 }
114 private function clearCache()
115 {
116 if(!$this->hasErrors())
117 {
118 Facebook\Config::clearCache();
119 Facebook\Installs::clearCache();
120 }
121 return $this;
122 }
123 private function sendEvent() : self
124 {
125 if(!$this->hasErrors())
126 {
127 Main\EventManager::getInstance()->send(
128 $event = new Event('seo','onExtensionInstall',[])
129 );
130 foreach ($event->getResults() as $result)
131 {
132 if($result->getType() === EventResult::ERROR)
133 {
134 $this->errors()->add($errors = $result->getParameters()['ERROR_COLLECTION'] ?? []);
135 }
136 }
137 }
138 return $this;
139 }
140 private function hasErrors() : bool
141 {
142 return $this->errorCollection->count() > 0;
143 }
144 private function getEventStatus() : int
145 {
146 return $this->hasErrors() ? EventResult::ERROR : EventResult::SUCCESS;
147 }
148 private function errors() : ErrorCollection
149 {
150 return $this->errorCollection;
151 }
152 private function result() : EventResult
153 {
154 return new EventResult($this->getEventStatus(), ['ERROR_COLLECTION' => $this->errors()]);
155 }
156
157 public static function handleEvent($clientId,$engineCode)
158 {
159 return
160 static::getInstance()
161 ->setEngineCode($engineCode)
162 ->setClientId($clientId)
163 ->run()
164 ->clearCache()
165 ->sendEvent();
166 }
167}
static getServiceByEngineCode(string $engineCode)