Skip to content

Commit 9e0fb4b

Browse files
author
Vitaliy Boyko
committed
graphQl-239: refactoring, adjusting tests
1 parent edcf04c commit 9e0fb4b

File tree

4 files changed

+105
-61
lines changed

4 files changed

+105
-61
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductImage/Url.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,18 @@ class Url implements ResolverInterface
2727
/**
2828
* @var PlaceholderProvider
2929
*/
30-
private $_placeholderProvider;
30+
private $placeholderProvider;
3131

3232
/**
33-
* Url constructor.
3433
* @param ImageFactory $productImageFactory
3534
* @param PlaceholderProvider $placeholderProvider
3635
*/
3736
public function __construct(
3837
ImageFactory $productImageFactory,
3938
PlaceholderProvider $placeholderProvider
40-
4139
) {
4240
$this->productImageFactory = $productImageFactory;
43-
$this->_placeholderProvider = $placeholderProvider;
41+
$this->placeholderProvider = $placeholderProvider;
4442
}
4543

4644
/**
@@ -83,7 +81,7 @@ private function getImageUrl(string $imageType, ?string $imagePath): string
8381
->setBaseFile($imagePath);
8482

8583
if ($image->isBaseFilePlaceholder()) {
86-
return $this->_placeholderProvider->getPlaceholder($imageType);
84+
return $this->placeholderProvider->getPlaceholder($imageType);
8785
}
8886

8987
return $image->getUrl();

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/Image/Placeholder.php

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,102 +8,64 @@
88
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Image;
99

1010
use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
11-
use Magento\Framework\App\Config\ScopeConfigInterface;
1211
use Magento\Framework\View\Asset\Repository as AssetRepository;
13-
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
14-
use Magento\Store\Model\StoreManagerInterface;
1512

1613
/**
17-
* Class Placeholder
18-
* @package Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Image
14+
* Image Placeholder provider
1915
*/
2016
class Placeholder
2117
{
2218
/**
2319
* @var PlaceholderFactory
2420
*/
25-
private $_placeholderFactory;
21+
private $placeholderFactory;
22+
2623
/**
2724
* @var AssetRepository
2825
*/
29-
private $_assetRepository;
30-
/**
31-
* @var ScopeConfigInterface
32-
*/
33-
private $_scopeConfig;
34-
/**
35-
* @var StoreManagerInterface
36-
*/
37-
private $_storeManager;
26+
private $assetRepository;
27+
3828
/**
39-
* @var ThemeProviderInterface
29+
* @var Theme
4030
*/
41-
private $_themeProvider;
31+
private $theme;
4232

4333
/**
4434
* Placeholder constructor.
4535
* @param PlaceholderFactory $placeholderFactory
4636
* @param AssetRepository $assetRepository
47-
* @param ScopeConfigInterface $scopeConfig
48-
* @param StoreManagerInterface $storeManager
49-
* @param ThemeProviderInterface $themeProvider
37+
* @param Theme $theme
5038
*/
5139
public function __construct(
5240
PlaceholderFactory $placeholderFactory,
5341
AssetRepository $assetRepository,
54-
ScopeConfigInterface $scopeConfig,
55-
StoreManagerInterface $storeManager,
56-
ThemeProviderInterface $themeProvider
42+
Theme $theme
5743
) {
58-
$this->_placeholderFactory = $placeholderFactory;
59-
$this->_assetRepository = $assetRepository;
60-
$this->_scopeConfig = $scopeConfig;
61-
$this->_storeManager = $storeManager;
62-
$this->_themeProvider = $themeProvider;
44+
$this->placeholderFactory = $placeholderFactory;
45+
$this->assetRepository = $assetRepository;
46+
$this->theme = $theme;
6347
}
6448

6549
/**
6650
* Get placeholder
6751
*
68-
* @param $imageType
52+
* @param string $imageType
6953
* @return string
7054
* @throws \Magento\Framework\Exception\NoSuchEntityException
7155
*/
72-
public function getPlaceholder($imageType): string
56+
public function getPlaceholder(string $imageType): string
7357
{
74-
$imageAsset = $this->_placeholderFactory->create(['type' => $imageType]);
58+
$imageAsset = $this->placeholderFactory->create(['type' => $imageType]);
7559

7660
// check if placeholder defined in config
7761
if ($imageAsset->getFilePath()) {
7862
return $imageAsset->getUrl();
7963
}
8064

81-
return $this->_assetRepository->createAsset(
65+
$themeData = $this->theme->getThemeData();
66+
return $this->assetRepository->createAsset(
8267
"Magento_Catalog::images/product/placeholder/{$imageType}.jpg",
83-
$this->getThemeData()
68+
$themeData
8469
)->getUrl();
8570
}
86-
87-
/**
88-
* Get theme model
89-
*
90-
* @return mixed
91-
* @throws \Magento\Framework\Exception\NoSuchEntityException
92-
*/
93-
public function getThemeData()
94-
{
95-
$themeId = $this->_scopeConfig->getValue(
96-
\Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID,
97-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
98-
$this->_storeManager->getStore()->getId()
99-
);
100-
101-
/** @var $theme \Magento\Framework\View\Design\ThemeInterface */
102-
$theme = $this->_themeProvider->getThemeById($themeId);
103-
104-
$data = $theme->getData();
105-
$data['themeModel'] = $theme;
106-
107-
return $data;
108-
}
10971
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Image;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
/**
15+
* Theme provider
16+
*/
17+
class Theme
18+
{
19+
/**
20+
* @var ScopeConfigInterface
21+
*/
22+
private $scopeConfig;
23+
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
29+
/**
30+
* @var ThemeProviderInterface
31+
*/
32+
private $themeProvider;
33+
34+
/**
35+
* @param ScopeConfigInterface $scopeConfig
36+
* @param StoreManagerInterface $storeManager
37+
* @param ThemeProviderInterface $themeProvider
38+
*/
39+
public function __construct(
40+
ScopeConfigInterface $scopeConfig,
41+
StoreManagerInterface $storeManager,
42+
ThemeProviderInterface $themeProvider
43+
) {
44+
$this->scopeConfig = $scopeConfig;
45+
$this->storeManager = $storeManager;
46+
$this->themeProvider = $themeProvider;
47+
}
48+
49+
/**
50+
* Get theme model
51+
*
52+
* @return array
53+
* @throws \Magento\Framework\Exception\NoSuchEntityException
54+
*/
55+
public function getThemeData(): array
56+
{
57+
$themeId = $this->scopeConfig->getValue(
58+
\Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID,
59+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
60+
$this->storeManager->getStore()->getId()
61+
);
62+
63+
/** @var $theme \Magento\Framework\View\Design\ThemeInterface */
64+
$theme = $this->themeProvider->getThemeById($themeId);
65+
66+
$data = $theme->getData();
67+
$data['themeModel'] = $theme;
68+
69+
return $data;
70+
}
71+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductImageTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,25 @@ public function testProductWithoutBaseImage()
6060
url
6161
label
6262
}
63+
small_image {
64+
url
65+
label
66+
}
6367
}
6468
}
6569
}
6670
QUERY;
6771
$response = $this->graphQlQuery($query);
6872
self::assertEquals('Simple Product', $response['products']['items'][0]['image']['label']);
73+
self::assertStringEndsWith(
74+
'images/product/placeholder/image.jpg',
75+
$response['products']['items'][0]['image']['url']
76+
);
77+
self::assertEquals('Simple Product', $response['products']['items'][0]['small_image']['label']);
78+
self::assertStringEndsWith(
79+
'images/product/placeholder/small_image.jpg',
80+
$response['products']['items'][0]['small_image']['url']
81+
);
6982
}
7083

7184
/**

0 commit comments

Comments
 (0)