Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BackendDecorator.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Mtf\Config\DataInterface;
10 use Magento\Mtf\Util\Protocol\CurlInterface;
12 
16 class BackendDecorator implements CurlInterface
17 {
23  protected $transport;
24 
30  protected $formKey = null;
31 
37  protected $response;
38 
44  protected $configuration;
45 
51  public function __construct(CurlTransport $transport, DataInterface $configuration)
52  {
53  $this->transport = $transport;
54  $this->configuration = $configuration;
55  $this->authorize();
56  }
57 
64  protected function authorize()
65  {
66  // Perform GET to backend url so form_key is set
67  $url = $_ENV['app_backend_url'];
68  $this->transport->write($url, [], CurlInterface::GET);
69  $this->read();
70 
71  $url = $_ENV['app_backend_url'] . $this->configuration->get('application/0/backendLoginUrl/0/value');
72  $data = [
73  'login[username]' => $this->configuration->get('application/0/backendLogin/0/value'),
74  'login[password]' => $this->configuration->get('application/0/backendPassword/0/value'),
75  'form_key' => $this->formKey,
76  ];
77  $this->transport->write($url, $data, CurlInterface::POST);
78  $response = $this->read();
79  if (strpos($response, 'login-form') !== false) {
80  throw new \Exception(
81  'Admin user cannot be logged in by curl handler!'
82  );
83  }
84  }
85 
91  protected function initFormKey()
92  {
93  preg_match('!var FORM_KEY = \'(\w+)\';!', $this->response, $matches);
94  if (!empty($matches[1])) {
95  $this->formKey = $matches[1];
96  }
97  }
98 
109  public function write($url, $params = [], $method = CurlInterface::POST, $headers = [])
110  {
111  if ($this->formKey) {
112  $params['form_key'] = $this->formKey;
113  } else {
114  throw new \Exception(sprintf('Form key is absent! Url: "%s" Response: "%s"', $url, $this->response));
115  }
116  $this->transport->write($url, http_build_query($params), $method, $headers);
117  }
118 
124  public function read()
125  {
126  $this->response = $this->transport->read();
127  $this->initFormKey();
128  return $this->response;
129  }
130 
138  public function addOption($option, $value)
139  {
140  $this->transport->addOption($option, $value);
141  }
142 
148  public function close()
149  {
150  $this->transport->close();
151  }
152 }
write($url, $params=[], $method=CurlInterface::POST, $headers=[])
__construct(CurlTransport $transport, DataInterface $configuration)
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18