Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Add Http\AbstractRequest, Http\RequestInterface, refactor Http\Request
Browse files Browse the repository at this point in the history
  • Loading branch information
nickolasburr committed Feb 2, 2019
1 parent 4aecb75 commit 04f9008
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 60 deletions.
117 changes: 117 additions & 0 deletions Model/Http/AbstractRequest.php
@@ -0,0 +1,117 @@
<?php
/**
* AbstractRequest.php
*
* NOTICE OF LICENSE
*
* This source file is subject to the MIT License, which
* is bundled with this package in the file LICENSE.txt.
*
* It is also available on the Internet at the following URL:
* https://docs.auroraextensions.com/magento/extensions/2.x/magentocroncloudfunctions/LICENSE.txt
*
* @package AuroraExtensions_MagentoCronCloudFunctions
* @copyright Copyright (C) 2019 Aurora Extensions <support@auroraextensions.com>
* @license MIT License
*/

namespace AuroraExtensions\MagentoCronCloudFunctions\Model\Http;

use AuroraExtensions\MagentoCronCloudFunctions\Helper\Cron as CronHelper;
use AuroraExtensions\MagentoCronCloudFunctions\Helper\Dict;
use AuroraExtensions\MagentoCronCloudFunctions\Model\Cron\JobInterface;

use Magento\Framework\DataObject;
use Zend\Http\Request as HttpRequest;

abstract class AbstractRequest extends DataObject implements RequestInterface
{
/** @property CronHelper $cronHelper */
protected $cronHelper;

/** @property JobInterface $cronJob */
protected $cronJob;

/** @property HeadersInterface $headers */
protected $headers;

/** @property HttpRequest $request */
protected $request;

/** @property TransportInterface $transport */
protected $transport;

/**
* @param CronHelper $cronHelper
* @param JobInterface $cronJob
* @param HeadersInterface $httpHeaders
* @param RequestInterface $httpRequest
* @param TransportInterface $transport
*/
public function __construct(
CronHelper $cronHelper,
JobInterface $cronJob,
HeadersInterface $headers,
HttpRequest $request,
TransportInterface $transport,
array $data = []
) {
$this->cronHelper = $cronHelper;
$this->cronJob = $cronJob;
$this->headers = $headers;
$this->request = $request;
$this->transport = $transport;

parent::__construct($data);
}

/**
* Get Zend HTTP request object.
*
* @return HttpRequest
*/
public function getHttpRequest()
{
return $this->request;
}

/**
* Set HTTP headers for request.
*
* @param array $headers
* @return $this
*/
public function setHeaders(array $headers = [])
{
$this->headers->addHeaders($headers);
$this->request->setHeaders($this->headers);

return $this;
}

/**
* Set HTTP method for request.
*
* @param string $method
* @return $this
*/
public function setMethod($method)
{
$this->request->setMethod($method);

return $this;
}

/**
* Set HTTP endpoint URI.
*
* @param string $uri
* @return $this
*/
public function setUri($uri)
{
$this->request->setUri($uri);

return $this;
}
}
76 changes: 16 additions & 60 deletions Model/Http/Request.php
Expand Up @@ -17,53 +17,8 @@

namespace AuroraExtensions\MagentoCronCloudFunctions\Model\Http;

use AuroraExtensions\MagentoCronCloudFunctions\Helper\Cron as CronHelper;
use AuroraExtensions\MagentoCronCloudFunctions\Helper\Dict;
use AuroraExtensions\MagentoCronCloudFunctions\Model\Cron\JobInterface;

use Zend\Http\Client as HttpClient;
use Zend\Http\Headers as HttpHeaders;
use Zend\Http\Request as HttpRequest;
use Zend\Http\Response as HttpResponse;

class Request
class Request extends AbstractRequest implements RequestInterface
{
/** @property CronHelper $cronHelper */
protected $cronHelper;

/** @property JobInterface $cronJob */
protected $cronJob;

/** @property HttpClient $httpClient */
protected $httpClient;

/** @property HttpHeaders $httpHeaders */
protected $httpHeaders;

/** @property HttpRequest $httpRequest */
protected $httpRequest;

/**
* @param CronHelper $cronHelper
* @param JobInterface $cronJob
* @param HttpClient $httpClient
* @param HttpHeaders $httpHeaders
* @param HttpRequest $request
*/
public function __construct(
CronHelper $cronHelper,
JobInterface $cronJob,
HttpClient $httpClient,
HttpHeaders $httpHeaders,
HttpRequest $httpRequest
) {
$this->cronHelper = $cronHelper;
$this->cronJob = $cronJob;
$this->httpClient = $httpClient;
$this->httpHeaders = $httpHeaders;
$this->httpRequest = $httpRequest;
}

/**
* Trigger Cloud Functions HTTP endpoint.
*
Expand All @@ -72,23 +27,24 @@ public function __construct(
*/
public function execute()
{
/** @var string $endpointUrl */
$endpointUrl = $this->cronHelper->getEndpointUrl($this->cronJob->getJobCode());
/** @var string $httpEndpoint */
$httpEndpoint = $this->cronHelper->getEndpointUrl($this->cronJob->getJobCode());

$this->httpHeaders->addHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/json',
]);
if ($this->hasData('headers')) {
$this->setHeaders($this->getData('headers'));
} else {
$this->setHeaders(Headers::$defaultHeaders);
}

$this->httpRequest->setHeaders($this->httpHeaders);
$this->httpRequest->setUri($endpointUrl);
$this->httpRequest->setMethod($this->cronJob->getHttpMethod());
$this->setMethod($this->cronJob->getHttpMethod());
$this->setUri($httpEndpoint);

$this->httpClient->setOptions([
'adapter' => HttpClient\Adapter\Curl::class,
'timeout' => 30,
]);
if ($this->hasData('options')) {
$this->transport->setOptions($this->getData('options'));
} else {
$this->transport->setOptions(Transport::$defaultOptions);
}

$this->httpClient->send($this->httpRequest);
$this->transport->send($this);
}
}
39 changes: 39 additions & 0 deletions Model/Http/RequestInterface.php
@@ -0,0 +1,39 @@
<?php
/**
* RequestInterface.php
*
* NOTICE OF LICENSE
*
* This source file is subject to the MIT License, which
* is bundled with this package in the file LICENSE.txt.
*
* It is also available on the Internet at the following URL:
* https://docs.auroraextensions.com/magento/extensions/2.x/magentocroncloudfunctions/LICENSE.txt
*
* @package AuroraExtensions_MagentoCronCloudFunctions
* @copyright Copyright (C) 2019 Aurora Extensions <support@auroraextensions.com>
* @license MIT License
*/

namespace AuroraExtensions\MagentoCronCloudFunctions\Model\Http;

interface RequestInterface
{
/**
* @param array $headers
* @return $this
*/
public function setHeaders(array $headers);

/**
* @param string $method
* @return $this
*/
public function setMethod($method);

/**
* @param string $uri
* @return $this
*/
public function setUri($uri);
}

0 comments on commit 04f9008

Please sign in to comment.