Skip to content

Commit

Permalink
Add DefaultDateProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
mpysiak committed Jun 27, 2024
1 parent 19bc7b8 commit 20e84a4
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 25 deletions.
3 changes: 3 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
</service>
<service id="Sylius\GmvBundle\Provider\GmvProviderInterface" alias="sylius_gmv.provider.gmv" />

<service id="sylius_gmv.provider.default_date" class="Sylius\GmvBundle\Provider\DefaultDateProvider" />
<service id="Sylius\GmvBundle\Provider\DefaultDateProviderInterface" alias="sylius_gmv.provider.default_date" />

<service id="sylius_gmv.command.gmv" class="Sylius\GmvBundle\Command\GmvCommand">
<argument type="service" id="sylius_gmv.validator.input_parameters" />
<argument type="service" id="sylius_gmv.parser.date" />
Expand Down
6 changes: 4 additions & 2 deletions src/Command/GmvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sylius\GmvBundle\Command;

use Sylius\GmvBundle\Parser\DateParserInterface;
use Sylius\GmvBundle\Provider\DefaultDateProviderInterface;
use Sylius\GmvBundle\Provider\GmvProviderInterface;
use Sylius\GmvBundle\Validator\InputParametersValidatorInterface;
use Symfony\Component\Console\Attribute\AsCommand;
Expand All @@ -33,6 +34,7 @@ public function __construct(
private readonly InputParametersValidatorInterface $validator,
private readonly DateParserInterface $dateParser,
private readonly GmvProviderInterface $gmvProvider,
private readonly DefaultDateProviderInterface $defaultDateProvider,
) {
parent::__construct();
}
Expand All @@ -44,13 +46,13 @@ protected function configure(): void
'periodStart',
InputArgument::OPTIONAL,
'The start of the period (e.g., 05/2024)',
$this->dateParser->getDefaultStartDate()->format('m/Y'),
$this->defaultDateProvider->getDefaultStartDate()->format('m/Y'),
)
->addArgument(
'periodEnd',
InputArgument::OPTIONAL,
'The end of the period (e.g., 06/2024)',
$this->dateParser->getDefaultEndDate()->format('m/Y'),
$this->defaultDateProvider->getDefaultEndDate()->format('m/Y'),
);
}

Expand Down
16 changes: 0 additions & 16 deletions src/Parser/DateParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,4 @@ public function parseEndOfMonth(string $date): \DateTime

return $dateTime->modify('last day of this month 23:59:59');
}

public function getDefaultStartDate(): \DateTime
{
$now = new \DateTime();

return $now
->modify('first day of -12 months');
}

public function getDefaultEndDate(): \DateTime
{
$now = new \DateTime();

return $now
->modify('last day of last month');
}
}
4 changes: 0 additions & 4 deletions src/Parser/DateParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@ interface DateParserInterface
public function parseStartOfMonth(string $date): \DateTime;

public function parseEndOfMonth(string $date): \DateTime;

public function getDefaultStartDate(): \DateTime;

public function getDefaultEndDate(): \DateTime;
}
33 changes: 33 additions & 0 deletions src/Provider/DefaultDateProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\GmvBundle\Provider;

final class DefaultDateProvider implements DefaultDateProviderInterface
{
public function getDefaultStartDate(): \DateTime
{
$now = new \DateTime();

return $now
->modify('first day of -12 months');
}

public function getDefaultEndDate(): \DateTime
{
$now = new \DateTime();

return $now
->modify('last day of last month');
}
}
21 changes: 21 additions & 0 deletions src/Provider/DefaultDateProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\GmvBundle\Provider;

interface DefaultDateProviderInterface
{
public function getDefaultStartDate(): \DateTime;

public function getDefaultEndDate(): \DateTime;
}
10 changes: 7 additions & 3 deletions tests/Unit/GmvCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPUnit\Framework\TestCase;
use Sylius\GmvBundle\Command\GmvCommand;
use Sylius\GmvBundle\Parser\DateParserInterface;
use Sylius\GmvBundle\Provider\DefaultDateProviderInterface;
use Sylius\GmvBundle\Provider\GmvProviderInterface;
use Sylius\GmvBundle\Validator\InputParametersValidatorInterface;
use Symfony\Component\Console\Application;
Expand All @@ -31,13 +32,16 @@ class GmvCommandTest extends TestCase

private CommandTester $commandTester;

private DefaultDateProviderInterface $defaultDateProvider;

protected function setUp(): void
{
$this->validator = $this->createMock(InputParametersValidatorInterface::class);
$this->dateParser = $this->createMock(DateParserInterface::class);
$this->gmvProvider = $this->createMock(GmvProviderInterface::class);
$this->defaultDateProvider = $this->createMock(DefaultDateProviderInterface::class);

$command = new GmvCommand($this->validator, $this->dateParser, $this->gmvProvider);
$command = new GmvCommand($this->validator, $this->dateParser, $this->gmvProvider, $this->defaultDateProvider);

$application = new Application();
$application->add($command);
Expand All @@ -51,11 +55,11 @@ private function mockDefaultValues(): void
$defaultStartDate = new \DateTime('first day of last month');
$defaultEndDate = new \DateTime('last day of last month');

$this->dateParser
$this->defaultDateProvider
->method('getDefaultStartDate')
->willReturn($defaultStartDate);

$this->dateParser
$this->defaultDateProvider
->method('getDefaultEndDate')
->willReturn($defaultEndDate);

Expand Down

0 comments on commit 20e84a4

Please sign in to comment.