Skip to content

Commit

Permalink
Merge pull request #3 from spryker-eco/feature/cc-34041/cc-34137-exte…
Browse files Browse the repository at this point in the history
…nd-user-transfer

Expand user
  • Loading branch information
ilyakubanov authored Aug 6, 2024
2 parents d1acbd9 + eb41299 commit a30a781
Show file tree
Hide file tree
Showing 16 changed files with 465 additions and 3 deletions.
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ parameters:
- src/
excludePaths:
- src/SprykerEco/Zed/AmazonQuicksight/Persistence/Propel/Abstract*.php
ignoreErrors:
- '#Call to method .+ on an unknown class .+Transfer#'
- '#Parameter .+ of method .+ has invalid typehint type .+Transfer#'
- '#Return typehint of method .+ has invalid type .+Transfer#'
- '#Instantiated class .+Transfer not found#'
- '#Return typehint of method .+ has invalid type Orm\\.+#'
- '#Call to method .+\(\) on an unknown class Orm\\.+#'
- '#Call to static method create\(\) on an unknown class .+Query.#'
- '#Parameter \$.+ of method .+ has invalid typehint type Orm\\.+#'
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
</transfer>

<transfer name="User">
<property name="idUser" type="int"/>
<property name="quicksightUser" type="QuicksightUser" strict="true"/>
</transfer>

<transfer name="UserCollection" strict="true">
<property name="users" type="User[]" singular="user"/>
</transfer>

</transfers>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace SprykerEco\Zed\AmazonQuicksight\Business;

use Spryker\Zed\Kernel\Business\AbstractBusinessFactory;
use SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpander;
use SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpanderInterface;

/**
* @method \SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig getConfig()
Expand All @@ -16,4 +18,11 @@
*/
class AmazonQuicksightBusinessFactory extends AbstractBusinessFactory
{
/**
* @return \SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpanderInterface
*/
public function createUserExpander(): UserExpanderInterface
{
return new UserExpander($this->getRepository());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace SprykerEco\Zed\AmazonQuicksight\Business;

use Generated\Shared\Transfer\UserCollectionTransfer;
use Spryker\Zed\Kernel\Business\AbstractFacade;

/**
Expand All @@ -16,4 +17,18 @@
*/
class AmazonQuicksightFacade extends AbstractFacade implements AmazonQuicksightFacadeInterface
{
/**
* {@inheritDoc}
*
* @api
*
* @param \Generated\Shared\Transfer\UserCollectionTransfer $userCollectionTransfer
*
* @return \Generated\Shared\Transfer\UserCollectionTransfer
*/
public function expandUserCollectionWithQuicksightUsers(
UserCollectionTransfer $userCollectionTransfer
): UserCollectionTransfer {
return $this->getFactory()->createUserExpander()->expandUserCollectionWithQuicksightUser($userCollectionTransfer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@

namespace SprykerEco\Zed\AmazonQuicksight\Business;

use Generated\Shared\Transfer\UserCollectionTransfer;

interface AmazonQuicksightFacadeInterface
{
/**
* Specification:
* - Requires `UserTransfer.idUser` for each user in `UserCollectionTransfer` to be set.
* - Iterates over `UserCollectionTransfer.users`.
* - Finds Quicksight users by `UserTransfer.idUser` in DB.
* - Populates `UserTransfer.quicksightUser` in collection with found Quicksight users.
*
* @api
*
* @param \Generated\Shared\Transfer\UserCollectionTransfer $userCollectionTransfer
*
* @return \Generated\Shared\Transfer\UserCollectionTransfer
*/
public function expandUserCollectionWithQuicksightUsers(
UserCollectionTransfer $userCollectionTransfer
): UserCollectionTransfer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* MIT License
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerEco\Zed\AmazonQuicksight\Business\Expander;

use Generated\Shared\Transfer\UserCollectionTransfer;
use SprykerEco\Zed\AmazonQuicksight\Persistence\AmazonQuicksightRepositoryInterface;

class UserExpander implements UserExpanderInterface
{
/**
* @var \SprykerEco\Zed\AmazonQuicksight\Persistence\AmazonQuicksightRepositoryInterface
*/
protected AmazonQuicksightRepositoryInterface $amazonQuicksightRepository;

/**
* @param \SprykerEco\Zed\AmazonQuicksight\Persistence\AmazonQuicksightRepositoryInterface $amazonQuicksightRepository
*/
public function __construct(AmazonQuicksightRepositoryInterface $amazonQuicksightRepository)
{
$this->amazonQuicksightRepository = $amazonQuicksightRepository;
}

/**
* @param \Generated\Shared\Transfer\UserCollectionTransfer $userCollectionTransfer
*
* @return \Generated\Shared\Transfer\UserCollectionTransfer
*/
public function expandUserCollectionWithQuicksightUser(
UserCollectionTransfer $userCollectionTransfer
): UserCollectionTransfer {
$userIds = $this->extractUserIds($userCollectionTransfer);
$quicksightUserTransfers = $this->amazonQuicksightRepository->getQuicksightUsersByUserIds($userIds);

if (!$quicksightUserTransfers) {
return $userCollectionTransfer;
}

$quicksightUserTransfersIndexedByIdUser = $this->getQuicksightUserTransfersIndexedByIdUser($quicksightUserTransfers);

foreach ($userCollectionTransfer->getUsers() as $userTransfer) {
$idUser = $userTransfer->getIdUserOrFail();
if (isset($quicksightUserTransfersIndexedByIdUser[$idUser])) {
$userTransfer->setQuicksightUser($quicksightUserTransfersIndexedByIdUser[$idUser]);
}
}

return $userCollectionTransfer;
}

/**
* @param \Generated\Shared\Transfer\UserCollectionTransfer $userCollectionTransfer
*
* @return list<int>
*/
protected function extractUserIds(UserCollectionTransfer $userCollectionTransfer): array
{
$userIds = [];
foreach ($userCollectionTransfer->getUsers() as $userTransfer) {
$userIds[] = $userTransfer->getIdUserOrFail();
}

return $userIds;
}

/**
* @param list<\Generated\Shared\Transfer\QuicksightUserTransfer> $quicksightUserTransfers
*
* @return array<int, \Generated\Shared\Transfer\QuicksightUserTransfer>
*/
protected function getQuicksightUserTransfersIndexedByIdUser(array $quicksightUserTransfers): array
{
$quicksightUserTransfersIndexedByIdUser = [];
foreach ($quicksightUserTransfers as $quicksightUserTransfer) {
$quicksightUserTransfersIndexedByIdUser[$quicksightUserTransfer->getFkUserOrFail()] = $quicksightUserTransfer;
}

return $quicksightUserTransfersIndexedByIdUser;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* MIT License
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerEco\Zed\AmazonQuicksight\Business\Expander;

use Generated\Shared\Transfer\UserCollectionTransfer;

interface UserExpanderInterface
{
/**
* @param \Generated\Shared\Transfer\UserCollectionTransfer $userCollectionTransfer
*
* @return \Generated\Shared\Transfer\UserCollectionTransfer
*/
public function expandUserCollectionWithQuicksightUser(
UserCollectionTransfer $userCollectionTransfer
): UserCollectionTransfer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* MIT License
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerEco\Zed\AmazonQuicksight\Communication\Plugin\User;

use Generated\Shared\Transfer\UserCollectionTransfer;
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
use Spryker\Zed\UserExtension\Dependency\Plugin\UserExpanderPluginInterface;

/**
* @method \SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig getConfig()
* @method \SprykerEco\Zed\AmazonQuicksight\Business\AmazonQuicksightFacadeInterface getFacade()
* @method \SprykerEco\Zed\AmazonQuicksight\Communication\AmazonQuicksightCommunicationFactory getFactory()
*/
class QuicksightUserExpanderPlugin extends AbstractPlugin implements UserExpanderPluginInterface
{
/**
* {@inheritDoc}
* - Iterates over `UserCollectionTransfer.users`.
* - Requires `UserTransfer.idUser` for each user in collection to be set.
* - Finds Quicksight users by `UserTransfer.idUser` in DB.
* - Populates `UserTransfer.quicksightUser` in collection with found Quicksight users.
*
* @api
*
* @param \Generated\Shared\Transfer\UserCollectionTransfer $userCollectionTransfer
*
* @return \Generated\Shared\Transfer\UserCollectionTransfer
*/
public function expand(UserCollectionTransfer $userCollectionTransfer): UserCollectionTransfer
{
return $this->getFacade()->expandUserCollectionWithQuicksightUsers($userCollectionTransfer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

namespace SprykerEco\Zed\AmazonQuicksight\Persistence;

use Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUserQuery;
use Spryker\Zed\Kernel\Persistence\AbstractPersistenceFactory;
use SprykerEco\Zed\AmazonQuicksight\Persistence\Propel\Mapper\QuicksightUserMapper;

/**
* @method \SprykerEco\Zed\AmazonQuicksight\AmazonQuicksightConfig getConfig()
Expand All @@ -16,4 +18,19 @@
*/
class AmazonQuicksightPersistenceFactory extends AbstractPersistenceFactory
{
/**
* @return \SprykerEco\Zed\AmazonQuicksight\Persistence\Propel\Mapper\QuicksightUserMapper
*/
public function createQuicksightUserMapper(): QuicksightUserMapper
{
return new QuicksightUserMapper();
}

/**
* @return \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUserQuery
*/
public function getQuicksightUserQuery(): SpyQuicksightUserQuery
{
return SpyQuicksightUserQuery::create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,24 @@
*/
class AmazonQuicksightRepository extends AbstractRepository implements AmazonQuicksightRepositoryInterface
{
/**
* @param list<int> $userIds
*
* @return list<\Generated\Shared\Transfer\QuicksightUserTransfer>
*/
public function getQuicksightUsersByUserIds(array $userIds): array
{
$quicksightUserEntities = $this->getFactory()
->getQuicksightUserQuery()
->filterByFkUser_In($userIds)
->find();

if (!$quicksightUserEntities->count()) {
return [];
}

return $this->getFactory()
->createQuicksightUserMapper()
->mapQuicksightUserEntitiesToQuicksightUserTransfers($quicksightUserEntities, []);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@

interface AmazonQuicksightRepositoryInterface
{
/**
* @param list<int> $userIds
*
* @return list<\Generated\Shared\Transfer\QuicksightUserTransfer>
*/
public function getQuicksightUsersByUserIds(array $userIds): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* MIT License
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace SprykerEco\Zed\AmazonQuicksight\Persistence\Propel\Mapper;

use Generated\Shared\Transfer\QuicksightUserTransfer;
use Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser;
use Propel\Runtime\Collection\Collection;

class QuicksightUserMapper
{
/**
* @param \Propel\Runtime\Collection\Collection<\Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser> $quicksightUserEntities
* @param list<\Generated\Shared\Transfer\QuicksightUserTransfer> $quicksightUserTransfers
*
* @return list<\Generated\Shared\Transfer\QuicksightUserTransfer>
*/
public function mapQuicksightUserEntitiesToQuicksightUserTransfers(
Collection $quicksightUserEntities,
array $quicksightUserTransfers
): array {
foreach ($quicksightUserEntities as $quicksightUserEntity) {
$quicksightUserTransfers[] = $this->mapQuicksightUserEntityToQuicksightUserTransfer(
$quicksightUserEntity,
new QuicksightUserTransfer(),
);
}

return $quicksightUserTransfers;
}

/**
* @param \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser $quicksightUserEntity
* @param \Generated\Shared\Transfer\QuicksightUserTransfer $quicksightUserTransfer
*
* @return \Generated\Shared\Transfer\QuicksightUserTransfer
*/
protected function mapQuicksightUserEntityToQuicksightUserTransfer(
SpyQuicksightUser $quicksightUserEntity,
QuicksightUserTransfer $quicksightUserTransfer
): QuicksightUserTransfer {
return $quicksightUserTransfer->fromArray($quicksightUserEntity->toArray(), true);
}
}
Loading

0 comments on commit a30a781

Please sign in to comment.