Skip to content

Commit 60ecd72

Browse files
General refactor following Reviewscouk#49
1 parent 1dab5ca commit 60ecd72

22 files changed

+593
-561
lines changed

1

Lines changed: 0 additions & 6 deletions
This file was deleted.

Block/Product/CustomRatingSnippet.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44

55
use Magento\Framework\View\Element\Template;
66

7-
class CustomRatingSnippet extends \Reviewscouk\Reviews\Block\Product\RatingSnippet
7+
class CustomRatingSnippet extends RatingSnippet
88
{
99
/**
10-
* Get the template for the block based on the admin setting
11-
*
1210
* @return string
1311
*/
1412
public function getTemplate()
1513
{
1614
$isEnabled = $this->_scopeConfig->isSetFlag('reviewscouk_reviews_onpage/widget/category_rating_snippet_widget_enabled');
17-
15+
1816
if ($isEnabled) {
1917
return 'Reviewscouk_Reviews::category/list.phtml';
2018
}

Block/Product/RatingSnippet.php

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,41 @@
22

33
namespace Reviewscouk\Reviews\Block\Product;
44

5+
use Magento\Catalog\Api\CategoryRepositoryInterface;
6+
use Magento\Catalog\Block\Product\Context;
57
use Magento\Catalog\Block\Product\ListProduct;
6-
use Framework\Registry as Registry;
7-
use Magento\Framework\Escaper as Escaper;
8-
// use Reviewscouk\Reviews\Helper\Config as ReviewsConfig;
9-
use Magento\Framework as Framework;
8+
use Magento\Catalog\Model\CategoryFactory;
9+
use Magento\Catalog\Model\Layer\Resolver;
10+
use Magento\Customer\Model\Session;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Data\Helper\PostHelper;
13+
use Magento\Framework\Escaper;
14+
use Magento\Framework\Url\Helper\Data;
1015
use Magento\Store\Model\StoreManagerInterface;
11-
use Magento\Framework\App\Helper\Context as ContextHelper;
12-
use \Reviewscouk\Reviews\Helper\Config as ReviewsConfig;
13-
16+
use Reviewscouk\Reviews\Helper\Config as ReviewsConfig;
1417

1518
class RatingSnippet extends ListProduct
1619
{
1720
protected $_customerSession;
1821
protected $categoryFactory;
19-
//protected $reviewsConfig = Reviews\Helper\Config;
20-
// protected $_storeManager = StoreManagerInterface::class;
2122
protected $store;
2223
protected $reviewsConfig;
23-
/**
24-
* ListProduct constructor.
25-
* @param \Magento\Catalog\Block\Product\Context $context
26-
* @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
27-
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
28-
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
29-
* @param \Magento\Framework\Url\Helper\Data $urlHelper
30-
* @param Helper $helper
31-
* @param array $data
32-
* @param \Magento\Customer\Model\Session $customerSession
33-
* @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
34-
*/
24+
3525
public function __construct(
36-
\Magento\Catalog\Block\Product\Context $context,
37-
\Magento\Framework\Data\Helper\PostHelper $postDataHelper,
38-
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
39-
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
40-
\Magento\Framework\Url\Helper\Data $urlHelper,
26+
Context $context,
27+
PostHelper $postDataHelper,
28+
Resolver $layerResolver,
29+
CategoryRepositoryInterface $categoryRepository,
30+
Data $urlHelper,
4131
array $data = [],
42-
\Magento\Customer\Model\Session $customerSession = null,
43-
\Magento\Catalog\Model\CategoryFactory $categoryFactory = null,
32+
Session $customerSession = null,
33+
CategoryFactory $categoryFactory = null,
4434
ReviewsConfig $reviewsConfigHelper = null,
4535
StoreManagerInterface $storeManager = null
4636
) {
4737
$this->_customerSession = $customerSession;
4838
$this->categoryFactory = $categoryFactory;
49-
$this->reviewsConfig = $reviewsConfigHelper ?: \Magento\Framework\App\ObjectManager::getInstance()->get(ReviewsConfig::class);
39+
$this->reviewsConfig = $reviewsConfigHelper ?: ObjectManager::getInstance()->get(ReviewsConfig::class);
5040
$this->store = $storeManager;
5141

5242
parent::__construct(
@@ -63,22 +53,24 @@ public function __construct(
6353
private function getProductSkus($product)
6454
{
6555
$sku = $product->getSku();
66-
$type = $product->getTypeID();
56+
$type = (string) $product->getTypeID();
6757

6858
$productSkus = [$sku];
69-
if ($type == 'configurable') {
59+
60+
if ($type === 'configurable') {
7061
$usedProducts = $product->getTypeInstance()->getUsedProducts($product);
7162
foreach ($usedProducts as $usedProduct) {
7263
$productSkus[] = $usedProduct->getSku();
7364
}
7465
}
7566

76-
if ($type == 'grouped') {
67+
if ($type === 'grouped') {
7768
$usedProducts = $product->getTypeInstance()->getAssociatedProducts($product);
7869
foreach ($usedProducts as $usedProduct) {
7970
$productSkus[] = $usedProduct->getSku();
8071
}
8172
}
73+
8274
return $productSkus;
8375
}
8476

@@ -88,9 +80,11 @@ public function getRatingSnippet($product)
8880
$ratingSnippetEnabled = $this->reviewsConfig->isCategoryRatingSnippetWidgetEnabled($this->store);
8981
$skus = $this->getProductSkus($product);
9082
$html = '';
91-
if($ratingSnippetEnabled) {
83+
84+
if ($ratingSnippetEnabled) {
9285
$html = '<div class="ruk_rating_snippet" data-sku="' . $escaper->escapeHtml((!empty($skus) ? implode(';', $skus) : '')) . '"></div>';
9386
}
87+
9488
return $html;
9589
}
9690

Block/Product/Reviewwidget.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
namespace Reviewscouk\Reviews\Block\Product;
44

5-
use Reviewscouk\Reviews as Reviews;
6-
use Magento\Framework as Framework;
5+
use Reviewscouk\Reviews;
6+
use Magento\Framework;
77

88
class Reviewwidget extends Framework\View\Element\Template
99
{
10-
1110
private $configHelper;
1211
private $dataHelper;
1312
private $registry;
@@ -126,7 +125,7 @@ protected function getWidgetColor()
126125
{
127126
$colour = $this->configHelper->getProductWidgetColour($this->store->getId());
128127
// people will sometimes put hash and sometimes they will forgot so we need to check for this error
129-
if (isset($colour) && strpos($colour, '#') === false) {
128+
if (isset($colour) && !str_contains($colour, '#')) {
130129
$colour = '#' . $colour;
131130
}
132131
// checking to see if we hare a valid colour. If not then we change it to reviews default hex colour
@@ -140,9 +139,11 @@ protected function getWidgetURL()
140139
{
141140
$region = $this->configHelper->getRegion($this->store->getId());
142141
$api_url = 'widget.reviews.co.uk';
142+
143143
if ($region == 'US') {
144144
$api_url = 'widget.reviews.io';
145145
}
146+
146147
return $api_url;
147148
}
148149
}

Block/Richsnippet.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Reviewscouk\Reviews\Block;
44

5-
use Magento\Backend as Backend;
6-
use Magento\Directory as Directory;
7-
use Magento\Framework as Framework;
8-
use Reviewscouk\Reviews as Reviews;
5+
use Magento\Backend;
6+
use Magento\Directory;
7+
use Magento\Framework;
8+
use Reviewscouk\Reviews;
99

1010
class Richsnippet extends Framework\View\Element\Template
1111
{
@@ -21,26 +21,24 @@ public function __construct(
2121
Reviews\Helper\Data $dataHelper,
2222
Framework\View\Element\Template\Context $context,
2323
Framework\Registry $registry,
24-
Backend\Block\Template\Context $backend,
25-
2624
Directory\Model\Currency $currency,
2725

2826
array $data = []
2927
) {
3028
parent::__construct($context, $data);
3129

3230
$this->configHelper = $config;
33-
$this->dataHelper = $dataHelper;
34-
$this->registry = $registry;
35-
$this->currency = $currency;
31+
$this->dataHelper = $dataHelper;
32+
$this->registry = $registry;
33+
$this->currency = $currency;
3634

3735
$this->store = $this->_storeManager->getStore();
3836
}
3937

4038
public function autoRichSnippet()
4139
{
4240
$merchant_enabled = $this->configHelper->isMerchantRichSnippetsEnabled($this->store->getId());
43-
$product_enabled = $this->configHelper->isProductRichSnippetsEnabled($this->store->getId());
41+
$product_enabled = $this->configHelper->isProductRichSnippetsEnabled($this->store->getId());
4442

4543
$current_product = $this->registry->registry('current_product');
4644

@@ -56,19 +54,20 @@ public function autoRichSnippet()
5654
}
5755

5856
$product = [
59-
'availability' => $productAvailability,
60-
'price' => $current_product->getFinalPrice(),
61-
'url' => $current_product->getProductUrl(),
62-
'description' => $current_product->getMetaDescription(),
57+
'availability' => $productAvailability,
58+
'price' => $current_product->getFinalPrice(),
59+
'url' => $current_product->getProductUrl(),
60+
'description' => $current_product->getMetaDescription(),
6361
'mpn' => ($current_product->hasData('mpn') ? $current_product->getData('mpn') : $current_product->getSku()),
6462
'priceCurrency' => $this->store->getDefaultCurrencyCode(),
6563
'brand' => ($current_product->hasData('manufacturer') ? $current_product->getAttributeText('manufacturer') : ($current_product->hasData('brand') ? $current_product->getAttributeText('brand') : 'Not Available')),
6664
];
6765

6866
return $this->getRichSnippet($sku, $product);
69-
} else if ($merchant_enabled) {
67+
} elseif ($merchant_enabled) {
7068
return $this->getRichSnippet();
7169
}
70+
7271
return '';
7372
}
7473

@@ -78,9 +77,9 @@ public function getRichSnippet($sku = null, $product = null)
7877
$sku = implode(';', $sku);
7978
}
8079

81-
$region = $this->configHelper->getRegion($this->store->getId());
80+
$region = $this->configHelper->getRegion($this->store->getId());
8281
$storeName = $this->configHelper->getStoreId($this->store->getId());
83-
$url = $region == 'us' ? 'https://widget.reviews.io/rich-snippet/dist.js' : 'https://widget.reviews.co.uk/rich-snippet/dist.js';
82+
$url = $region == 'us' ? 'https://widget.reviews.io/rich-snippet/dist.js' : 'https://widget.reviews.co.uk/rich-snippet/dist.js';
8483

8584
$output = '<script src="' . $url . '"></script>';
8685
$output .= '<script>

0 commit comments

Comments
 (0)