Skip to content

Commit

Permalink
[Refactor] Rename all SalesStatistics entries into Statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafikooo committed Dec 11, 2023
1 parent 0c9f36d commit b749e28
Show file tree
Hide file tree
Showing 32 changed files with 225 additions and 224 deletions.
4 changes: 2 additions & 2 deletions src/Sylius/Behat/Context/Api/Admin/DashboardContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(private ApiClientInterface $client, private Response
*/
public function iBrowseAdministrationDashboardStatistics(): void
{
$this->client->index(Resources::SALES_STATISTICS);
$this->client->index(Resources::STATISTICS);
}

/**
Expand All @@ -40,7 +40,7 @@ public function iBrowseAdministrationDashboardStatistics(): void
*/
public function iBrowseAdministrationDashboardStatisticsForChannel(ChannelInterface $channel): void
{
$this->client->index(Resources::SALES_STATISTICS, ['channelCode' => $channel->getCode()]);
$this->client->index(Resources::STATISTICS, ['channelCode' => $channel->getCode()]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Context/Api/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class Resources

public const CUSTOMERS = 'customers';

public const SALES_STATISTICS = 'sales-statistics';
public const STATISTICS = 'statistics';

public const EXCHANGE_RATES = 'exchange-rates';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace Sylius\Bundle\ApiBundle\Controller;

use Sylius\Bundle\ApiBundle\Query\Admin\GetSalesStatistics;
use Sylius\Component\Core\Sales\ValueObject\SalesPeriod;
use Sylius\Bundle\ApiBundle\Query\Admin\GetStatistics;
use Sylius\Component\Core\DateTime\Period;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -24,7 +24,7 @@
use Symfony\Component\Serializer\SerializerInterface;
use Webmozart\Assert\Assert;

final class GetSalesStatisticsAction
final class GetStatisticsAction
{
public function __construct(private MessageBusInterface $queryBus, private SerializerInterface $serializer)
{
Expand All @@ -38,14 +38,14 @@ public function __invoke(Request $request): Response
return new JsonResponse(['error' => 'Missing channelCode parameter.'], Response::HTTP_BAD_REQUEST);
}

$salesPeriod = new SalesPeriod(
$period = new Period(
new \DateTimeImmutable('first day of january this year 00:00:00'),
new \DateTimeImmutable('last day of december this year 23:59:59'),
\DateInterval::createFromDateString('1 month'),
);

try {
$envelope = $this->queryBus->dispatch(new GetSalesStatistics($salesPeriod, $channelCode));
$envelope = $this->queryBus->dispatch(new GetStatistics($period, $channelCode));
} catch (HandlerFailedException $exception) {
return new JsonResponse(['error' => $exception->getPrevious()->getMessage()], Response::HTTP_BAD_REQUEST);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
use Symfony\Component\HttpFoundation\Response;

/** @experimental */
final class SalesStatisticsModifier implements DocumentationModifierInterface
final class StatisticsModifier implements DocumentationModifierInterface
{
private const PATH = '/admin/sales-statistics';
private const PATH = '/admin/statistics';

public function __construct(private string $apiRoute)
{
Expand All @@ -31,7 +31,7 @@ public function __construct(private string $apiRoute)
public function modify(OpenApi $docs): OpenApi
{
$schemas = $docs->getComponents()->getSchemas();
$schemas['SalesStatistics'] = [
$schemas['Statistics'] = [
'type' => 'object',
'properties' => [
'salesPerPeriod' => [
Expand Down Expand Up @@ -87,25 +87,25 @@ public function modify(OpenApi $docs): OpenApi
private function getPathItem(): PathItem
{
return new PathItem(
ref: 'Sales statistics',
summary: 'Get sales statistics',
ref: 'Statistics',
summary: 'Get statistics',
get: new Operation(
operationId: 'get_sales_statistics',
tags: ['Sales statistics'],
operationId: 'get_statistics',
tags: ['Statistics'],
responses: [
Response::HTTP_OK => [
'description' => 'Sales statistics',
'description' => 'Statistics',
'content' => [
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/SalesStatistics',
'$ref' => '#/components/schemas/Statistics',
],
],
],
],
],
summary: 'Get sales statistics',
description: 'Get sales statistics',
summary: 'Get statistics',
description: 'Get statistics',
parameters: $this->getParameters(),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
namespace Sylius\Bundle\ApiBundle\Query\Admin;

use Sylius\Bundle\ApiBundle\Command\ChannelCodeAwareInterface;
use Sylius\Component\Core\Sales\ValueObject\SalesPeriod;
use Sylius\Component\Core\DateTime\Period;

/** @experimental */
class GetSalesStatistics implements ChannelCodeAwareInterface
class GetStatistics implements ChannelCodeAwareInterface
{
public function __construct(private SalesPeriod $salesPeriod, private ?string $channelCode = null)
public function __construct(private Period $period, private ?string $channelCode = null)
{
}

public function getSalesPeriod(): SalesPeriod
public function getPeriod(): Period
{
return $this->salesPeriod;
return $this->period;
}

public function getChannelCode(): ?string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@

namespace Sylius\Bundle\ApiBundle\QueryHandler\Admin;

use Sylius\Bundle\ApiBundle\Query\Admin\GetSalesStatistics;
use Sylius\Bundle\ApiBundle\Query\Admin\GetStatistics;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Sales\Provider\SalesStatisticsProviderInterface;
use Sylius\Component\Core\Sales\ValueObject\SalesStatistics;
use Sylius\Component\Core\Statistics\Provider\StatisticsProviderInterface;
use Sylius\Component\Core\Statistics\ValueObject\Statistics;

/** @experimental */
final class GetSalesStatisticsHandler
final class GetStatisticsHandler
{
/** @param ChannelRepositoryInterface<ChannelInterface> $channelRepository */
public function __construct(
private SalesStatisticsProviderInterface $salesStatisticsProvider,
private StatisticsProviderInterface $statisticsProvider,
private ChannelRepositoryInterface $channelRepository,
) {
}

public function __invoke(GetSalesStatistics $query): SalesStatistics
public function __invoke(GetStatistics $query): Statistics
{
/** @var ChannelInterface|null $channel */
$channel = $this->channelRepository->findOneByCode($query->getChannelCode());
Expand All @@ -39,6 +39,6 @@ public function __invoke(GetSalesStatistics $query): SalesStatistics
throw new ChannelNotFoundException(sprintf('Channel with code "%s" does not exist.', $query->getChannelCode()));
}

return $this->salesStatisticsProvider->provide($query->getSalesPeriod(), $channel);
return $this->statisticsProvider->provide($query->getPeriod(), $channel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<tag name="sylius.open_api.modifier" />
</service>

<service id="Sylius\Bundle\ApiBundle\OpenApi\Documentation\SalesStatisticsModifier">
<service id="Sylius\Bundle\ApiBundle\OpenApi\Documentation\StatisticsModifier">
<argument>%sylius.security.new_api_route%</argument>
<tag name="sylius.open_api.modifier" />
</service>
Expand Down
6 changes: 3 additions & 3 deletions src/Sylius/Bundle/ApiBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ sylius_api_admin_authentication_token:
path: /admin/authentication-token
methods: ['POST']

sylius_api_admin_sales_statistics:
path: /admin/sales-statistics
sylius_api_admin_statistics:
path: /admin/statistics
methods: ['GET']
defaults:
_controller: Sylius\Bundle\ApiBundle\Controller\GetSalesStatisticsAction
_controller: Sylius\Bundle\ApiBundle\Controller\GetStatisticsAction

sylius_api_shop_authentication_token:
path: /shop/authentication-token
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Bundle/ApiBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
<argument type="service" id="validator" />
</service>

<service id="Sylius\Bundle\ApiBundle\Controller\GetSalesStatisticsAction" public="true">
<service id="Sylius\Bundle\ApiBundle\Controller\GetStatisticsAction" public="true">
<argument type="service" id="sylius.query_bus" />
<argument type="service" id="serializer" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<services>
<defaults public="true" />

<service id="Sylius\Bundle\ApiBundle\QueryHandler\Admin\GetSalesStatisticsHandler">
<argument type="service" id="Sylius\Component\Core\Sales\Provider\SalesStatisticsProviderInterface" />
<service id="Sylius\Bundle\ApiBundle\QueryHandler\Admin\GetStatisticsHandler">
<argument type="service" id="Sylius\Component\Core\Statistics\Provider\StatisticsProviderInterface" />
<argument type="service" id="sylius.repository.channel" />
<tag name="messenger.message_handler" bus="sylius.query_bus" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,49 @@

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\ApiBundle\Query\Admin\GetSalesStatistics;
use Sylius\Bundle\ApiBundle\Query\Admin\GetStatistics;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Sales\Provider\SalesStatisticsProviderInterface;
use Sylius\Component\Core\Sales\ValueObject\SalesPeriod;
use Sylius\Component\Core\Sales\ValueObject\SalesStatistics;
use Sylius\Component\Core\Statistics\Provider\StatisticsProviderInterface;
use Sylius\Component\Core\Statistics\ValueObject\Period;
use Sylius\Component\Core\Statistics\ValueObject\Statistics;

final class GetSalesStatisticsHandlerSpec extends ObjectBehavior
final class GetStatisticsHandlerSpec extends ObjectBehavior
{
function let(SalesStatisticsProviderInterface $salesStatisticsProvider, ChannelRepositoryInterface $channelRepository): void
function let(StatisticsProviderInterface $statisticsProvider, ChannelRepositoryInterface $channelRepository): void
{
$this->beConstructedWith($salesStatisticsProvider, $channelRepository);
$this->beConstructedWith($statisticsProvider, $channelRepository);
}

function it_throws_an_exception_if_channel_has_not_been_found(
SalesStatisticsProviderInterface $salesStatisticsProvider,
StatisticsProviderInterface $statisticsProvider,
ChannelRepositoryInterface $channelRepository,
SalesPeriod $salesPeriod,
Period $period,
): void {
$channelRepository->findOneByCode('NON_EXISTING_CHANNEL_CODE')->willReturn(null);

$salesStatisticsProvider->provide(Argument::cetera())->shouldNotBeCalled();
$statisticsProvider->provide(Argument::cetera())->shouldNotBeCalled();

$this
->shouldThrow(ChannelNotFoundException::class)
->during(
'__invoke',
[new GetSalesStatistics($salesPeriod->getWrappedObject(), 'NON_EXISTING_CHANNEL_CODE')],
[new GetStatistics($period->getWrappedObject(), 'NON_EXISTING_CHANNEL_CODE')],
);
}

function it_provides_sales_statistics_for_given_channel(
SalesStatisticsProviderInterface $salesStatisticsProvider,
StatisticsProviderInterface $statisticsProvider,
ChannelRepositoryInterface $channelRepository,
SalesPeriod $salesPeriod,
Period $period,
ChannelInterface $channel,
SalesStatistics $salesStatistics,
Statistics $statistics,
): void {
$channelRepository->findOneByCode('CHANNEL_CODE')->willReturn($channel);
$salesStatisticsProvider->provide($salesPeriod, $channel)->willReturn($salesStatistics);
$statisticsProvider->provide($period, $channel)->willReturn($statistics);

$this(new GetSalesStatistics($salesPeriod->getWrappedObject(), 'CHANNEL_CODE'))
->shouldReturn($salesStatistics);
$this(new GetStatistics($period->getWrappedObject(), 'CHANNEL_CODE'))
->shouldReturn($statistics);
}
}
46 changes: 23 additions & 23 deletions src/Sylius/Bundle/CoreBundle/Provider/SalesPerPeriodProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
namespace Sylius\Bundle\CoreBundle\Provider;

use Doctrine\ORM\EntityRepository;
use Sylius\Component\Core\DateTime\Period;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\Sales\Mapper\SalesPeriodMapperInterface;
use Sylius\Component\Core\Sales\Provider\SalesPerPeriodProviderInterface;
use Sylius\Component\Core\Sales\ValueObject\SalesPeriod;
use Sylius\Component\Core\Statistics\Mapper\SalesPeriodMapperInterface;
use Sylius\Component\Core\Statistics\Provider\SalesPerPeriodProviderInterface;

final class SalesPerPeriodProvider implements SalesPerPeriodProviderInterface
{
Expand All @@ -28,7 +28,7 @@ public function __construct(private EntityRepository $orderRepository, private S
{
}

public function provide(SalesPeriod $salesPeriod, ChannelInterface $channel): array
public function provide(Period $period, ChannelInterface $channel): array
{
$queryBuilder = $this->orderRepository->createQueryBuilder('o')
->select('SUM(o.total) AS total')
Expand All @@ -38,14 +38,14 @@ public function provide(SalesPeriod $salesPeriod, ChannelInterface $channel): ar
->setParameter('channel', $channel)
;

switch ($salesPeriod->getInterval()) {
switch ($period->getInterval()) {
case 'year':
$queryBuilder
->addSelect('YEAR(o.checkoutCompletedAt) as year')
->groupBy('year')
->andWhere('YEAR(o.checkoutCompletedAt) >= :startYear AND YEAR(o.checkoutCompletedAt) <= :endYear')
->setParameter('startYear', $salesPeriod->getStartDate()->format('Y'))
->setParameter('endYear', $salesPeriod->getEndDate()->format('Y'))
->setParameter('startYear', $period->getStartDate()->format('Y'))
->setParameter('endYear', $period->getEndDate()->format('Y'))
;
$dateFormatter = static function (\DateTimeInterface $date): string {
return $date->format('Y');
Expand All @@ -67,10 +67,10 @@ public function provide(SalesPeriod $salesPeriod, ChannelInterface $channel): ar
'YEAR(o.checkoutCompletedAt) = :endYear AND YEAR(o.checkoutCompletedAt) != :startYear AND MONTH(o.checkoutCompletedAt) <= :endMonth',
'YEAR(o.checkoutCompletedAt) > :startYear AND YEAR(o.checkoutCompletedAt) < :endYear',
))
->setParameter('startYear', $salesPeriod->getStartDate()->format('Y'))
->setParameter('startMonth', $salesPeriod->getStartDate()->format('n'))
->setParameter('endYear', $salesPeriod->getEndDate()->format('Y'))
->setParameter('endMonth', $salesPeriod->getEndDate()->format('n'))
->setParameter('startYear', $period->getStartDate()->format('Y'))
->setParameter('startMonth', $period->getStartDate()->format('n'))
->setParameter('endYear', $period->getEndDate()->format('Y'))
->setParameter('endMonth', $period->getEndDate()->format('n'))
;
$dateFormatter = static function (\DateTimeInterface $date): string {
return $date->format('n.Y');
Expand All @@ -92,10 +92,10 @@ public function provide(SalesPeriod $salesPeriod, ChannelInterface $channel): ar
'YEAR(o.checkoutCompletedAt) = :endYear AND YEAR(o.checkoutCompletedAt) != :startYear AND WEEK(o.checkoutCompletedAt) <= :endWeek',
'YEAR(o.checkoutCompletedAt) > :startYear AND YEAR(o.checkoutCompletedAt) < :endYear',
))
->setParameter('startYear', $salesPeriod->getStartDate()->format('Y'))
->setParameter('startWeek', (ltrim($salesPeriod->getStartDate()->format('W'), '0') ?: '0'))
->setParameter('endYear', $salesPeriod->getEndDate()->format('Y'))
->setParameter('endWeek', (ltrim($salesPeriod->getEndDate()->format('W'), '0') ?: '0'))
->setParameter('startYear', $period->getStartDate()->format('Y'))
->setParameter('startWeek', (ltrim($period->getStartDate()->format('W'), '0') ?: '0'))
->setParameter('endYear', $period->getEndDate()->format('Y'))
->setParameter('endWeek', (ltrim($period->getEndDate()->format('W'), '0') ?: '0'))
;
$dateFormatter = static function (\DateTimeInterface $date): string {
return (ltrim($date->format('W'), '0') ?: '0') . ' ' . $date->format('Y');
Expand Down Expand Up @@ -124,12 +124,12 @@ public function provide(SalesPeriod $salesPeriod, ChannelInterface $channel): ar
'YEAR(o.checkoutCompletedAt) = :endYear AND YEAR(o.checkoutCompletedAt) != :startYear AND MONTH(o.checkoutCompletedAt) < :endMonth',
'YEAR(o.checkoutCompletedAt) > :startYear AND YEAR(o.checkoutCompletedAt) < :endYear',
))
->setParameter('startYear', $salesPeriod->getStartDate()->format('Y'))
->setParameter('startMonth', $salesPeriod->getStartDate()->format('n'))
->setParameter('startDay', $salesPeriod->getStartDate()->format('j'))
->setParameter('endYear', $salesPeriod->getEndDate()->format('Y'))
->setParameter('endMonth', $salesPeriod->getEndDate()->format('n'))
->setParameter('endDay', $salesPeriod->getEndDate()->format('j'))
->setParameter('startYear', $period->getStartDate()->format('Y'))
->setParameter('startMonth', $period->getStartDate()->format('n'))
->setParameter('startDay', $period->getStartDate()->format('j'))
->setParameter('endYear', $period->getEndDate()->format('Y'))
->setParameter('endMonth', $period->getEndDate()->format('n'))
->setParameter('endDay', $period->getEndDate()->format('j'))
;
$dateFormatter = static function (\DateTimeInterface $date): string {
return $date->format('j.n.Y');
Expand All @@ -140,11 +140,11 @@ public function provide(SalesPeriod $salesPeriod, ChannelInterface $channel): ar

break;
default:
throw new \RuntimeException(sprintf('Interval "%s" not supported.', $salesPeriod->getInterval()));
throw new \RuntimeException(sprintf('Interval "%s" not supported.', $period->getInterval()));
}

$ordersTotals = $queryBuilder->getQuery()->getArrayResult();

return $this->salesPeriodMapper->map($salesPeriod, $ordersTotals);
return $this->salesPeriodMapper->map($period, $ordersTotals);
}
}
Loading

0 comments on commit b749e28

Please sign in to comment.