-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
591 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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 App\Doctrine; | ||
|
||
use Doctrine\DBAL\Platforms\MySQLPlatform; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
use Doctrine\DBAL\Platforms\SqlitePlatform; | ||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\AST\Node; | ||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
use Doctrine\ORM\Query\TokenType; | ||
|
||
final class Day extends FunctionNode | ||
{ | ||
/** @var Node|string|null */ | ||
public $date; | ||
|
||
public function parse(Parser $parser): void | ||
{ | ||
$parser->match(TokenType::T_IDENTIFIER); | ||
$parser->match(TokenType::T_OPEN_PARENTHESIS); | ||
|
||
$this->date = $parser->ArithmeticPrimary(); | ||
|
||
$parser->match(TokenType::T_CLOSE_PARENTHESIS); | ||
} | ||
|
||
public function getSql(SqlWalker $sqlWalker): string | ||
{ | ||
$platform = $sqlWalker->getConnection()->getDatabasePlatform(); | ||
|
||
if ($platform instanceof MySQLPlatform) { | ||
return sprintf('DAY(%s)', $sqlWalker->walkArithmeticPrimary($this->date)); | ||
} | ||
|
||
if ($platform instanceof PostgreSQLPlatform) { | ||
return sprintf('EXTRACT(DAY FROM %s)', $sqlWalker->walkArithmeticPrimary($this->date)); | ||
} | ||
|
||
if ($platform instanceof SqlitePlatform) { | ||
return sprintf('CAST(STRFTIME("%%d", %s) AS NUMBER)', $sqlWalker->walkArithmeticPrimary($this->date)); | ||
} | ||
|
||
throw new \RuntimeException(sprintf('Platform "%s" is not supported!', $platform::class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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 App\Doctrine; | ||
|
||
use Doctrine\DBAL\Platforms\MySQLPlatform; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
use Doctrine\DBAL\Platforms\SqlitePlatform; | ||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\AST\Node; | ||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
use Doctrine\ORM\Query\TokenType; | ||
|
||
final class Month extends FunctionNode | ||
{ | ||
/** @var Node|string|null */ | ||
public $date; | ||
|
||
public function parse(Parser $parser): void | ||
{ | ||
$parser->match(TokenType::T_IDENTIFIER); | ||
$parser->match(TokenType::T_OPEN_PARENTHESIS); | ||
|
||
$this->date = $parser->ArithmeticPrimary(); | ||
|
||
$parser->match(TokenType::T_CLOSE_PARENTHESIS); | ||
} | ||
|
||
public function getSql(SqlWalker $sqlWalker): string | ||
{ | ||
$platform = $sqlWalker->getConnection()->getDatabasePlatform(); | ||
|
||
if ($platform instanceof MySQLPlatform) { | ||
return sprintf('MONTH(%s)', $sqlWalker->walkArithmeticPrimary($this->date)); | ||
} | ||
|
||
if ($platform instanceof PostgreSQLPlatform) { | ||
return sprintf('EXTRACT(MONTH FROM %s)', $sqlWalker->walkArithmeticPrimary($this->date)); | ||
} | ||
|
||
if ($platform instanceof SqlitePlatform) { | ||
return sprintf('CAST(STRFTIME("%%m", %s) AS NUMBER)', $sqlWalker->walkArithmeticPrimary($this->date)); | ||
} | ||
|
||
throw new \RuntimeException(sprintf('Platform "%s" is not supported!', $platform::class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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 App\Doctrine; | ||
|
||
use Doctrine\DBAL\Platforms\MySQLPlatform; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
use Doctrine\DBAL\Platforms\SqlitePlatform; | ||
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | ||
use Doctrine\ORM\Query\AST\Node; | ||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
use Doctrine\ORM\Query\TokenType; | ||
|
||
final class Year extends FunctionNode | ||
{ | ||
/** @var Node|string|null */ | ||
public $date; | ||
|
||
public function parse(Parser $parser): void | ||
{ | ||
$parser->match(TokenType::T_IDENTIFIER); | ||
$parser->match(TokenType::T_OPEN_PARENTHESIS); | ||
|
||
$this->date = $parser->ArithmeticPrimary(); | ||
|
||
$parser->match(TokenType::T_CLOSE_PARENTHESIS); | ||
} | ||
|
||
public function getSql(SqlWalker $sqlWalker): string | ||
{ | ||
$platform = $sqlWalker->getConnection()->getDatabasePlatform(); | ||
|
||
if ($platform instanceof MySQLPlatform) { | ||
return sprintf('YEAR(%s)', $sqlWalker->walkArithmeticPrimary($this->date)); | ||
} | ||
|
||
if ($platform instanceof PostgreSQLPlatform) { | ||
return sprintf('EXTRACT(YEAR FROM %s)', $sqlWalker->walkArithmeticPrimary($this->date)); | ||
} | ||
|
||
if ($platform instanceof SqlitePlatform) { | ||
return sprintf('CAST(STRFTIME("%%Y", %s) AS NUMBER)', $sqlWalker->walkArithmeticPrimary($this->date)); | ||
} | ||
|
||
throw new \RuntimeException(sprintf('Platform "%s" is not supported!', $platform::class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?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 App\Statistics\Provider; | ||
|
||
use App\Repository\TalkRepository; | ||
|
||
final class DayTalksProvider | ||
{ | ||
public function __construct( | ||
private readonly TalkRepository $talkRepository, | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<array{period: \DateTimeInterface, total: int}> | ||
*/ | ||
public function provide(\DatePeriod $datePeriod): array | ||
{ | ||
$talkStatistics = $this->talkRepository->findTalkStatisticsPerDay($datePeriod); | ||
|
||
$talks = []; | ||
foreach ($datePeriod as $date) { | ||
$talks[] = [ | ||
'period' => $date, | ||
'total' => $this->getTotalForDate($talkStatistics, $date), | ||
]; | ||
} | ||
|
||
return $talks; | ||
} | ||
|
||
/** @param array<array{total: string|int, year: int, month: int, day: int}> $totals */ | ||
private function getTotalForDate(array $totals, \DateTimeInterface $date): int | ||
{ | ||
$formattedPeriodDate = $date->format('Y-n-j'); | ||
|
||
foreach ($totals as $entry) { | ||
if ($entry['year'] . '-' . $entry['month'] . '-' . $entry['day'] === $formattedPeriodDate) { | ||
return (int) $entry['total']; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?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 App\Statistics\Provider; | ||
|
||
use App\Repository\TalkRepository; | ||
|
||
final class MonthTalksProvider | ||
{ | ||
public function __construct( | ||
private readonly TalkRepository $talkRepository, | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<array{period: \DateTimeInterface, total: int}> | ||
*/ | ||
public function provide(\DatePeriod $datePeriod): array | ||
{ | ||
$talkStatistics = $this->talkRepository->findTalkStatisticsPerMonth($datePeriod); | ||
|
||
$talks = []; | ||
foreach ($datePeriod as $date) { | ||
$talks[] = [ | ||
'period' => $date, | ||
'total' => $this->getTotalForDate($talkStatistics, $date), | ||
]; | ||
} | ||
|
||
return $talks; | ||
} | ||
|
||
/** @param array<array{total: string|int, year: int, month: int}> $totals */ | ||
private function getTotalForDate(array $totals, \DateTimeInterface $date): int | ||
{ | ||
$formattedPeriodDate = $date->format('Y-n'); | ||
|
||
foreach ($totals as $entry) { | ||
$entryDate = $entry['year'] . '-' . $entry['month']; | ||
if ($formattedPeriodDate === $entryDate) { | ||
return (int) $entry['total']; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
} |
Oops, something went wrong.