Skip to content

Commit

Permalink
consolidate repeated logic and moved to a parent class
Browse files Browse the repository at this point in the history
  • Loading branch information
manewc committed Sep 24, 2024
1 parent 6e57ffb commit 5b66892
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 63 deletions.
23 changes: 3 additions & 20 deletions src/adapters/class-jw-player-7-for-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,14 @@
namespace Alley\WP\WP_Video_Sync\Adapters;

use Alley\WP\WP_Video_Sync\Interfaces\Adapter;
use Alley\WP\WP_Video_Sync\Last_Modified_Date;
use DateTimeImmutable;
use stdClass;

/**
* JW Player 7 for WP Adapter. Supports both the free and premium versions of the plugin.
*/
class JW_Player_7_For_WP implements Adapter {
/**
* The date of the last modification to the last batch of videos.
*
* @var ?DateTimeImmutable
*/
private ?DateTimeImmutable $last_modified_date;

/**
* Fetches the date of the last modification to the last batch of videos.
*
* @return ?DateTimeImmutable
*/
public function get_last_modified_date(): ?DateTimeImmutable {
return $this->last_modified_date;
}
class JW_Player_7_For_WP extends Last_Modified_Date implements Adapter {

/**
* Fetches videos from JW Player that were modified after the provided DateTime.
Expand All @@ -54,10 +40,7 @@ public function get_videos( DateTimeImmutable $updated_after, int $batch_size ):

// Attempt to set the last modified date.
if ( isset( $videos[ count( $videos ) - 1 ]->last_modified ) ) {
$last_modified_date = DateTimeImmutable::createFromFormat( DATE_W3C, $videos[ count( $videos ) - 1 ]->last_modified );
if ( $last_modified_date instanceof DateTimeImmutable ) {
$this->last_modified_date = $last_modified_date;
}
$this->set_last_modified_date( $videos[ count( $videos ) - 1 ]->last_modified );
}

return $videos;
Expand Down
45 changes: 8 additions & 37 deletions src/adapters/class-jw-player.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,14 @@
use Alley\WP\WP_Video_Sync\API\JW_Player_API;
use Alley\WP\WP_Video_Sync\API\Request;
use Alley\WP\WP_Video_Sync\Interfaces\Adapter;
use Alley\WP\WP_Video_Sync\Last_Modified_Date;
use DateTimeImmutable;
use stdClass;

/**
* JW Player Adapter.
*/
class JW_Player implements Adapter {

/**
* The date of the last modification to the last batch of videos.
*
* @var ?DateTimeImmutable
*/
private ?DateTimeImmutable $last_modified_date = null;
class JW_Player extends Last_Modified_Date implements Adapter {

/**
* The JW Player API.
Expand All @@ -41,34 +35,6 @@ public function __construct( JW_Player_API $api ) {
$this->jw_player_api = $api;
}

/**
* Fetches the date of the last modification to the last batch of videos.
*
* @return ?DateTimeImmutable
*/
public function get_last_modified_date(): ?DateTimeImmutable {
return $this->last_modified_date;
}

/**
* Sets the date of the last modification to the latest batch of videos.
*
* @param array $videos An array of videos and associated data.
* @return void
*/
public function set_last_modified_date( array $videos ): void {
if (
! empty( $videos )
&& isset( $videos[ count( $videos ) - 1 ]->last_modified )
) {
$last_modified_date = DateTimeImmutable::createFromFormat( DATE_W3C, $videos[ count( $videos ) - 1 ]->last_modified );

if ( $last_modified_date instanceof DateTimeImmutable ) {
$this->last_modified_date = $last_modified_date;
}
}
}

/**
* Fetches videos from JW Player that were modified after the provided DateTime.
*
Expand All @@ -93,7 +59,12 @@ public function get_videos( DateTimeImmutable $updated_after, int $batch_size ):
}

// Attempt to set the last modified date.
$this->set_last_modified_date( $videos['media'] );
if (
! empty( $videos['media'] )
&& isset( $videos['media'][ count( $videos['media'] ) - 1 ]->last_modified )
) {
$this->set_last_modified_date( $videos['media'][ count( $videos ) - 1 ]->last_modified );
}

// Return the videos.
return ! empty( $videos['media'] ) ? $videos['media'] : [];
Expand Down
46 changes: 46 additions & 0 deletions src/class-last-modified-date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* WP_Video_Sync: Last modified date.
*
* @package wp-video-sync
*/

namespace Alley\WP\WP_Video_Sync;

use DateTimeImmutable;

/**
* Class for tracking the modified date of the last video in a batch.
*/
class Last_Modified_Date {

/**
* The date of the last modification to the last batch of videos.
*
* @var ?DateTimeImmutable
*/
private ?DateTimeImmutable $last_modified_date = null;

/**
* Fetches the date of the last modification to the last batch of videos.
*
* @return ?DateTimeImmutable
*/
public function get_last_modified_date(): ?DateTimeImmutable {
return $this->last_modified_date;
}

/**
* Sets the date of the last modification based on the latest batch of videos.
*
* @param string $last_modified_date The date of the last modified video in the batch.
* @return void
*/
public function set_last_modified_date( string $last_modified_date = '' ): void {
$last_modified_date = DateTimeImmutable::createFromFormat( DATE_W3C, $last_modified_date );

if ( $last_modified_date instanceof \DateTimeImmutable ) {
$this->last_modified_date = $last_modified_date;
}
}
}
6 changes: 0 additions & 6 deletions src/interfaces/adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
* Defines an interface that all adapters must implement.
*/
interface Adapter {
/**
* Fetches the date of the last modification to the last batch of videos.
*
* @return ?DateTimeImmutable
*/
public function get_last_modified_date(): ?DateTimeImmutable;

/**
* Fetches videos from the provider that were modified after the provided DateTime.
Expand Down

0 comments on commit 5b66892

Please sign in to comment.