From bef1e7d8f9a523d03a6f1f42716fdfe72612bcfc Mon Sep 17 00:00:00 2001 From: Henning Leutz <leutz@pcsg.de> Date: Fri, 24 Jun 2022 11:36:37 +0200 Subject: [PATCH] refactor: code style --- src/QUI/ERP/Coupons/CouponCode.php | 67 ++++++++++++++++++------------ 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/src/QUI/ERP/Coupons/CouponCode.php b/src/QUI/ERP/Coupons/CouponCode.php index 2a88c57..038cd06 100644 --- a/src/QUI/ERP/Coupons/CouponCode.php +++ b/src/QUI/ERP/Coupons/CouponCode.php @@ -2,11 +2,22 @@ namespace QUI\ERP\Coupons; +use DateTime; +use Exception; use QUI; use QUI\ERP\Discount\Handler as DiscountHandler; use QUI\ERP\Order\OrderInterface; use QUI\Permissions\Permission; +use function array_key_first; +use function count; +use function current; +use function in_array; +use function is_array; +use function is_null; +use function json_decode; +use function method_exists; + /** * Class CouponCode */ @@ -80,7 +91,7 @@ class CouponCode * * @var bool */ - protected $valid = true; + protected bool $valid = true; /** * Max usages @@ -93,7 +104,9 @@ class CouponCode * CouponCode constructor. * * @param int $id - Invite Code ID + * * @throws CouponCodeException + * @throws \Exception */ public function __construct(int $id) { @@ -126,22 +139,22 @@ public function __construct(int $id) ], 404); } - $data = \current($result); + $data = current($result); $this->id = (int)$data['id']; $this->code = $data['code']; $this->title = $data['title']; if (!empty($data['usages'])) { - $this->usages = \json_decode($data['usages'], true); + $this->usages = json_decode($data['usages'], true); } if (!empty($data['userIds'])) { - $this->userIds = \json_decode($data['userIds'], true); + $this->userIds = json_decode($data['userIds'], true); } if (!empty($data['groupIds'])) { - $this->groupIds = \json_decode($data['groupIds'], true); + $this->groupIds = json_decode($data['groupIds'], true); } if (!empty($data['maxUsages'])) { @@ -149,13 +162,13 @@ public function __construct(int $id) } if (!empty($data['discountIds'])) { - $this->discountIds = \json_decode($data['discountIds'], true); + $this->discountIds = json_decode($data['discountIds'], true); } - $this->CreateDate = new \DateTime($data['createDate']); + $this->CreateDate = new DateTime($data['createDate']); if (!empty($data['validUntilDate'])) { - $this->ValidUntilDate = new \DateTime($data['validUntilDate']); + $this->ValidUntilDate = new DateTime($data['validUntilDate']); $this->ValidUntilDate->setTime(23, 59, 59); } @@ -181,7 +194,7 @@ public function getCode(): string /** * @return \DateTime */ - public function getCreateDate(): \DateTime + public function getCreateDate(): DateTime { return $this->CreateDate; } @@ -199,7 +212,7 @@ public function getUsages(): array /** * @return \DateTime|null */ - public function getValidUntilDate(): ?\DateTime + public function getValidUntilDate(): ?DateTime { return $this->ValidUntilDate; } @@ -233,7 +246,7 @@ public function getDiscounts(): array foreach ($this->discountIds as $discountId) { try { $discounts[] = $DiscountHandler->getChild($discountId); - } catch (\Exception $Exception) { + } catch (Exception $Exception) { QUI\System\Log::writeDebugException($Exception); } } @@ -270,13 +283,13 @@ public function getGroupIds(): array */ public function redeem($User = null, $Order = null) { - if (\is_null($User)) { + if (is_null($User)) { $User = QUI::getUserBySession(); } $this->checkRedemption($User); - $Now = new \DateTime(); + $Now = new DateTime(); $usage = [ 'userId' => $User->getId(), @@ -284,7 +297,7 @@ public function redeem($User = null, $Order = null) 'orderPrefixedId' => false ]; - if (!\is_null($Order) && $Order instanceof QUI\ERP\Order\Order) { + if ($Order instanceof QUI\ERP\Order\Order) { $usage['orderPrefixedId'] = $Order->getPrefixedId(); } @@ -331,7 +344,7 @@ public function checkRedemption($User) try { /** @var QUI\ERP\Discount\Discount $Discount */ $Discount = $DiscountHandler->getChild($discountId); - } catch (\Exception $Exception) { + } catch (Exception $Exception) { $discountError = $Exception->getMessage(); continue; } @@ -343,7 +356,7 @@ public function checkRedemption($User) } if (!$discountsValid) { - if (\count($this->discountIds) === 1) { + if (count($this->discountIds) === 1) { throw new CouponCodeException([ 'quiqqer/coupons', 'exception.CouponCode.discount_invalid', @@ -382,7 +395,7 @@ public function checkRedemption($User) // Restriction to QUIQQER user(s) if (!empty($this->userIds)) { - if (\in_array($User->getId(), $this->userIds)) { + if (in_array($User->getId(), $this->userIds)) { if ($this->maxUsages !== Handler::MAX_USAGE_UNLIMITED && $this->hasUserRedeemed($User)) { throw new CouponCodeException([ @@ -446,7 +459,7 @@ public function checkOrderRedemption($Order) try { /** @var QUI\ERP\Discount\Discount $Discount */ $Discount = $DiscountHandler->getChild($discountId); - } catch (\Exception $Exception) { + } catch (Exception $Exception) { $discountError = $Exception->getMessage(); continue; } @@ -458,7 +471,7 @@ public function checkOrderRedemption($Order) } if (!$discountsValid) { - if (\count($this->discountIds) === 1) { + if (count($this->discountIds) === 1) { throw new CouponCodeException([ 'quiqqer/coupons', 'exception.CouponCode.discount_invalid', @@ -556,7 +569,7 @@ public function delete() if (!empty($Discount->getAttribute('hidden'))) { try { $Discount->delete(); - } catch (\Exception $Exception) { + } catch (Exception $Exception) { QUI\System\Log::writeException($Exception); } } @@ -602,7 +615,7 @@ protected function checkValidity() { // Check if the expiration date has been reached if (!empty($this->ValidUntilDate)) { - $Now = new \DateTime(); + $Now = new DateTime(); if ($Now > $this->ValidUntilDate) { $this->valid = false; @@ -655,7 +668,7 @@ public function addToOrder(QUI\ERP\Order\OrderInProcess $Order) return; } - if (!\is_array($coupons)) { + if (!is_array($coupons)) { return; } @@ -665,15 +678,15 @@ public function addToOrder(QUI\ERP\Order\OrderInProcess $Order) $vatArray = $calculations['vatArray']; $vat = false; - if (\count($vatArray) === 1) { - $vat = \array_key_first($vatArray); + if (count($vatArray) === 1) { + $vat = array_key_first($vatArray); } foreach ($coupons as $coupon) { /* @var $Coupon CouponCode */ try { $Coupon = Handler::getCouponCodeByCode($coupon); - } catch (\Exception $Exception) { + } catch (Exception $Exception) { continue; } @@ -731,13 +744,13 @@ public function addToOrder(QUI\ERP\Order\OrderInProcess $Order) $code = $Article->getCustomData()['code']; foreach ($articles as $Entry) { - if (!\method_exists($Entry, 'getCustomData')) { + if (!method_exists($Entry, 'getCustomData')) { continue; } $customData = $Entry->getCustomData(); - if (!$customData || !\is_array($customData)) { + if (!$customData || !is_array($customData)) { continue; } -- GitLab