Skip to content

Commit ae25fd2

Browse files
author
David Verholen
committed
implement code review change requests
1 parent 3b9261b commit ae25fd2

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

app/code/Magento/Catalog/Block/Product/View/Gallery.php

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,26 @@
1111
*/
1212
namespace Magento\Catalog\Block\Product\View;
1313

14+
use Magento\Catalog\Block\Product\Context;
1415
use Magento\Catalog\Helper\Image;
1516
use Magento\Catalog\Model\Product;
17+
use Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface;
1618
use Magento\Framework\Data\Collection;
1719
use Magento\Framework\DataObject;
20+
use Magento\Framework\App\ObjectManager;
21+
use Magento\Framework\Json\EncoderInterface;
22+
use Magento\Framework\Serialize\Serializer\Json;
23+
use Magento\Framework\Stdlib\ArrayUtils;
1824

19-
class Gallery extends \Magento\Catalog\Block\Product\View\AbstractView
25+
class Gallery extends AbstractView
2026
{
2127
/**
2228
* @var \Magento\Framework\Config\View
2329
*/
2430
protected $configView;
2531

2632
/**
27-
* @var \Magento\Framework\Json\EncoderInterface
33+
* @var EncoderInterface
2834
*/
2935
protected $jsonEncoder;
3036

@@ -34,24 +40,30 @@ class Gallery extends \Magento\Catalog\Block\Product\View\AbstractView
3440
protected $galleryImagesConfig;
3541

3642
/**
37-
* @param \Magento\Catalog\Block\Product\Context $context
38-
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
39-
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
40-
* @param \Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface $imagesConfigFactory
41-
* @param array $galleryImagesConfig
42-
* @param array $data
43+
* @var ImagesConfigFactoryInterface
44+
*/
45+
private $galleryImagesConfigFactory;
46+
47+
/**
48+
* @param Context $context
49+
* @param ArrayUtils $arrayUtils
50+
* @param EncoderInterface $jsonEncoder
51+
* @param ImagesConfigFactoryInterface $imagesConfigFactory
52+
* @param array $galleryImagesConfig
53+
* @param array $data
4354
*/
4455
public function __construct(
45-
\Magento\Catalog\Block\Product\Context $context,
46-
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
47-
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
48-
\Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface $imagesConfigFactory,
49-
array $galleryImagesConfig = [],
50-
array $data = []
56+
Context $context,
57+
ArrayUtils $arrayUtils,
58+
EncoderInterface $jsonEncoder,
59+
array $data = [],
60+
ImagesConfigFactoryInterface $imagesConfigFactory = null,
61+
array $galleryImagesConfig = []
5162
) {
52-
$this->jsonEncoder = $jsonEncoder;
5363
parent::__construct($context, $arrayUtils, $data);
54-
$this->galleryImagesConfig = $imagesConfigFactory->create($galleryImagesConfig);
64+
$this->jsonEncoder = $jsonEncoder ?: ObjectManager::getInstance()->get(Json::class);
65+
$this->galleryImagesConfigFactory = $imagesConfigFactory ?: ObjectManager::getInstance()->get(ImagesConfigFactoryInterface::class);
66+
$this->galleryImagesConfig = $galleryImagesConfig;
5567
}
5668

5769
/**
@@ -65,7 +77,7 @@ public function getGalleryImages()
6577
$images = $product->getMediaGalleryImages();
6678
if ($images instanceof \Magento\Framework\Data\Collection) {
6779
foreach ($images as $image) {
68-
foreach($this->galleryImagesConfig->getItems() as $imageConfig) {
80+
foreach($this->getGalleryImagesConfig()->getItems() as $imageConfig) {
6981
/** @var Product $product */
7082
$image->setData(
7183
$imageConfig->getData('data_object_key'),
@@ -116,7 +128,7 @@ public function getGalleryImagesJson()
116128
'position' => $image->getData('position'),
117129
'isMain' => $this->isMainImage($image),
118130
]);
119-
foreach($this->galleryImagesConfig->getItems() as $imageConfig) {
131+
foreach($this->getGalleryImagesConfig()->getItems() as $imageConfig) {
120132
$imageItem->setData(
121133
$imageConfig->getData('json_object_key'),
122134
$image->getData($imageConfig->getData('data_object_key'))
@@ -189,4 +201,16 @@ private function getConfigView()
189201
}
190202
return $this->configView;
191203
}
204+
205+
/**
206+
* @return Collection
207+
*/
208+
private function getGalleryImagesConfig()
209+
{
210+
if (false === $this->hasData('gallery_images_config')) {
211+
$this->setData('gallery_images_config', $this->galleryImagesConfigFactory->create($this->galleryImagesConfig));
212+
}
213+
214+
return $this->getData('gallery_images_config');
215+
}
192216
}

app/code/Magento/Catalog/Model/Product/Gallery/ImagesConfigFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ class ImagesConfigFactory implements ImagesConfigFactoryInterface
1414
/**
1515
* @var \Magento\Framework\Data\CollectionFactory
1616
*/
17-
protected $_dataCollectionFactory;
17+
protected $dataCollectionFactory;
1818

1919
/**
2020
* ImagesConfigFactory constructor.
2121
*
22-
* @param CollectionFactory $_dataCollectionFactory
22+
* @param CollectionFactory $dataCollectionFactory
2323
*/
24-
public function __construct(CollectionFactory $_dataCollectionFactory)
24+
public function __construct(CollectionFactory $dataCollectionFactory)
2525
{
26-
$this->_dataCollectionFactory = $_dataCollectionFactory;
26+
$this->dataCollectionFactory = $dataCollectionFactory;
2727
}
2828

2929
/**
@@ -37,7 +37,7 @@ public function __construct(CollectionFactory $_dataCollectionFactory)
3737
public function create(array $imagesConfig, array $data = [])
3838
{
3939
/** @var \Magento\Framework\Data\Collection $collection */
40-
$collection = $this->_dataCollectionFactory->create($data);
40+
$collection = $this->dataCollectionFactory->create($data);
4141
array_map(function($imageConfig) use ($collection) {
4242
$collection->addItem(new DataObject($imageConfig));
4343
}, $imagesConfig);

app/code/Magento/ProductVideo/Block/Product/View/Gallery.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function __construct(
3232
\Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
3333
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
3434
\Magento\ProductVideo\Helper\Media $mediaHelper,
35-
\Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface $imagesConfigFactory,
36-
array $galleryImagesConfig = [],
37-
array $data = []
35+
array $data = [],
36+
\Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface $imagesConfigFactory = null,
37+
array $galleryImagesConfig = []
3838
) {
3939
parent::__construct(
4040
$context,

0 commit comments

Comments
 (0)