Skip to content

Commit

Permalink
Merge branch 'feature/cc-34041/dev-quick-sight-mvp' into feature/cc-3…
Browse files Browse the repository at this point in the history
…4041/cc-34136-persist-qs-user
  • Loading branch information
ilyakubanov committed Aug 6, 2024
2 parents d6154a9 + a30a781 commit 78cac83
Show file tree
Hide file tree
Showing 15 changed files with 417 additions and 0 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 @@ -18,6 +18,7 @@
</transfer>

<transfer name="User">
<property name="idUser" type="int"/>
<property name="quicksightUser" type="QuicksightUser" strict="true"/>
<property name="idUser" type="int"/>
<property name="username" type="string"/>
Expand All @@ -42,4 +43,8 @@
<property name="entityIdentifier" type="string"/>
</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 @@ -13,6 +13,8 @@
use SprykerEco\Zed\AmazonQuicksight\Business\ApiClient\AmazonQuicksightApiClientInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Creator\QuicksightUserCreator;
use SprykerEco\Zed\AmazonQuicksight\Business\Creator\QuicksightUserCreatorInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpander;
use SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpanderInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatter;
use SprykerEco\Zed\AmazonQuicksight\Business\Formatter\AmazonQuicksightRequestDataFormatterInterface;
use SprykerEco\Zed\AmazonQuicksight\Business\Mapper\AmazonQuicksightMapper;
Expand All @@ -26,6 +28,14 @@
*/
class AmazonQuicksightBusinessFactory extends AbstractBusinessFactory
{
/**
* @return \SprykerEco\Zed\AmazonQuicksight\Business\Expander\UserExpanderInterface
*/
public function createUserExpander(): UserExpanderInterface
{
return new UserExpander($this->getRepository());
}

/**
* @return \SprykerEco\Zed\AmazonQuicksight\Business\Creator\QuicksightUserCreatorInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace SprykerEco\Zed\AmazonQuicksight\Business;

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

/**
Expand All @@ -17,6 +18,21 @@
*/
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);
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,27 @@
namespace SprykerEco\Zed\AmazonQuicksight\Business;

use Generated\Shared\Transfer\UserCollectionResponseTransfer;
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;

/**
* Specification:
* - Expects `UserCollectionResponseTransfer.users.quicksightUser.role` to be set.
Expand Down
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,6 +7,7 @@

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;

Expand All @@ -24,4 +25,12 @@ 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
Expand Up @@ -9,9 +9,30 @@

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 \Generated\Shared\Transfer\QuicksightUserTransfer $quicksightUserTransfer
* @param \Orm\Zed\AmazonQuicksight\Persistence\SpyQuicksightUser $quicksightUserEntity
Expand Down
Loading

0 comments on commit 78cac83

Please sign in to comment.