Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize-contructor: #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions Block/Onepage/AffirmButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ class AffirmButton extends Template
*
* @var \Magento\Quote\Model\Quote
*/
protected $quote;
protected $quote = null;

/**
* @var \Magento\Checkout\Model\Session
*/
protected $session;

/**
* Button template
Expand All @@ -72,10 +77,23 @@ public function __construct(
array $data = []
) {
$this->helper = $helper;
$this->quote = $session->getQuote();
$this->session = $session;
parent::__construct($context, $data);
}

/**
* Return current quote from checkout session.
* @return \Magento\Quote\Api\Data\CartInterface|\Magento\Quote\Model\Quote
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getQuote(){
if(null == $this->quote){
$this->quote = $this->session->getQuote();
}
return $this->quote;
}

/**
* Get button image from system configs
*
Expand Down
32 changes: 25 additions & 7 deletions Controller/Payment/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ class Confirm extends Action implements CsrfAwareActionInterface
*
* @var \Magento\Quote\Model\Quote
*/
protected $quote;
protected $quote = null;

/**
* @var \Magento\Checkout\Model\Session
*/
protected $session;

/**
* Inject objects to the Confirm action
Expand All @@ -100,10 +105,23 @@ public function __construct(
$this->checkout = $checkout;
$this->checkoutSession = $checkoutSession;
$this->quoteManagement = $quoteManager;
$this->quote = $checkoutSession->getQuote();
$this->session = $checkoutSession;
parent::__construct($context);
}


/**
* Return current quote from checkout session.
* @return \Magento\Quote\Api\Data\CartInterface|\Magento\Quote\Model\Quote
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getQuote(){
if(null == $this->quote){
$this->quote = $this->session->getQuote();
}
return $this->quote;
}

/**
* @inheritDoc
*/
Expand All @@ -112,14 +130,14 @@ public function createCsrfValidationException(
): ?InvalidRequestException {
return null;
}

/**
* @inheritDoc
*/
public function validateForCsrf(RequestInterface $request): ?bool
{
return true;
}
}

/**
* Dispatch request
Expand All @@ -137,7 +155,7 @@ public function execute()
$this->checkoutSession->clearHelperData();

// "last successful quote"
$quoteId = $this->quote->getId();
$quoteId = $this->getQuote()->getId();
$this->checkoutSession->setLastQuoteId($quoteId)->setLastSuccessQuoteId($quoteId);

// an order may be created
Expand All @@ -149,7 +167,7 @@ public function execute()
}
$this->_eventManager->dispatch(
'affirm_place_order_success',
['order' => $order, 'quote' => $this->quote ]
['order' => $order, 'quote' => $this->getQuote() ]
);
$this->_redirect('checkout/onepage/success');
return;
Expand Down
26 changes: 22 additions & 4 deletions Helper/FinancingProgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ class FinancingProgram
*
* @var \Magento\Quote\Model\Quote
*/
protected $quote;
protected $quote = null;

/**
* @var \Magento\Checkout\Model\Session
*/
protected $session;

/**
* Customer session
Expand Down Expand Up @@ -120,7 +125,7 @@ public function __construct(
CategoryCollectionFactory $categoryCollectionFactory
) {
$this->customerSession = $customerSession;
$this->quote = $session->getQuote();
$this->session = $session;
$this->scopeConfig = $scopeConfig;
$this->affirmPaymentConfig = $configAffirm;
$this->_localeDate = $localeDate;
Expand All @@ -130,6 +135,19 @@ public function __construct(
$this->_init();
}

/**
* Return current quote from checkout session.
* @return \Magento\Quote\Api\Data\CartInterface|\Magento\Quote\Model\Quote
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getQuote(){
if(null == $this->quote){
$this->quote = $this->session->getQuote();
}
return $this->quote;
}

/**
* Initialization
*
Expand Down Expand Up @@ -257,7 +275,7 @@ public function getCustomerFinancingProgram()
protected function getQuoteProductCollection()
{
if (null === $this->products) {
$visibleQuoteItems = $this->quote->getAllVisibleItems();
$visibleQuoteItems = $this->getQuote()->getAllVisibleItems();
$productIds = [];
foreach ($visibleQuoteItems as $visibleQuoteItem) {
$productIds[] = $visibleQuoteItem->getProductId();
Expand Down Expand Up @@ -496,7 +514,7 @@ protected function getFinancingProgramFromCategoriesALS($productCollection)
*/
protected function getQuoteBaseGrandTotal()
{
return $this->quote->getBaseGrandTotal();
return $this->getQuote()->getBaseGrandTotal();
}

/**
Expand Down
34 changes: 26 additions & 8 deletions Helper/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ class Payment
*
* @var \Magento\Quote\Model\Quote
*/
protected $quote;
protected $quote = null;

/**
* @var \Magento\Checkout\Model\Session
*/
protected $session;

/**
* Method specification factory
Expand Down Expand Up @@ -149,7 +154,7 @@ public function __construct(
) {
$this->methodSpecificationFactory = $methodSpecificationFactory;
$this->payment = $payment;
$this->quote = $session->getQuote();
$this->session = $session;
$this->customerSession = $customerSession;
$this->storeManager = $storeManagerInterface;
$this->design = $design;
Expand All @@ -160,6 +165,19 @@ public function __construct(
$this->stockRegistry = $stockRegistry;
}

/**
* Return current quote from checkout session.
* @return \Magento\Quote\Api\Data\CartInterface|\Magento\Quote\Model\Quote
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getQuote(){
if(null == $this->quote){
$this->quote = $this->session->getQuote();
}
return $this->quote;
}

/**
* Get placeholder image
*
Expand Down Expand Up @@ -191,19 +209,19 @@ public function isAffirmAvailable()
AbstractMethod::CHECK_USE_FOR_CURRENCY,
AbstractMethod::CHECK_ORDER_TOTAL_MIN_MAX,
];
if ($this->quote->getIsVirtual() && !$this->quote->getCustomerId()) {
if ($this->getQuote()->getIsVirtual() && !$this->getQuote()->getCustomerId()) {
$checkData[] = AbstractMethod::CHECK_USE_FOR_COUNTRY;
}

$check = $this->methodSpecificationFactory
->create($checkData)
->isApplicable(
$this->payment,
$this->quote
$this->getQuote()
);

if ($check && $this->validateVirtual()) {
return $this->payment->isAvailable($this->quote);
return $this->payment->isAvailable($this->getQuote());
}
return false;
}
Expand Down Expand Up @@ -310,12 +328,12 @@ public function getConfigurableProductBackordersOptions(Product $product = null)
*/
public function validateVirtual()
{
if ($this->quote->getIsVirtual() && !$this->quote->getCustomerIsGuest()) {
if ($this->getQuote()->getIsVirtual() && !$this->getQuote()->getCustomerIsGuest()) {
$countryId = self::VALIDATE_COUNTRY;
// get customer addresses list
$addresses = $this->quote->getCustomer()->getAddresses();
$addresses = $this->getQuote()->getCustomer()->getAddresses();
// get default shipping address for the customer
$defaultShipping = $this->quote->getCustomer()->getDefaultShipping();
$defaultShipping = $this->getQuote()->getCustomer()->getDefaultShipping();
/** @var $address \Magento\Customer\Api\Data\AddressInterface */
if ($defaultShipping) {
foreach ($addresses as $address) {
Expand Down
41 changes: 26 additions & 15 deletions Model/AffirmCheckoutManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class AffirmCheckoutManager implements AffirmCheckoutManagerInterface
*
* @var \Magento\Quote\Model\Quote
*/
protected $quote;
protected $quote = null;

/**
* Injected repository
Expand Down Expand Up @@ -136,7 +136,6 @@ public function __construct(
Logger $logger
) {
$this->checkoutSession = $checkoutSession;
$this->quote = $this->checkoutSession->getQuote();
$this->quoteRepository = $quoteRepository;
$this->productMetadata = $productMetadata;
$this->moduleResource = $moduleResource;
Expand All @@ -146,6 +145,19 @@ public function __construct(
$this->logger = $logger;
}

/**
* Return current quote from checkout session.
* @return \Magento\Quote\Api\Data\CartInterface|\Magento\Quote\Model\Quote
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getQuote(){
if(null == $this->quote){
$this->quote = $this->checkoutSession->getQuote();
}
return $this->quote;
}

/**
* Init checkout and get retrieve increment id
* form affirm checkout
Expand All @@ -156,11 +168,11 @@ public function __construct(
public function initCheckout()
{
// collection totals before submit
$this->quote->collectTotals();
$this->quote->reserveOrderId();
$orderIncrementId = $this->quote->getReservedOrderId();
$discountAmount = $this->quote->getBaseSubtotal() - $this->quote->getBaseSubtotalWithDiscount();
$shippingAddress = $this->quote->getShippingAddress();
$this->getQuote()->collectTotals();
$this->getQuote()->reserveOrderId();
$orderIncrementId = $this->getQuote()->getReservedOrderId();
$discountAmount = $this->getQuote()->getBaseSubtotal() - $this->getQuote()->getBaseSubtotalWithDiscount();
$shippingAddress = $this->getQuote()->getShippingAddress();

$response = [];
if ($discountAmount > 0.001) {
Expand All @@ -173,11 +185,10 @@ public function initCheckout()
}

try {
$country = $this
->quote
$country = $this->getQuote()
->getBillingAddress()
->getCountry();
$result = $this->quote
$result = $this->getQuote()
->getPayment()
->getMethodInstance()
->canUseForCountry($country);
Expand All @@ -190,7 +201,7 @@ public function initCheckout()
return $e->getMessage();
}
if ($orderIncrementId) {
$this->quoteRepository->save($this->quote);
$this->quoteRepository->save($this->getQuote());
$response['order_increment_id'] = $orderIncrementId;
}
if ($this->productMetadata->getEdition() == 'Enterprise') {
Expand All @@ -199,7 +210,7 @@ public function initCheckout()
if ($wrapped) {
$response['wrapped_items'] = $wrapped;
}
$giftCards = $this->quote->getGiftCards();
$giftCards = $this->getQuote()->getGiftCards();
if ($giftCards) {
$giftCards = json_decode($giftCards);
foreach ($giftCards as $giftCard) {
Expand All @@ -213,7 +224,7 @@ public function initCheckout()


$itemTypes = array();
$items = $this->quote->getAllVisibleItems();
$items = $this->getQuote()->getAllVisibleItems();
foreach ($items as $item) {
$itemTypes[] = $item->getProductType();
}
Expand Down Expand Up @@ -248,7 +259,7 @@ public function initCheckout()
}

private function getShippingAddress(){
$shippingAddress = $this->quote->getShippingAddress();
$shippingAddress = $this->getQuote()->getShippingAddress();
$shippingObject = array(
'name' => array(
'full_name' => $shippingAddress->getName(),
Expand All @@ -267,7 +278,7 @@ private function getShippingAddress(){
}

private function getBillingAddress(){
$billingAddress = $this->quote->getBillingAddress();
$billingAddress = $this->getQuote()->getBillingAddress();
$billingObject = array(
'name' => array(
'full_name' => $billingAddress->getName(),
Expand Down
Loading