Skip to content

Commit

Permalink
Add Contact- and AccountDataProviderResolver (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-rath authored Sep 17, 2022
1 parent 7899841 commit 7a7f69b
Show file tree
Hide file tree
Showing 5 changed files with 470 additions and 0 deletions.
98 changes: 98 additions & 0 deletions Content/DataProviderResolver/AccountDataProviderResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\HeadlessBundle\Content\DataProviderResolver;

use JMS\Serializer\SerializationContext;
use Sulu\Bundle\ContactBundle\Api\Account;
use Sulu\Bundle\ContactBundle\Entity\Account as AccountEntity;
use Sulu\Bundle\HeadlessBundle\Content\Serializer\AccountSerializerInterface;
use Sulu\Component\Content\Compat\PropertyParameter;
use Sulu\Component\SmartContent\Configuration\ProviderConfigurationInterface;
use Sulu\Component\SmartContent\DataProviderInterface;

class AccountDataProviderResolver implements DataProviderResolverInterface
{
public static function getDataProvider(): string
{
return 'accounts';
}

/**
* @var DataProviderInterface
*/
private $accountDataProvider;

/**
* @var AccountSerializerInterface
*/
private $accountSerializer;

public function __construct(
DataProviderInterface $accountDataProvider,
AccountSerializerInterface $accountSerializer
) {
$this->accountDataProvider = $accountDataProvider;
$this->accountSerializer = $accountSerializer;
}

public function getProviderConfiguration(): ProviderConfigurationInterface
{
return $this->accountDataProvider->getConfiguration();
}

/**
* @return PropertyParameter[]
*/
public function getProviderDefaultParams(): array
{
return $this->accountDataProvider->getDefaultPropertyParameter();
}

public function resolve(
array $filters,
array $propertyParameters,
array $options = [],
?int $limit = null,
int $page = 1,
?int $pageSize = null
): DataProviderResult {
$providerResult = $this->accountDataProvider->resolveResourceItems(
$filters,
$propertyParameters,
$options,
$limit,
$page,
$pageSize
);

/** @var string $locale */
$locale = $options['locale'];

$items = [];
foreach ($providerResult->getItems() as $providerItem) {
/** @var Account $account */
$account = $providerItem->getResource();
/** @var AccountEntity $accountEntity */
$accountEntity = $account->getEntity();

$items[] = $this->accountSerializer->serialize(
$accountEntity,
$locale,
SerializationContext::create()->setGroups(['partialAccount'])
);
}

return new DataProviderResult($items, $providerResult->getHasNextPage());
}
}
98 changes: 98 additions & 0 deletions Content/DataProviderResolver/ContactDataProviderResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\HeadlessBundle\Content\DataProviderResolver;

use JMS\Serializer\SerializationContext;
use Sulu\Bundle\ContactBundle\Api\Contact;
use Sulu\Bundle\ContactBundle\Entity\Contact as ContactEntity;
use Sulu\Bundle\HeadlessBundle\Content\Serializer\ContactSerializerInterface;
use Sulu\Component\Content\Compat\PropertyParameter;
use Sulu\Component\SmartContent\Configuration\ProviderConfigurationInterface;
use Sulu\Component\SmartContent\DataProviderInterface;

class ContactDataProviderResolver implements DataProviderResolverInterface
{
public static function getDataProvider(): string
{
return 'contacts';
}

/**
* @var DataProviderInterface
*/
private $contactDataProvider;

/**
* @var ContactSerializerInterface
*/
private $contactSerializer;

public function __construct(
DataProviderInterface $contactDataProvider,
ContactSerializerInterface $contactSerializer
) {
$this->contactDataProvider = $contactDataProvider;
$this->contactSerializer = $contactSerializer;
}

public function getProviderConfiguration(): ProviderConfigurationInterface
{
return $this->contactDataProvider->getConfiguration();
}

/**
* @return PropertyParameter[]
*/
public function getProviderDefaultParams(): array
{
return $this->contactDataProvider->getDefaultPropertyParameter();
}

public function resolve(
array $filters,
array $propertyParameters,
array $options = [],
?int $limit = null,
int $page = 1,
?int $pageSize = null
): DataProviderResult {
$providerResult = $this->contactDataProvider->resolveResourceItems(
$filters,
$propertyParameters,
$options,
$limit,
$page,
$pageSize
);

/** @var string $locale */
$locale = $options['locale'];

$items = [];
foreach ($providerResult->getItems() as $providerItem) {
/** @var Contact $contact */
$contact = $providerItem->getResource();
/** @var ContactEntity $contactEntity */
$contactEntity = $contact->getEntity();

$items[] = $this->contactSerializer->serialize(
$contactEntity,
$locale,
SerializationContext::create()->setGroups(['partialContact'])
);
}

return new DataProviderResult($items, $providerResult->getHasNextPage());
}
}
20 changes: 20 additions & 0 deletions Resources/config/data-provider-resolvers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,25 @@

<tag name="sulu_headless.data_provider_resolver"/>
</service>

<service
id="sulu_headless.provider_resolver.contact"
class="Sulu\Bundle\HeadlessBundle\Content\DataProviderResolver\ContactDataProviderResolver"
>
<argument type="service" id="sulu_contact.smart_content.data_provider.contact"/>
<argument type="service" id="sulu_headless.serializer.contact"/>

<tag name="sulu_headless.data_provider_resolver"/>
</service>

<service
id="sulu_headless.provider_resolver.account"
class="Sulu\Bundle\HeadlessBundle\Content\DataProviderResolver\AccountDataProviderResolver"
>
<argument type="service" id="sulu_contact.smart_content.data_provider.account"/>
<argument type="service" id="sulu_headless.serializer.account"/>

<tag name="sulu_headless.data_provider_resolver"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\HeadlessBundle\Tests\Unit\Content\DataProviderResolver;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Sulu\Bundle\ContactBundle\Api\Account;
use Sulu\Bundle\ContactBundle\Entity\AccountInterface;
use Sulu\Bundle\HeadlessBundle\Content\DataProviderResolver\AccountDataProviderResolver;
use Sulu\Bundle\HeadlessBundle\Content\Serializer\AccountSerializerInterface;
use Sulu\Component\Contact\SmartContent\AccountDataProvider;
use Sulu\Component\Content\Compat\PropertyParameter;
use Sulu\Component\SmartContent\Configuration\ProviderConfigurationInterface;
use Sulu\Component\SmartContent\DataProviderResult;
use Sulu\Component\SmartContent\ResourceItemInterface;

class AccountDataProviderResolverTest extends TestCase
{
/**
* @var AccountDataProvider|ObjectProphecy
*/
private $accountDataProvider;

/**
* @var AccountSerializerInterface|ObjectProphecy
*/
private $accountSerializer;

/**
* @var AccountDataProviderResolver
*/
private $accountResolver;

protected function setUp(): void
{
$this->accountDataProvider = $this->prophesize(AccountDataProvider::class);
$this->accountSerializer = $this->prophesize(AccountSerializerInterface::class);

$this->accountResolver = new AccountDataProviderResolver(
$this->accountDataProvider->reveal(),
$this->accountSerializer->reveal()
);
}

public function testGetDataProvider(): void
{
self::assertSame('accounts', $this->accountResolver::getDataProvider());
}

public function testGetProviderConfiguration(): void
{
$configuration = $this->prophesize(ProviderConfigurationInterface::class);
$this->accountDataProvider->getConfiguration()->willReturn($configuration->reveal());

$this->assertSame($configuration->reveal(), $this->accountResolver->getProviderConfiguration());
}

public function testGetProviderDefaultParams(): void
{
$propertyParameter = $this->prophesize(PropertyParameter::class);
$this->accountDataProvider->getDefaultPropertyParameter()->willReturn(['test' => $propertyParameter->reveal()]);

$this->assertSame(['test' => $propertyParameter->reveal()], $this->accountResolver->getProviderDefaultParams());
}

public function testResolve(): void
{
$account1 = $this->prophesize(AccountInterface::class);
$apiAccount1 = $this->prophesize(Account::class);
$apiAccount1->getEntity()->willReturn($account1->reveal());

$account2 = $this->prophesize(AccountInterface::class);
$apiAccount2 = $this->prophesize(Account::class);
$apiAccount2->getEntity()->willReturn($account2->reveal());

$resourceItem1 = $this->prophesize(ResourceItemInterface::class);
$resourceItem1->getResource()->willReturn($apiAccount1->reveal());

$resourceItem2 = $this->prophesize(ResourceItemInterface::class);
$resourceItem2->getResource()->willReturn($apiAccount2->reveal());

$providerResult = $this->prophesize(DataProviderResult::class);
$providerResult->getHasNextPage()->willReturn(true);
$providerResult->getItems()->willReturn([$resourceItem1, $resourceItem2]);

$this->accountDataProvider->resolveResourceItems([], [], ['locale' => 'en'], 10, 1, 5)->willReturn($providerResult->reveal());

$this->accountSerializer->serialize($account1, 'en', Argument::cetera())->willReturn([
'id' => 1,
'name' => 'Account 1',
]);

$this->accountSerializer->serialize($account2, 'en', Argument::cetera())->willReturn([
'id' => 2,
'name' => 'Account 2',
]);

$result = $this->accountResolver->resolve([], [], ['locale' => 'en'], 10, 1, 5);

$this->assertTrue($result->getHasNextPage());
$this->assertSame(
[
[
'id' => 1,
'name' => 'Account 1',
],
[
'id' => 2,
'name' => 'Account 2',
],
],
$result->getItems()
);
}
}
Loading

0 comments on commit 7a7f69b

Please sign in to comment.