Skip to content

Commit

Permalink
Merge pull request #7795 from magento-atwix-pyrrans/AC-5889
Browse files Browse the repository at this point in the history
[Pyrrans] AC-5889 replaced Zend_HTTP with laminas-http
  • Loading branch information
Andrii Beziazychnyi authored Aug 25, 2022
2 parents bddae56 + 50e7536 commit 1e46eec
Show file tree
Hide file tree
Showing 55 changed files with 828 additions and 582 deletions.
5 changes: 3 additions & 2 deletions app/code/Magento/AdminNotification/Model/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\AdminNotification\Model;

use Laminas\Http\Request;
use Magento\AdminNotification\Model\InboxFactory;
use Magento\Backend\App\ConfigInterface;
use Magento\Framework\App\DeploymentConfig;
Expand Down Expand Up @@ -227,7 +228,7 @@ public function getFeedData()
{
/** @var Curl $curl */
$curl = $this->curlFactory->create();
$curl->setConfig(
$curl->setOptions(
[
'timeout' => 2,
'useragent' => $this->productMetadata->getName()
Expand All @@ -236,7 +237,7 @@ public function getFeedData()
'referer' => $this->urlBuilder->getUrl('*/*/*')
]
);
$curl->write(\Zend_Http_Client::GET, $this->getFeedUrl(), '1.0');
$curl->write(Request::METHOD_GET, $this->getFeedUrl(), '1.0');
$data = $curl->read();
$data = preg_split('/^\r?$/m', $data, 2);
$data = trim($data[1] ?? '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@

namespace Magento\AdminNotification\Model\System\Message;

use Laminas\Http\Request;
use Laminas\Http\Response;
use Magento\Backend\App\ConfigInterface;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\Adapter\Curl;
use Magento\Framework\HTTP\Adapter\CurlFactory;
use Magento\Framework\Notification\MessageInterface;
use Magento\Framework\Phrase;
use Magento\Store\Model\Store;
use Throwable;

/**
* @api
* @since 100.0.2
*/
class Security implements \Magento\Framework\Notification\MessageInterface
class Security implements MessageInterface
{
/**
* Cache key for saving verification result
*/
const VERIFICATION_RESULT_CACHE_KEY = 'configuration_files_access_level_verification';
public const VERIFICATION_RESULT_CACHE_KEY = 'configuration_files_access_level_verification';

/**
* File path for verification
Expand All @@ -34,36 +44,36 @@ class Security implements \Magento\Framework\Notification\MessageInterface
private $_verificationTimeOut = 2;

/**
* @var \Magento\Framework\App\CacheInterface
* @var CacheInterface
*/
protected $_cache;

/**
* @var \Magento\Backend\App\ConfigInterface
* @var ConfigInterface
*/
protected $_backendConfig;

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
* @var ScopeConfigInterface
*/
protected $_config;

/**
* @var \Magento\Framework\HTTP\Adapter\CurlFactory
* @var CurlFactory
*/
protected $_curlFactory;

/**
* @param \Magento\Framework\App\CacheInterface $cache
* @param \Magento\Backend\App\ConfigInterface $backendConfig
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory
* @param CacheInterface $cache
* @param ConfigInterface $backendConfig
* @param ScopeConfigInterface $config
* @param CurlFactory $curlFactory
*/
public function __construct(
\Magento\Framework\App\CacheInterface $cache,
\Magento\Backend\App\ConfigInterface $backendConfig,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory
CacheInterface $cache,
ConfigInterface $backendConfig,
ScopeConfigInterface $config,
CurlFactory $curlFactory
) {
$this->_cache = $cache;
$this->_backendConfig = $backendConfig;
Expand Down Expand Up @@ -100,12 +110,12 @@ private function _isFileAccessible()
{
$unsecureBaseURL = $this->_config->getValue(Store::XML_PATH_UNSECURE_BASE_URL, 'default');

/** @var $http \Magento\Framework\HTTP\Adapter\Curl */
/** @var $http Curl */
$http = $this->_curlFactory->create();
$http->setConfig(['timeout' => $this->_verificationTimeOut]);
$http->write(\Zend_Http_Client::POST, $unsecureBaseURL . $this->_filePath);
$http->setOptions(['timeout' => $this->_verificationTimeOut]);
$http->write(Request::METHOD_POST, $unsecureBaseURL . $this->_filePath);
$responseBody = $http->read();
$responseCode = \Zend_Http_Response::extractCode($responseBody);
$responseCode = $this->extractCodeFromResponse($responseBody);
$http->close();

return $responseCode == 200;
Expand Down Expand Up @@ -134,7 +144,7 @@ public function isDisplayed()
/**
* Retrieve message text
*
* @return \Magento\Framework\Phrase
* @return Phrase
*/
public function getText()
{
Expand All @@ -151,6 +161,24 @@ public function getText()
*/
public function getSeverity()
{
return \Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL;
return MessageInterface::SEVERITY_CRITICAL;
}

/**
* Extract the response code from a response string
*
* @param string $responseString
*
* @return false|int
*/
private function extractCodeFromResponse(string $responseString)
{
try {
$responseCode = Response::fromString($responseString)->getStatusCode();
} catch (Throwable $e) {
$responseCode = false;
}

return $responseCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function testCheckUpdate(bool $callInbox, string $curlRequest): void
$this->curlFactory
->method('create')
->willReturn($this->curl);
$this->curl->expects($this->once())->method('setConfig')->with($configValues)->willReturnSelf();
$this->curl->expects($this->once())->method('setOptions')->with($configValues)->willReturnSelf();
$this->curl->expects($this->once())->method('read')->willReturn($curlRequest);
$this->backendConfig->expects($this->once())->method('isSetFlag')->willReturn(false);
$this->backendConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Analytics\Model\Connector\Http\Client;

use Laminas\Http\Response;
use Magento\Analytics\Model\Connector\Http\ConverterInterface;
use Psr\Log\LoggerInterface;
use Magento\Framework\HTTP\Adapter\CurlFactory;
Expand Down Expand Up @@ -56,11 +57,12 @@ public function __construct(
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function request($method, $url, array $body = [], array $headers = [], $version = '1.1')
{
$response = new \Zend_Http_Response(0, []);
$response = new Response();
$response->setCustomStatusCode(Response::STATUS_CODE_CUSTOM);

try {
$curl = $this->curlFactory->create();
Expand Down Expand Up @@ -93,6 +95,8 @@ public function request($method, $url, array $body = [], array $headers = [], $v
}

/**
* Apply content type header from converter
*
* @param array $headers
*
* @return array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
*/
namespace Magento\Analytics\Model\Connector\Http;

use Laminas\Http\Response;

/**
* An interface for an HTTP client.
*
* Sends requests via a proper adapter.
* Send requests via a proper adapter.
*/
interface ClientInterface
{
Expand All @@ -23,7 +25,7 @@ interface ClientInterface
* @param array $headers
* @param string $version
*
* @return \Zend_Http_Response
* @return Response
*/
public function request($method, $url, array $body = [], array $headers = [], $version = '1.1');
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Analytics\Model\Connector\Http;

use Laminas\Http\Response;

/**
* Extract result from http response. Call response handler by status.
*/
Expand All @@ -13,12 +15,12 @@ class ResponseResolver
/**
* @var ConverterInterface
*/
private $converter;
private ConverterInterface $converter;

/**
* @var array
*/
private $responseHandlers;
private array $responseHandlers;

/**
* @param ConverterInterface $converter
Expand All @@ -33,24 +35,27 @@ public function __construct(ConverterInterface $converter, array $responseHandle
/**
* Get result from $response.
*
* @param \Zend_Http_Response $response
* @param Response $response
* @return bool|string
*/
public function getResult(\Zend_Http_Response $response)
public function getResult(Response $response)
{
$result = false;
$converterMediaType = $this->converter->getContentMediaType();

/** Content-Type header may not only contain media-type declaration */
if ($response->getBody()
&& is_int(strripos($response->getHeader('Content-Type'), (string) $converterMediaType))) {
$responseBody = $this->converter->fromBody($response->getBody());
$responseBody = $response->getBody();
$contentType = $response->getHeaders()->has('Content-Type') ?
$response->getHeaders()->get('Content-Type')->getFieldValue() :
'';
if ($responseBody && is_int(strripos($contentType, $converterMediaType))) {
$responseBody = $this->converter->fromBody($responseBody);
} else {
$responseBody = [];
}

if (array_key_exists($response->getStatus(), $this->responseHandlers)) {
$result = $this->responseHandlers[$response->getStatus()]->handleResponse($responseBody);
if (array_key_exists($response->getStatusCode(), $this->responseHandlers)) {
$result = $this->responseHandlers[$response->getStatusCode()]->handleResponse($responseBody);
}

return $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/
namespace Magento\Analytics\Model\Connector;

use Laminas\Http\Request;
use Magento\Analytics\Model\AnalyticsToken;
use Magento\Analytics\Model\Connector\Http\ResponseResolver;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\ZendClient;
use Psr\Log\LoggerInterface;
use Magento\Store\Model\Store;
use Magento\Analytics\Model\Connector\Http\ResponseResolver;
use Psr\Log\LoggerInterface;

/**
* Command notifies MBI about that data collection was finished.
Expand Down Expand Up @@ -79,7 +79,7 @@ public function execute()
$result = false;
if ($this->analyticsToken->isTokenExist()) {
$response = $this->httpClient->request(
ZendClient::POST,
Request::METHOD_POST,
$this->config->getValue($this->notifyDataChangedUrlPath),
[
"access-token" => $this->analyticsToken->getToken(),
Expand Down
8 changes: 5 additions & 3 deletions app/code/Magento/Analytics/Model/Connector/OTPRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/
namespace Magento\Analytics\Model\Connector;

use Laminas\Http\Request;
use Magento\Analytics\Model\AnalyticsToken;
use Magento\Analytics\Model\Connector\Http\ResponseResolver;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\ZendClient;
use Magento\Store\Model\Store;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -91,7 +91,7 @@ public function call()

if ($this->analyticsToken->isTokenExist()) {
$response = $this->httpClient->request(
ZendClient::POST,
Request::METHOD_POST,
$this->config->getValue($this->otpUrlConfigPath),
[
"access-token" => $this->analyticsToken->getToken(),
Expand All @@ -105,7 +105,9 @@ public function call()
sprintf(
'Obtaining of an OTP from the MBI service has been failed: %s. Content-Type: %s',
!empty($response->getBody()) ? $response->getBody() : 'Response body is empty',
$response->getHeader('Content-Type')
$response->getHeaders()->has('Content-Type') ?
$response->getHeaders()->get('Content-Type')->getFieldValue() :
''
)
);
}
Expand Down
10 changes: 6 additions & 4 deletions app/code/Magento/Analytics/Model/Connector/SignUpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
*/
namespace Magento\Analytics\Model\Connector;

use Laminas\Http\Request;
use Magento\Analytics\Model\AnalyticsToken;
use Magento\Analytics\Model\Connector\Http\ResponseResolver;
use Magento\Analytics\Model\IntegrationManager;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Psr\Log\LoggerInterface;
use Magento\Framework\HTTP\ZendClient;
use Magento\Store\Model\Store;
use Psr\Log\LoggerInterface;

/**
* SignUp merchant for Free Tier project
Expand Down Expand Up @@ -98,7 +98,7 @@ public function execute()
if ($integrationToken) {
$this->integrationManager->activateIntegration();
$response = $this->httpClient->request(
ZendClient::POST,
Request::METHOD_POST,
$this->config->getValue($this->signUpUrlPath),
[
"token" => $integrationToken->getData('token'),
Expand All @@ -113,7 +113,9 @@ public function execute()
'Subscription for MBI service has been failed. An error occurred during token exchange: %s.'
. ' Content-Type: %s',
!empty($response->getBody()) ? $response->getBody() : 'Response body is empty',
$response->getHeader('Content-Type')
$response->getHeaders()->has('Content-Type') ?
$response->getHeaders()->get('Content-Type')->getFieldValue() :
''
)
);
}
Expand Down
Loading

0 comments on commit 1e46eec

Please sign in to comment.