Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FrontendDecorator.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Mtf\Util\Protocol\CurlInterface;
12 
16 class FrontendDecorator implements CurlInterface
17 {
23  protected $transport;
24 
30  protected $formKey = null;
31 
37  protected $response;
38 
44  protected $cookies = '';
45 
51  public function __construct(CurlTransport $transport, Customer $customer)
52  {
53  $this->transport = $transport;
54  $this->authorize($customer);
55  }
56 
64  protected function authorize(Customer $customer)
65  {
66  $url = $_ENV['app_frontend_url'] . 'customer/account/login/';
67  $this->transport->write($url, [], CurlInterface::GET);
68  $this->read();
69  $url = $_ENV['app_frontend_url'] . 'customer/account/loginPost/';
70  $data = [
71  'login[username]' => $customer->getEmail(),
72  'login[password]' => $customer->getPassword(),
73  'form_key' => $this->formKey,
74  ];
75  $this->transport->write($url, $data, CurlInterface::POST, ['Set-Cookie:' . $this->cookies]);
76  $response = $this->read();
77  if (strpos($response, 'customer/account/login') !== false) {
78  throw new \Exception($customer->getFirstname() . ', cannot be logged in by curl handler!');
79  }
80  }
81 
87  protected function initFormKey()
88  {
89  $str = substr($this->response, strpos($this->response, 'form_key'));
90  preg_match('/value="(.*)" \/>/', $str, $matches);
91  if (!empty($matches[1])) {
92  $this->formKey = $matches[1];
93  }
94  }
95 
101  protected function initCookies()
102  {
103  preg_match_all('|Set-Cookie: (.*);|U', $this->response, $matches);
104  if (!empty($matches[1])) {
105  $this->cookies = implode('; ', $matches[1]);
106  }
107  }
108 
118  public function write($url, $params = [], $method = CurlInterface::POST, $headers = [])
119  {
120  if ($this->formKey) {
121  $params['form_key'] = $this->formKey;
122  }
123  $headers = ['Set-Cookie:' . $this->cookies];
124  $this->transport->write($url, http_build_query($params), $method, $headers);
125  }
126 
132  public function read()
133  {
134  $this->response = $this->transport->read();
135  $this->initCookies();
136  $this->initFormKey();
137  return $this->response;
138  }
139 
147  public function addOption($option, $value)
148  {
149  $this->transport->addOption($option, $value);
150  }
151 
157  public function close()
158  {
159  $this->transport->close();
160  }
161 }
__construct(CurlTransport $transport, Customer $customer)
$customer
Definition: customers.php:11
write($url, $params=[], $method=CurlInterface::POST, $headers=[])
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18