Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CurrentCustomer.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Customer\Api\Data\CustomerInterfaceFactory;
13 use Magento\Framework\Module\Manager as ModuleManager;
15 
20 {
24  protected $customerSession;
25 
29  protected $layout;
30 
34  protected $customerFactory;
35 
40 
44  protected $request;
45 
49  protected $moduleManager;
50 
54  protected $view;
55 
65  public function __construct(
68  CustomerInterfaceFactory $customerFactory,
71  ModuleManager $moduleManager,
73  ) {
74  $this->customerSession = $customerSession;
75  $this->layout = $layout;
76  $this->customerFactory = $customerFactory;
77  $this->customerRepository = $customerRepository;
78  $this->request = $request;
79  $this->moduleManager = $moduleManager;
80  $this->view = $view;
81  }
82 
88  protected function getDepersonalizedCustomer()
89  {
90  $customer = $this->customerFactory->create();
91  $customer->setGroupId($this->customerSession->getCustomerGroupId());
92  return $customer;
93  }
94 
100  protected function getCustomerFromService()
101  {
102  return $this->customerRepository->getById($this->customerSession->getId());
103  }
104 
110  public function getCustomer()
111  {
112  if ($this->moduleManager->isEnabled('Magento_PageCache')
113  && !$this->request->isAjax()
114  && $this->view->isLayoutLoaded()
115  && $this->layout->isCacheable()
116  ) {
117  return $this->getDepersonalizedCustomer();
118  } else {
119  return $this->getCustomerFromService();
120  }
121  }
122 
128  public function getCustomerId()
129  {
130  return $this->customerSession->getId();
131  }
132 
139  public function setCustomerId($customerId)
140  {
141  $this->customerSession->setId($customerId);
142  }
143 }
$customer
Definition: customers.php:11
__construct(CustomerSession $customerSession, LayoutInterface $layout, CustomerInterfaceFactory $customerFactory, CustomerRepositoryInterface $customerRepository, RequestInterface $request, ModuleManager $moduleManager, ViewInterface $view)