Skip to content

Commit

Permalink
Make cookie optional, clean up code, add Config
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Apr 19, 2019
1 parent 2e883e1 commit d16d7d0
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions Plugin/ResponsePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,55 @@
namespace Yireo\ServerPush\Plugin;

use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\App\Response\Http as HttpResponse;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\State as AppState;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Symfony\Component\DomCrawler\Crawler;
use Yireo\ServerPush\Config\Config;

/**
* Plugin to add a Link header for each static asset
*/
class ResponsePlugin
{
/** @var HttpRequest */
protected $request;

/**
* @var StoreManagerInterface
* @var Config
*/
private $storeManager;
private $config;

/**
* @var AppState
* @var HttpRequest
*/
private $appState;
private $request;

/**
* @var ScopeInterface
* @var StoreManagerInterface
*/
protected $scopeConfig;
private $storeManager;

/**
* @var CookieManagerInterface
*/
private $cookieManager;

/**
* \Magento\Store\Model\StoreManagerInterface $storeManager
*
* @param Config $config
* @param HttpRequest $request
* @param StoreManagerInterface $storeManager
* @param AppState $appState
* @param ScopeConfigInterface $scopeConfigInterface
* @param CookieManagerInterface $cookieManager
*/
public function __construct(
Config $config,
HttpRequest $request,
StoreManagerInterface $storeManager,
AppState $appState,
ScopeConfigInterface $scopeConfigInterface,
CookieManagerInterface $cookieManager
) {
$this->config = $config;
$this->request = $request;
$this->storeManager = $storeManager;
$this->appState = $appState;
$this->scopeConfig = $scopeConfigInterface;
$this->cookieManager = $cookieManager;
}

Expand All @@ -88,13 +79,9 @@ public function beforeSendResponse(ResponseInterface $response)
* @return bool
* @throws LocalizedException
*/
protected function shouldAddLinkHeader(HttpResponse $response)
private function shouldAddLinkHeader(HttpResponse $response)
{
if (!$this->scopeConfig->getValue('dev/debug/server_push', ScopeInterface::SCOPE_STORE)) {
return false;
}

if ($this->appState->getAreaCode() !== 'frontend') {
if (!$this->config->enabled()) {
return false;
}

Expand All @@ -110,8 +97,8 @@ protected function shouldAddLinkHeader(HttpResponse $response)
return false;
}

if ($cookieValue = (int)$this->cookieManager->getCookie('serverpush')) {
if ($cookieValue === 1) {
if ($this->config->useCookie()) {
if ((int)$this->cookieManager->getCookie('serverpush') === 1) {
return false;
}
}
Expand All @@ -126,7 +113,7 @@ protected function shouldAddLinkHeader(HttpResponse $response)
*
* @throws NoSuchEntityException
*/
protected function addLinkHeader(HttpResponse $response)
private function addLinkHeader(HttpResponse $response)
{
$values = [];
$crawler = new Crawler($response->getContent());
Expand Down Expand Up @@ -171,7 +158,7 @@ protected function addLinkHeader(HttpResponse $response)
* @return string
* @throws NoSuchEntityException
*/
protected function prepareLink(string $link): string
private function prepareLink(string $link): string
{
if (empty($link)) {
return '';
Expand Down

0 comments on commit d16d7d0

Please sign in to comment.