Skip to content

Commit

Permalink
Merge pull request #5460 from magento-tsg-csl3/2.4-develop-pr17
Browse files Browse the repository at this point in the history
[TSG-CSL3] For 2.4 (pr17)
  • Loading branch information
zakdma authored Mar 17, 2020
2 parents 8eb9deb + a544049 commit be49d90
Show file tree
Hide file tree
Showing 34 changed files with 1,356 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MassSchedule
private $logger;

/**
* @var OperationRepository
* @var OperationRepositoryInterface
*/
private $operationRepository;

Expand All @@ -75,7 +75,7 @@ class MassSchedule
* @param AsyncResponseInterfaceFactory $asyncResponseFactory
* @param BulkManagementInterface $bulkManagement
* @param LoggerInterface $logger
* @param OperationRepository $operationRepository
* @param OperationRepositoryInterface $operationRepository
* @param UserContextInterface $userContext
* @param Encryptor $encryptor
*/
Expand All @@ -85,7 +85,7 @@ public function __construct(
AsyncResponseInterfaceFactory $asyncResponseFactory,
BulkManagementInterface $bulkManagement,
LoggerInterface $logger,
OperationRepository $operationRepository,
OperationRepositoryInterface $operationRepository,
UserContextInterface $userContext,
Encryptor $encryptor
) {
Expand Down Expand Up @@ -137,7 +137,7 @@ public function publishMass($topicName, array $entitiesArray, $groupId = null, $
$requestItem = $this->itemStatusInterfaceFactory->create();

try {
$operation = $this->operationRepository->createByTopic($topicName, $entityParams, $groupId);
$operation = $this->operationRepository->create($topicName, $entityParams, $groupId, $key);
$operations[] = $operation;
$requestItem->setId($key);
$requestItem->setStatus(ItemStatusInterface::STATUS_ACCEPTED);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\AsynchronousOperations\Model;

use Magento\AsynchronousOperations\Api\Data\OperationInterface;

/**
* Repository interface to create operation
*/
interface OperationRepositoryInterface
{
/**
* Create operation by topic, parameters and group ID
*
* @param string $topicName
* @param array $entityParams
* format: array(
* '<arg1-name>' => '<arg1-value>',
* '<arg2-name>' => '<arg2-value>',
* )
* @param string $groupId
* @param int $operationId
* @return OperationInterface
*/
public function create($topicName, $entityParams, $groupId, $operationId): OperationInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Magento\AsynchronousOperations\Api\Data\OperationInterface;
use Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory;
use Magento\AsynchronousOperations\Model\OperationRepositoryInterface;
use Magento\Framework\MessageQueue\MessageValidator;
use Magento\Framework\MessageQueue\MessageEncoder;
use Magento\Framework\Serialize\Serializer\Json;
Expand All @@ -18,10 +19,10 @@
/**
* Create operation for list of bulk operations.
*/
class OperationRepository
class OperationRepository implements OperationRepositoryInterface
{
/**
* @var \Magento\AsynchronousOperations\Api\Data\OperationInterfaceFactory
* @var OperationInterfaceFactory
*/
private $operationFactory;

Expand Down Expand Up @@ -67,10 +68,14 @@ public function __construct(
}

/**
* @param $topicName
* @param $entityParams
* @param $groupId
* @return mixed
* Create operation by topic, parameters and group ID
*
* @param string $topicName
* @param array $entityParams
* @param string $groupId
* @return OperationInterface
* @deprecated No longer used.
* @see create()
*/
public function createByTopic($topicName, $entityParams, $groupId)
{
Expand All @@ -91,8 +96,16 @@ public function createByTopic($topicName, $entityParams, $groupId)
],
];

/** @var \Magento\AsynchronousOperations\Api\Data\OperationInterface $operation */
/** @var OperationInterface $operation */
$operation = $this->operationFactory->create($data);
return $this->entityManager->save($operation);
}

/**
* @inheritDoc
*/
public function create($topicName, $entityParams, $groupId, $operationId): OperationInterface
{
return $this->createByTopic($topicName, $entityParams, $groupId);
}
}
1 change: 1 addition & 0 deletions app/code/Magento/AsynchronousOperations/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<preference for="Magento\AsynchronousOperations\Api\Data\BulkOperationsStatusInterface" type="Magento\AsynchronousOperations\Model\BulkStatus\Short" />
<preference for="Magento\AsynchronousOperations\Api\Data\OperationSearchResultsInterface" type="Magento\AsynchronousOperations\Model\OperationSearchResults" />
<preference for="Magento\AsynchronousOperations\Api\OperationRepositoryInterface" type="Magento\AsynchronousOperations\Model\OperationRepository" />
<preference for="Magento\AsynchronousOperations\Model\OperationRepositoryInterface" type="Magento\AsynchronousOperations\Model\ResourceModel\Operation\OperationRepository" />
<type name="Magento\Framework\EntityManager\MetadataPool">
<arguments>
<argument name="metadata" xsi:type="array">
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Catalog/Block/Product/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@ public function getQuantityValidators()
*/
public function getIdentities()
{
$identities = $this->getProduct()->getIdentities();
$product = $this->getProduct();

return $identities;
return $product ? $product->getIdentities() : [];
}

/**
Expand Down
16 changes: 10 additions & 6 deletions app/code/Magento/Catalog/Model/Category/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @api
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @since 101.0.0
*/
class DataProvider extends ModifierPoolDataProvider
Expand Down Expand Up @@ -176,6 +177,10 @@ class DataProvider extends ModifierPoolDataProvider
* @var AuthorizationInterface
*/
private $auth;
/**
* @var Image
*/
private $categoryImage;

/**
* @param string $name
Expand All @@ -196,6 +201,7 @@ class DataProvider extends ModifierPoolDataProvider
* @param ScopeOverriddenValue|null $scopeOverriddenValue
* @param ArrayManager|null $arrayManager
* @param FileInfo|null $fileInfo
* @param Image|null $categoryImage
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -216,7 +222,8 @@ public function __construct(
?ArrayUtils $arrayUtils = null,
ScopeOverriddenValue $scopeOverriddenValue = null,
ArrayManager $arrayManager = null,
FileInfo $fileInfo = null
FileInfo $fileInfo = null,
?Image $categoryImage = null
) {
$this->eavValidationRules = $eavValidationRules;
$this->collection = $categoryCollectionFactory->create();
Expand All @@ -232,6 +239,7 @@ public function __construct(
ObjectManager::getInstance()->get(ScopeOverriddenValue::class);
$this->arrayManager = $arrayManager ?: ObjectManager::getInstance()->get(ArrayManager::class);
$this->fileInfo = $fileInfo ?: ObjectManager::getInstance()->get(FileInfo::class);
$this->categoryImage = $categoryImage ?? ObjectManager::getInstance()->get(Image::class);

parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);
}
Expand Down Expand Up @@ -601,11 +609,7 @@ private function convertValues($category, $categoryData): array
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$categoryData[$attributeCode][0]['name'] = basename($fileName);

if ($this->fileInfo->isBeginsWithMediaDirectoryPath($fileName)) {
$categoryData[$attributeCode][0]['url'] = $fileName;
} else {
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
}
$categoryData[$attributeCode][0]['url'] = $this->categoryImage->getUrl($category, $attributeCode);

$categoryData[$attributeCode][0]['size'] = isset($stat) ? $stat['size'] : 0;
$categoryData[$attributeCode][0]['type'] = $mime;
Expand Down
11 changes: 11 additions & 0 deletions app/code/Magento/Catalog/Model/Category/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,15 @@ private function getMediaDirectoryPathRelativeToBaseDirectoryPath(string $filePa

return $mediaDirectoryRelativeSubpath;
}

/**
* Get file relative path to media directory
*
* @param string $filename
* @return string
*/
public function getRelativePathToMediaDirectory(string $filename): string
{
return $this->getFilePath($filename);
}
}
75 changes: 75 additions & 0 deletions app/code/Magento/Catalog/Model/Category/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Category;

use Magento\Catalog\Model\Category;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Category Image Service
*/
class Image
{
private const ATTRIBUTE_NAME = 'image';
/**
* @var FileInfo
*/
private $fileInfo;
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* Initialize dependencies.
*
* @param FileInfo $fileInfo
* @param StoreManagerInterface $storeManager
*/
public function __construct(
FileInfo $fileInfo,
StoreManagerInterface $storeManager
) {
$this->fileInfo = $fileInfo;
$this->storeManager = $storeManager;
}
/**
* Resolve category image URL
*
* @param Category $category
* @param string $attributeCode
* @return string
* @throws LocalizedException
*/
public function getUrl(Category $category, string $attributeCode = self::ATTRIBUTE_NAME): string
{
$url = '';
$image = $category->getData($attributeCode);
if ($image) {
if (is_string($image)) {
$store = $this->storeManager->getStore();
$mediaBaseUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
if ($this->fileInfo->isBeginsWithMediaDirectoryPath($image)) {
$relativePath = $this->fileInfo->getRelativePathToMediaDirectory($image);
$url = rtrim($mediaBaseUrl, '/') . '/' . ltrim($relativePath, '/');
} elseif (substr($image, 0, 1) !== '/') {
$url = rtrim($mediaBaseUrl, '/') . '/' . ltrim(FileInfo::ENTITY_MEDIA_PATH, '/') . '/' . $image;
} else {
$url = $image;
}
} else {
throw new LocalizedException(
__('Something went wrong while getting the image url.')
);
}
}
return $url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Catalog\Model\Category\Attribute\Backend\Image;
use Magento\Catalog\Model\Category\DataProvider;
use Magento\Catalog\Model\Category\FileInfo;
use Magento\Catalog\Model\Category\Image as CategoryImage;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
Expand Down Expand Up @@ -98,6 +99,11 @@ class DataProviderTest extends TestCase
*/
private $auth;

/**
* @var CategoryImage|MockObject
*/
private $categoryImage;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -155,6 +161,11 @@ protected function setUp()
$this->arrayUtils = $this->getMockBuilder(ArrayUtils::class)
->setMethods(['flatten'])
->disableOriginalConstructor()->getMock();

$this->categoryImage = $this->createPartialMock(
CategoryImage::class,
['getUrl']
);
}

/**
Expand Down Expand Up @@ -185,7 +196,8 @@ private function getModel()
'categoryFactory' => $this->categoryFactory,
'pool' => $this->modifierPool,
'auth' => $this->auth,
'arrayUtils' => $this->arrayUtils
'arrayUtils' => $this->arrayUtils,
'categoryImage' => $this->categoryImage,
]
);

Expand Down Expand Up @@ -324,8 +336,8 @@ public function testGetData()
$categoryMock->expects($this->once())
->method('getAttributes')
->willReturn(['image' => $attributeMock]);
$categoryMock->expects($this->once())
->method('getImageUrl')
$this->categoryImage->expects($this->once())
->method('getUrl')
->willReturn($categoryUrl);

$this->registry->expects($this->once())
Expand Down
Loading

0 comments on commit be49d90

Please sign in to comment.