Move all files to tome/
This commit is contained in:
parent
5675bcfc36
commit
674daab35b
2874 changed files with 0 additions and 0 deletions
|
@ -0,0 +1,5 @@
|
|||
name: Presentations
|
||||
description: Custom functionality for presentations.
|
||||
core_version_requirement: ^11
|
||||
type: module
|
||||
package: Custom
|
21
tome/modules/opd_presentations/opd_presentations.module
Normal file
21
tome/modules/opd_presentations/opd_presentations.module
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Drupal\opd_presentations\Event;
|
||||
use Drupal\opd_presentations\Presentation;
|
||||
|
||||
/**
|
||||
* Implements hook_entity_bundle_info_alter().
|
||||
*
|
||||
* @param array<non-empty-string, array{class: non-empty-string}> $bundles
|
||||
*/
|
||||
function opd_presentations_entity_bundle_info_alter(array &$bundles): void {
|
||||
if (isset($bundles['node'])) {
|
||||
$bundles['node'][Presentation::NODE_TYPE]['class'] = Presentation::class;
|
||||
}
|
||||
|
||||
if (isset($bundles['paragraph'])) {
|
||||
$bundles['paragraph'][Event::PARAGRAPH_TYPE]['class'] = Event::class;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
services:
|
||||
Drupal\opd_presentations\Action\CountGivenPresentations:
|
||||
autowire: true
|
||||
Drupal\opd_presentations\Repository\PresentationNodeRepository:
|
||||
autowire: true
|
||||
Drupal\opd_presentations\Repository\PresentationRepositoryInterface: '@Drupal\opd_presentations\Repository\PresentationNodeRepository'
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_presentations\Action;
|
||||
|
||||
use Drupal\opd_presentations\Presentation;
|
||||
use Drupal\opd_presentations\Repository\PresentationRepositoryInterface;
|
||||
|
||||
readonly final class CountGivenPresentations {
|
||||
|
||||
public function __construct(
|
||||
private PresentationRepositoryInterface $presentationRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
public function __invoke(): int {
|
||||
$presentations = $this->presentationRepository->getPublished();
|
||||
|
||||
return array_reduce(
|
||||
array: $presentations,
|
||||
callback: fn (int $count, Presentation $presentation)
|
||||
=> $count + $presentation->getEvents()->getPast()->count(),
|
||||
initial: 0,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
20
tome/modules/opd_presentations/src/Date.php
Normal file
20
tome/modules/opd_presentations/src/Date.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_presentations;
|
||||
|
||||
readonly final class Date {
|
||||
|
||||
public function toTimestamp(): int {
|
||||
return $this->date->getTimestamp();
|
||||
}
|
||||
|
||||
public static function fromString(string $date): self {
|
||||
return new self(new \DateTimeImmutable($date));
|
||||
}
|
||||
|
||||
private function __construct(private \DateTimeImmutable $date) {
|
||||
}
|
||||
|
||||
}
|
28
tome/modules/opd_presentations/src/Event.php
Normal file
28
tome/modules/opd_presentations/src/Event.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_presentations;
|
||||
|
||||
use Drupal\paragraphs\Entity\Paragraph;
|
||||
use Drupal\paragraphs\ParagraphInterface;
|
||||
|
||||
final class Event extends Paragraph implements ParagraphInterface {
|
||||
|
||||
public const PARAGRAPH_TYPE = 'event';
|
||||
|
||||
public function getEventDate(): string {
|
||||
/** @var non-empty-string */
|
||||
return $this->get('field_date')->value;
|
||||
}
|
||||
|
||||
public function getEventName(): string {
|
||||
/** @var non-empty-string */
|
||||
return $this->get('field_event_name')->value;
|
||||
}
|
||||
|
||||
public function isPast(): bool {
|
||||
return $this->getEventDate() < strtotime('today');
|
||||
}
|
||||
|
||||
}
|
70
tome/modules/opd_presentations/src/Events.php
Normal file
70
tome/modules/opd_presentations/src/Events.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_presentations;
|
||||
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* @implements \IteratorAggregate<Event>
|
||||
*/
|
||||
readonly final class Events implements \Countable, \IteratorAggregate {
|
||||
|
||||
public function count(): int {
|
||||
return count($this->events);
|
||||
}
|
||||
|
||||
public function filter(\Closure $callback): self {
|
||||
return new self(array_filter(
|
||||
array: $this->events,
|
||||
callback: $callback,
|
||||
));
|
||||
}
|
||||
|
||||
public function first(): Event {
|
||||
return array_values($this->events)[0];
|
||||
}
|
||||
|
||||
public function getIterator(): \Traversable {
|
||||
return new \ArrayIterator($this->events);
|
||||
}
|
||||
|
||||
public function getPast(): self {
|
||||
return (new self($this->events))
|
||||
->filter(fn (Event $event): bool => $event->isPast());
|
||||
}
|
||||
|
||||
public static function fromDateStrings(string ...$dates): self {
|
||||
$events = array_map(
|
||||
array: $dates,
|
||||
callback: fn (string $date): Event => Event::create([
|
||||
'field_date' => strtotime($date),
|
||||
]),
|
||||
);
|
||||
|
||||
return new self($events);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Event[]
|
||||
*/
|
||||
public function toEvents(): array {
|
||||
return $this->events;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Event[] $events
|
||||
*/
|
||||
public static function fromEvents(array $events): self {
|
||||
return new self($events);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Event[] $events
|
||||
*/
|
||||
private function __construct(private array $events) {
|
||||
Assert::allIsInstanceOf($events, Event::class);
|
||||
}
|
||||
|
||||
}
|
18
tome/modules/opd_presentations/src/Presentation.php
Normal file
18
tome/modules/opd_presentations/src/Presentation.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_presentations;
|
||||
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\node\NodeInterface;
|
||||
|
||||
final class Presentation extends Node implements NodeInterface {
|
||||
|
||||
public const NODE_TYPE = 'presentation';
|
||||
|
||||
public function getEvents(): Events {
|
||||
return Events::fromEvents($this->get('field_events')->referencedEntities());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_presentations\Repository;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\Core\Entity\Query\QueryInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\node\NodeStorageInterface;
|
||||
use Drupal\opd_presentations\Presentation;
|
||||
|
||||
final class PresentationNodeRepository implements PresentationRepositoryInterface {
|
||||
|
||||
private NodeStorageInterface $nodeStorage;
|
||||
|
||||
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
|
||||
$this->nodeStorage = $entityTypeManager->getStorage('node');
|
||||
}
|
||||
|
||||
public function getPublished(): array {
|
||||
$query = $this->query();
|
||||
$query->condition('status', NodeInterface::PUBLISHED);
|
||||
|
||||
$nodeIds = $query->execute();
|
||||
assert(is_array($nodeIds));
|
||||
|
||||
/** @var Presentation[] */
|
||||
return $this->nodeStorage->loadMultiple($nodeIds);
|
||||
}
|
||||
|
||||
private function query(): QueryInterface {
|
||||
$query = $this->nodeStorage->getQuery();
|
||||
$query->accessCheck();
|
||||
$query->condition('type', Presentation::NODE_TYPE);
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_presentations\Repository;
|
||||
|
||||
use Drupal\opd_presentations\Presentation;
|
||||
|
||||
interface PresentationRepositoryInterface {
|
||||
|
||||
/**
|
||||
* @return Presentation[]
|
||||
*/
|
||||
public function getPublished(): array;
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\opd_presentations\Action;
|
||||
|
||||
use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait;
|
||||
use Drupal\opd_presentations\Action\CountGivenPresentations;
|
||||
use Drupal\opd_presentations\Events;
|
||||
use weitzman\DrupalTestTraits\ExistingSiteBase;
|
||||
|
||||
final class CountGivenPresentationsTest extends ExistingSiteBase {
|
||||
|
||||
use PresentationCreationTrait;
|
||||
|
||||
public function test_it_counts_events(): void {
|
||||
$action = $this->container->get(CountGivenPresentations::class);
|
||||
assert($action instanceof CountGivenPresentations);
|
||||
|
||||
$this->createPresentation(
|
||||
Events::fromDateStrings('yesterday'),
|
||||
);
|
||||
|
||||
$this->assertGreaterThanOrEqual(
|
||||
actual: $action(),
|
||||
expected: 1,
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_only_counts_published_events(): void {
|
||||
$action = $this->container->get(CountGivenPresentations::class);
|
||||
assert($action instanceof CountGivenPresentations);
|
||||
|
||||
// Get the existing presentation count (including existing nodes).
|
||||
$originalCount = $action();
|
||||
|
||||
$this->createPresentation(
|
||||
events: Events::fromDateStrings('yesterday'),
|
||||
isPublished: FALSE,
|
||||
);
|
||||
|
||||
// Ensure the count has only increased by one, even though an unpublished
|
||||
// presentation was created.
|
||||
$this->assertSame(
|
||||
actual: $action(),
|
||||
expected: $originalCount,
|
||||
);
|
||||
}
|
||||
|
||||
public function test_it_only_counts_past_events(): void {
|
||||
$action = $this->container->get(CountGivenPresentations::class);
|
||||
assert($action instanceof CountGivenPresentations);
|
||||
|
||||
// Get the existing presentation count (including existing nodes).
|
||||
$originalCount = $action();
|
||||
|
||||
$this->assertGreaterThanOrEqual(
|
||||
actual: $originalCount,
|
||||
expected: 0,
|
||||
);
|
||||
|
||||
$this->createPresentation(
|
||||
Events::fromDateStrings('tomorrow', 'yesterday'),
|
||||
);
|
||||
|
||||
// Ensure the count has only increased by one, even though a future and past event were created.
|
||||
$this->assertSame(
|
||||
actual: $action(),
|
||||
expected: $originalCount + 1,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opd_presentations\Functional;
|
||||
|
||||
use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait;
|
||||
use Drupal\opd_presentations\Events;
|
||||
use weitzman\DrupalTestTraits\ExistingSiteBase;
|
||||
|
||||
final class PresentationTest extends ExistingSiteBase {
|
||||
|
||||
use PresentationCreationTrait;
|
||||
|
||||
public function test_only_past_events_are_returned(): void {
|
||||
$presentation = $this->createPresentation(
|
||||
events: Events::fromDateStrings('now', 'yesterday', 'tomorrow'),
|
||||
);
|
||||
|
||||
$events = $presentation->getEvents()->getPast();
|
||||
|
||||
$this->assertCount(
|
||||
expectedCount: 1,
|
||||
haystack: $events,
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\opd_presentations\Traits;
|
||||
|
||||
use Drupal\Tests\node\Traits\NodeCreationTrait;
|
||||
use Drupal\ctools\Testing\EntityCreationTrait;
|
||||
use Drupal\opd_presentations\Date;
|
||||
use Drupal\opd_presentations\Event;
|
||||
use Drupal\opd_presentations\Events;
|
||||
use Drupal\opd_presentations\Presentation;
|
||||
|
||||
trait PresentationCreationTrait {
|
||||
|
||||
use EntityCreationTrait;
|
||||
use NodeCreationTrait;
|
||||
|
||||
private function createPresentation(Events $events, bool $isPublished = TRUE): Presentation {
|
||||
$presentation = $this->createNode([
|
||||
'field_events' => $events->toEvents(),
|
||||
'status' => $isPublished,
|
||||
'type' => Presentation::NODE_TYPE,
|
||||
]);
|
||||
|
||||
assert($presentation instanceof Presentation);
|
||||
|
||||
return $presentation;
|
||||
}
|
||||
|
||||
private function createEvent(string $eventName, Date $eventDate): Event {
|
||||
$event = $this->createEntity(
|
||||
entity_type: 'paragraph',
|
||||
values: [
|
||||
'field_date' => $eventDate->toTimestamp(),
|
||||
'field_event_name' => $eventName,
|
||||
'type' => Event::PARAGRAPH_TYPE,
|
||||
],
|
||||
);
|
||||
|
||||
assert($event instanceof Event);
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue