Skip to content
This repository was archived by the owner on May 20, 2019. It is now read-only.

Add topic description feature #30

Open
wants to merge 1 commit into
base: 2.3-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions app/code/Magento/AsynchronousOperations/Model/MassSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Magento\AsynchronousOperations\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject\IdentityGeneratorInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\AsynchronousOperations\Api\Data\ItemStatusInterfaceFactory;
Expand Down Expand Up @@ -54,6 +55,11 @@ class MassSchedule
*/
private $operationRepository;

/**
* @var TopicDescriptionPool
*/
private $topicDescriptionPool;

/**
* Initialize dependencies.
*
Expand All @@ -63,37 +69,52 @@ class MassSchedule
* @param BulkManagementInterface $bulkManagement
* @param LoggerInterface $logger
* @param OperationRepository $operationRepository
* @param TopicDescriptionPool $topicDescriptionPool
*/
public function __construct(
IdentityGeneratorInterface $identityService,
ItemStatusInterfaceFactory $itemStatusInterfaceFactory,
AsyncResponseInterfaceFactory $asyncResponseFactory,
BulkManagementInterface $bulkManagement,
LoggerInterface $logger,
OperationRepository $operationRepository
OperationRepository $operationRepository,
TopicDescriptionPool $topicDescriptionPool = null
) {
$this->identityService = $identityService;
$this->itemStatusInterfaceFactory = $itemStatusInterfaceFactory;
$this->asyncResponseFactory = $asyncResponseFactory;
$this->bulkManagement = $bulkManagement;
$this->logger = $logger;
$this->operationRepository = $operationRepository;
$this->topicDescriptionPool = $topicDescriptionPool ?:
ObjectManager::getInstance()->get(TopicDescriptionPool::class);
}

/**
* Schedule new bulk operation based on the list of entities
*
* @param $topicName
* @param $entitiesArray
* @param null $groupId
* @param null $userId
* @param string $topicName
* @param array $entitiesArray
* @param int|null $groupId
* @param int|null $userId
* @param string|null $bulkDescription
* @return AsyncResponseInterface
* @throws BulkException
* @throws LocalizedException
*/
public function publishMass($topicName, array $entitiesArray, $groupId = null, $userId = null)
{
$bulkDescription = __('Topic %1', $topicName);
public function publishMass(
$topicName,
array $entitiesArray,
$groupId = null,
$userId = null,
$bulkDescription = null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really have a strong reason to change method arguments list or we may delay them till the moment when we will have some code that passes this argument?

) {
if ($bulkDescription === null) {
$bulkDescription = __($this->topicDescriptionPool->getDescription($topicName));
}
if ($bulkDescription === null) {
$bulkDescription = __('Topic %1', $topicName);
}

if ($groupId == null) {
$groupId = $this->identityService->generateId();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\AsynchronousOperations\Model;

/**
* Bulk operations description pool
*/
class TopicDescriptionPool
{
/**
* @var array
*/
private $descriptions;

/**
* BulkDescriptionPool constructor
*
* @param array $descriptions
*/
public function __construct(
$descriptions = []
) {
$this->descriptions = $descriptions;
}

/**
* Get description for a given topic name
*
* @param string $topicName
* @return string|null
*/
public function getDescription($topicName)
{
if (isset($this->descriptions[$topicName])) {
return $this->descriptions[$topicName];
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\AsynchronousOperations\Test\Unit\Model;

use Magento\AsynchronousOperations\Model\TopicDescriptionPool;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

/**
* Class StatusMapperTest
*/
class TopicDescriptionPoolTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\AsynchronousOperations\Model\TopicDescriptionPool
*/
private $topicDescriptionPool;

/**
* @var array
*/
private $testTopics = [
'async.V1.test.topic1' => 'Test topic 1',
'async.V1.test.topic2' => 'Test topic 2',
'async.V1.test.topic3' => 'Test topic 3',
];

protected function setUp()
{
$this->topicDescriptionPool = (new ObjectManager($this))->getObject(
TopicDescriptionPool::class,
[
'descriptions' => $this->testTopics
]
);
}

public function testGetDescriptionOnExistingValues()
{
foreach ($this->testTopics as $topic => $description) {
self::assertEquals($description, $this->topicDescriptionPool->getDescription($topic));
}
}

public function testGetDescriptionOnNonExistingValue()
{
self::assertNull($this->topicDescriptionPool->getDescription('non.existing.topic'));
}
}