From 670df99edaae45b9dbd8e0c5528ed549d5e722fc Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 16 Mar 2022 12:04:44 +0200 Subject: [PATCH] type fixes --- application/Espo/Core/WebSocket/Pusher.php | 23 ++++++++++--- .../Espo/Core/WebSocket/SenderFactory.php | 2 +- .../Espo/Core/WebSocket/Submission.php | 3 +- .../Espo/Core/WebSocket/SubscriberFactory.php | 2 +- application/Espo/Core/Webhook/Manager.php | 34 +++++++++++-------- application/Espo/Core/Webhook/Sender.php | 24 +++++++++---- application/Espo/Entities/Webhook.php | 15 ++++++++ 7 files changed, 75 insertions(+), 28 deletions(-) diff --git a/application/Espo/Core/WebSocket/Pusher.php b/application/Espo/Core/WebSocket/Pusher.php index 6abf4f1aee..364c0f2cc1 100644 --- a/application/Espo/Core/WebSocket/Pusher.php +++ b/application/Espo/Core/WebSocket/Pusher.php @@ -75,10 +75,7 @@ class Pusher implements WampServerInterface */ protected $topicHash = []; - /** - * @var ?string - */ - private $phpExecutablePath; + private string $phpExecutablePath; /** * @param array> $categoriesData @@ -91,7 +88,19 @@ class Pusher implements WampServerInterface $this->categoryList = array_keys($categoriesData); $this->categoriesData = $categoriesData; - $this->phpExecutablePath = $phpExecutablePath ?: (new PhpExecutableFinder)->find(); + if (!$phpExecutablePath) { + $phpExecutablePath = (new PhpExecutableFinder)->find() ?: null; + } + + if (!$phpExecutablePath) { + if ($isDebugMode) { + $this->log("Error: No php-executable-path."); + } + + throw new Exception("No php-executable-path."); + } + + $this->phpExecutablePath = $phpExecutablePath; $this->isDebugMode = $isDebugMode; } @@ -111,6 +120,7 @@ class Pusher implements WampServerInterface return; } + /** @var string */ /** @phpstan-ignore-next-line */ $connectionId = $connection->resourceId; @@ -169,6 +179,7 @@ class Pusher implements WampServerInterface return; } + /** @var string */ /** @phpstan-ignore-next-line */ $connectionId = $connection->resourceId; @@ -230,6 +241,7 @@ class Pusher implements WampServerInterface if (array_key_exists('paramList', $data)) { foreach ($data['paramList'] as $i => $item) { + /** @var string $item */ if (isset($arr[$i + 1])) { $params[$item] = $arr[$i + 1]; } @@ -328,6 +340,7 @@ class Pusher implements WampServerInterface */ protected function subscribeUser(ConnectionInterface $connection, $userId) { + /** @var string */ /** @phpstan-ignore-next-line */ $resourceId = $connection->resourceId; diff --git a/application/Espo/Core/WebSocket/SenderFactory.php b/application/Espo/Core/WebSocket/SenderFactory.php index 2b01516373..fc15c188da 100644 --- a/application/Espo/Core/WebSocket/SenderFactory.php +++ b/application/Espo/Core/WebSocket/SenderFactory.php @@ -56,7 +56,7 @@ class SenderFactory implements Factory { $messager = $this->config->get('webSocketMessager') ?? self::DEFAULT_MESSAGER; - /** @var ?class-string */ + /** @var ?class-string */ $className = $this->metadata->get(['app', 'webSocket', 'messagers', $messager, 'senderClassName']); if (!$className) { diff --git a/application/Espo/Core/WebSocket/Submission.php b/application/Espo/Core/WebSocket/Submission.php index 0fd51a40c1..66c9257f62 100644 --- a/application/Espo/Core/WebSocket/Submission.php +++ b/application/Espo/Core/WebSocket/Submission.php @@ -30,6 +30,7 @@ namespace Espo\Core\WebSocket; use Espo\Core\Utils\Log; +use Espo\Core\Utils\Json; use stdClass; use Throwable; @@ -61,7 +62,7 @@ class Submission $data->topicId = $topic; - $message = json_encode($data); + $message = Json::encode($data); try { $this->sender->send($message); diff --git a/application/Espo/Core/WebSocket/SubscriberFactory.php b/application/Espo/Core/WebSocket/SubscriberFactory.php index 9caf4e4cb2..9514a9f4a3 100644 --- a/application/Espo/Core/WebSocket/SubscriberFactory.php +++ b/application/Espo/Core/WebSocket/SubscriberFactory.php @@ -56,7 +56,7 @@ class SubscriberFactory implements Factory { $messager = $this->config->get('webSocketMessager') ?? self::DEFAULT_MESSAGER; - /** @var ?class-string */ + /** @var ?class-string */ $className = $this->metadata->get(['app', 'webSocket', 'messagers', $messager, 'subscriberClassName']); if (!$className) { diff --git a/application/Espo/Core/Webhook/Manager.php b/application/Espo/Core/Webhook/Manager.php index 9338f46dff..f1635fb1c2 100644 --- a/application/Espo/Core/Webhook/Manager.php +++ b/application/Espo/Core/Webhook/Manager.php @@ -48,10 +48,7 @@ use Espo\Entities\{ */ class Manager { - /** - * @var string - */ - private $cacheKey = 'webhooks'; + private string $cacheKey = 'webhooks'; /** * @var string[] @@ -63,15 +60,15 @@ class Manager */ private $data = null; - private $config; + private Config $config; - private $dataCache; + private DataCache $dataCache; - private $entityManager; + private EntityManager $entityManager; - private $fieldUtil; + private FieldUtil $fieldUtil; - private $log; + private Log $log; public function __construct( Config $config, @@ -91,10 +88,16 @@ class Manager private function loadData(): void { - if ($this->config->get('useCache')) { - if ($this->dataCache->has($this->cacheKey)) { - $this->data = $this->dataCache->get($this->cacheKey); + if ($this->config->get('useCache') && $this->dataCache->has($this->cacheKey)) { + $data = $this->dataCache->get($this->cacheKey); + + if (!is_array($data)) { + $data = null; } + + /** @var ?array $data */ + + $this->data = $data; } if (is_null($this->data)) { @@ -128,8 +131,11 @@ class Manager ]) ->find(); - foreach ($list as $e) { - $data[$e->get('event')] = true; + foreach ($list as $webhook) { + /** @var string */ + $event = $webhook->getEvent(); + + $data[$event] = true; } return $data; diff --git a/application/Espo/Core/Webhook/Sender.php b/application/Espo/Core/Webhook/Sender.php index 6c8ae93b0f..5f2575d8af 100644 --- a/application/Espo/Core/Webhook/Sender.php +++ b/application/Espo/Core/Webhook/Sender.php @@ -29,11 +29,12 @@ namespace Espo\Core\Webhook; +use Espo\Core\Exceptions\Error; + use Espo\Entities\Webhook; -use Espo\Core\{ - Utils\Config, -}; +use Espo\Core\Utils\Config; +use Espo\Core\Utils\Json; /** * Sends a portion. @@ -53,14 +54,15 @@ class Sender /** * @param array $dataList + * @throws Error */ public function send(Webhook $webhook, array $dataList): int { - $payload = json_encode($dataList); + $payload = Json::encode($dataList); $signature = null; - $secretKey = $webhook->get('secretKey'); + $secretKey = $webhook->getSecretKey(); if ($secretKey) { $signature = $this->buildSignature($webhook, $payload, $secretKey); @@ -78,7 +80,17 @@ class Sender $headerList[] = 'X-Signature: ' . $signature; } - $handler = curl_init($webhook->get('url')); + $url = $webhook->getUrl(); + + if (!$url) { + throw new Error("Webhook does not have URL."); + } + + $handler = curl_init($url); + + if ($handler === false) { + throw new Error("Could not init CURL for URL {$url}."); + } curl_setopt($handler, \CURLOPT_RETURNTRANSFER, true); curl_setopt($handler, \CURLOPT_FOLLOWLOCATION, true); diff --git a/application/Espo/Entities/Webhook.php b/application/Espo/Entities/Webhook.php index 1470dfe485..3d6638c604 100644 --- a/application/Espo/Entities/Webhook.php +++ b/application/Espo/Entities/Webhook.php @@ -32,4 +32,19 @@ namespace Espo\Entities; class Webhook extends \Espo\Core\ORM\Entity { public const ENTITY_TYPE = 'Webhook'; + + public function getEvent(): ?string + { + return $this->get('event'); + } + + public function getSecretKey(): ?string + { + return $this->get('secretKey'); + } + + public function getUrl(): ?string + { + return $this->get('url'); + } }