Info.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Account\Dashboard;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. /**
  9. * Dashboard Customer Info
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Info extends \Magento\Framework\View\Element\Template
  15. {
  16. /**
  17. * Cached subscription object
  18. *
  19. * @var \Magento\Newsletter\Model\Subscriber
  20. */
  21. protected $_subscription;
  22. /**
  23. * @var \Magento\Newsletter\Model\SubscriberFactory
  24. */
  25. protected $_subscriberFactory;
  26. /**
  27. * @var \Magento\Customer\Helper\View
  28. */
  29. protected $_helperView;
  30. /**
  31. * @var \Magento\Customer\Helper\Session\CurrentCustomer
  32. */
  33. protected $currentCustomer;
  34. /**
  35. * Constructor
  36. *
  37. * @param \Magento\Framework\View\Element\Template\Context $context
  38. * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
  39. * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
  40. * @param \Magento\Customer\Helper\View $helperView
  41. * @param array $data
  42. */
  43. public function __construct(
  44. \Magento\Framework\View\Element\Template\Context $context,
  45. \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
  46. \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
  47. \Magento\Customer\Helper\View $helperView,
  48. array $data = []
  49. ) {
  50. $this->currentCustomer = $currentCustomer;
  51. $this->_subscriberFactory = $subscriberFactory;
  52. $this->_helperView = $helperView;
  53. parent::__construct($context, $data);
  54. }
  55. /**
  56. * Returns the Magento Customer Model for this block
  57. *
  58. * @return \Magento\Customer\Api\Data\CustomerInterface|null
  59. */
  60. public function getCustomer()
  61. {
  62. try {
  63. return $this->currentCustomer->getCustomer();
  64. } catch (NoSuchEntityException $e) {
  65. return null;
  66. }
  67. }
  68. /**
  69. * Get the full name of a customer
  70. *
  71. * @return string full name
  72. */
  73. public function getName()
  74. {
  75. return $this->_helperView->getCustomerName($this->getCustomer());
  76. }
  77. /**
  78. * @return string
  79. */
  80. public function getChangePasswordUrl()
  81. {
  82. return $this->_urlBuilder->getUrl('customer/account/edit/changepass/1');
  83. }
  84. /**
  85. * Get Customer Subscription Object Information
  86. *
  87. * @return \Magento\Newsletter\Model\Subscriber
  88. */
  89. public function getSubscriptionObject()
  90. {
  91. if (!$this->_subscription) {
  92. $this->_subscription = $this->_createSubscriber();
  93. $customer = $this->getCustomer();
  94. if ($customer) {
  95. $this->_subscription->loadByCustomerId($customer->getId());
  96. }
  97. }
  98. return $this->_subscription;
  99. }
  100. /**
  101. * Gets Customer subscription status
  102. *
  103. * @return bool
  104. *
  105. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  106. */
  107. public function getIsSubscribed()
  108. {
  109. return $this->getSubscriptionObject()->isSubscribed();
  110. }
  111. /**
  112. * Newsletter module availability
  113. *
  114. * @return bool
  115. */
  116. public function isNewsletterEnabled()
  117. {
  118. return $this->getLayout()
  119. ->getBlockSingleton(\Magento\Customer\Block\Form\Register::class)
  120. ->isNewsletterEnabled();
  121. }
  122. /**
  123. * @return \Magento\Newsletter\Model\Subscriber
  124. */
  125. protected function _createSubscriber()
  126. {
  127. return $this->_subscriberFactory->create();
  128. }
  129. /**
  130. * @return string
  131. */
  132. protected function _toHtml()
  133. {
  134. return $this->currentCustomer->getCustomerId() ? parent::_toHtml() : '';
  135. }
  136. }