Skip to content

Commit

Permalink
Use attendee repository in event repository
Browse files Browse the repository at this point in the history
This changes are required due to the new attendee repository in trunk.
  • Loading branch information
psrpinto committed Mar 18, 2024
1 parent 5ce16bf commit 9f96836
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
28 changes: 10 additions & 18 deletions includes/event/event-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
use WP_Error;
use WP_Post;
use WP_Query;
use Wporg\TranslationEvents\Attendee_Repository;
use Wporg\TranslationEvents\Translation_Events;

class Event_Repository implements Event_Repository_Interface {
private const POST_TYPE = Translation_Events::CPT;
private const USER_META_KEY_ATTENDING = Translation_Events::USER_META_KEY_ATTENDING;
private const POST_TYPE = Translation_Events::CPT;

private Attendee_Repository $attendee_repository;

public function __construct( Attendee_Repository $attendee_repository ) {
$this->attendee_repository = $attendee_repository;
}

public function create_event( Event $event ): void {
$event_id = wp_insert_post(
Expand Down Expand Up @@ -91,14 +97,7 @@ public function get_current_events( int $page = -1, int $page_size = -1 ): Event

public function get_current_events_for_user( int $user_id, int $page = -1, int $page_size = -1 ): Events_Query_Result {
$this->assert_pagination_arguments( $page, $page_size );

$attending_array = get_user_meta( $user_id, self::USER_META_KEY_ATTENDING, true );
if ( ! $attending_array ) {
$attending_array = array();
}

// $attending_array is an associative array with the event_id as key.
$event_ids_user_is_attending = array_keys( $attending_array );
$event_ids_user_is_attending = $this->attendee_repository->get_events_for_user( $user_id );

$now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) );
return $this->get_events_active_between(
Expand All @@ -112,14 +111,7 @@ public function get_current_events_for_user( int $user_id, int $page = -1, int $

public function get_past_events_for_user( int $user_id, int $page = -1, int $page_size = -1 ): Events_Query_Result {
$this->assert_pagination_arguments( $page, $page_size );

$attending_array = get_user_meta( $user_id, self::USER_META_KEY_ATTENDING, true );
if ( ! $attending_array ) {
$attending_array = array();
}

// $attending_array is an associative array with the event_id as key.
$event_ids_user_is_attending = array_keys( $attending_array );
$event_ids_user_is_attending = $this->attendee_repository->get_events_for_user( $user_id );

// We consider the start of time to be January 1st 2024,
// which is guaranteed to be earlier than when this plugin was created.
Expand Down
8 changes: 5 additions & 3 deletions tests/event/event-repository-cached.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@

use DateTimeImmutable;
use DateTimeZone;
use WP_UnitTestCase;
use GP_UnitTestCase;
use Wporg\TranslationEvents\Attendee_Repository;
use Wporg\TranslationEvents\Event\Event_Repository_Cached;
use Wporg\TranslationEvents\Event\Event;
use Wporg\TranslationEvents\Tests\Event_Factory;

class Event_Repository_Cached_Test extends WP_UnitTestCase {
class Event_Repository_Cached_Test extends GP_UnitTestCase {
private Event_Repository_Cached $repository;

public function setUp(): void {
parent::setUp();
$this->event_factory = new Event_Factory();
$this->repository = new Event_Repository_Cached();
$this->repository = new Event_Repository_Cached( new Attendee_Repository() );

wp_cache_delete( 'translation-events-active-events' );
$this->set_normal_user_as_current();
}

public function test_get_active_events() {
Expand Down
5 changes: 4 additions & 1 deletion tests/event/event-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTimeImmutable;
use DateTimeZone;
use GP_UnitTestCase;
use Wporg\TranslationEvents\Attendee_Repository;
use Wporg\TranslationEvents\Event\Event;
use Wporg\TranslationEvents\Event\Event_Repository;
use Wporg\TranslationEvents\Event\EventNotFound;
Expand All @@ -17,7 +18,9 @@ class Event_Repository_Test extends GP_UnitTestCase {
public function setUp(): void {
parent::setUp();
$this->event_factory = new Event_Factory();
$this->repository = new Event_Repository();
$this->repository = new Event_Repository( new Attendee_Repository() );

$this->set_normal_user_as_current();
}

public function test_get_event_throws_not_found_when_event_does_not_exist() {
Expand Down

0 comments on commit 9f96836

Please sign in to comment.