Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Add listener to save commit on blob update
Browse files Browse the repository at this point in the history
Update the usage of `$this->em->{persist,create}` to
`$this->{persist,create}` to ensure the action isn't dispatched when
saving relations, so we can hook into the blob save action to persist
the commit.

Fixes #866.
  • Loading branch information
mAAdhaTTah committed Jan 16, 2021
1 parent af082f3 commit 2c31cb0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 16 deletions.
11 changes: 11 additions & 0 deletions app/Database/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Intraxia\Gistpen\Contract\Repository;
use Intraxia\Gistpen\Model\Blob;
use Intraxia\Gistpen\Model\Language;
use Intraxia\Gistpen\Model\Commit;
use Intraxia\Gistpen\Model\State;
use Intraxia\Jaxion\Contract\Axolotl\EntityManager;
use Intraxia\Jaxion\Axolotl\Collection;
Expand Down Expand Up @@ -100,10 +101,20 @@ protected function fill_relations( Model $model, array $params ) {
}
}
break;
case 'commits':
$value = $this->em->find_by( Commit::class, array_merge( $params, array(
'repo_id' => $model->get_primary_id(),
'post_status' => 'any',
'order' => 'ASC',
'orderby' => 'date',
) ) );
break;
}

if ( null !== $value ) {
$model->unguard();
$model->set_attribute( $key, $value );
$model->reguard();
}
}

Expand Down
6 changes: 3 additions & 3 deletions app/Database/Repository/WordPressPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function create( $class, array $data = array(), array $options = array()
$blob_data['repo_id'] = $model->get_primary_id();
$blob_data['status'] = $model->get_attribute( 'status' );

$blob = $this->em->create( Blob::class, $blob_data, array(
$blob = $this->create( Blob::class, $blob_data, array(
'unguarded' => true,
) );

Expand Down Expand Up @@ -338,7 +338,7 @@ public function persist( Model $model ) {
$blob->status = $model->get_attribute( 'status' );
$blob->reguard();

$this->em->persist( $blob );
$this->persist( $blob );
}

foreach ( $deleted_blobs as $deleted_blob ) {
Expand Down Expand Up @@ -389,7 +389,7 @@ public function persist( Model $model ) {
$states = new Collection( State::class );

foreach ( $model->states as $state ) {
$state = $this->em->persist( $state );
$state = $this->persist( $state );

if ( ! is_wp_error( $state ) ) {
$states = $states->add( $state );
Expand Down
29 changes: 26 additions & 3 deletions app/Listener/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct( EntityManager $em ) {
*
* @param Repo $repo
*/
public function add_commit( Repo $repo ) {
public function add_repo_commit( Repo $repo ) {
$commits = $this->em->find_by( Commit::class, array(
'repo_id' => $repo->ID,
'with' => array(
Expand Down Expand Up @@ -117,6 +117,21 @@ public function add_commit( Repo $repo ) {
$this->em->persist( $new_commit );
}

/**
* Checks if the Repo has changed and creates a new commit if it has.
*
* @param Blob $blob
*/
public function add_blob_commit( Blob $blob ) {
$this->add_repo_commit( $this->em->find( Repo::class, $blob->repo_id, [
'with' => [
'blobs' => [
'with' => 'language',
],
],
] ) );
}

/**
* Remove the action hook to save a post revision
*
Expand Down Expand Up @@ -206,11 +221,19 @@ public function action_hooks() {
return array(
array(
'hook' => 'wpgp.create.repo',
'method' => 'add_commit',
'method' => 'add_repo_commit',
),
array(
'hook' => 'wpgp.persist.repo',
'method' => 'add_commit',
'method' => 'add_repo_commit',
),
array(
'hook' => 'wpgp.create.blob',
'method' => 'add_blob_commit',
),
array(
'hook' => 'wpgp.persist.blob',
'method' => 'add_blob_commit',
),
array(
'hook' => 'post_updated',
Expand Down
1 change: 1 addition & 0 deletions app/Model/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Repo extends Model implements UsesWordPressPost {
'gist_id',
'created_at',
'updated_at',
'commits',
);

/**
Expand Down
8 changes: 7 additions & 1 deletion test/Integration/Http/Blob/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,16 @@ public function test_saves_blob_with_filename_to_repo() {
$repo = $this->app->make( 'database' )
->find( Repo::class, $this->repo->ID, [
'with' => [
'blobs' => [
'blobs' => [
'with' => [
'language' => [],
],
],
'commits' => [
'with' => [
'states' => [],
],
],
],
] );

Expand All @@ -108,6 +113,7 @@ public function test_saves_blob_with_filename_to_repo() {
],
] );
$this->assertSame( $blob->filename, $this->blob->filename );
$this->assertCount( 2, $repo->commits );
}

public function test_saves_blob_with_filename_code_and_language() {
Expand Down
18 changes: 9 additions & 9 deletions test/Unit/Listener/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function test_should_save_when_no_commits_saved() {
),
) );

$this->database->add_commit( $repo );
$this->database->add_repo_commit( $repo );

$commits = $this->em->find_by( Commit::class, array(
'repo_id' => $repo->ID,
Expand Down Expand Up @@ -104,7 +104,7 @@ public function test_should_not_save_new_commit_if_unchanged() {
'with' => 'states',
) );

$this->database->add_commit( $repo );
$this->database->add_repo_commit( $repo );

$commits = $this->em->find_by( Commit::class, array(
'repo_id' => $repo->ID,
Expand All @@ -128,7 +128,7 @@ public function test_should_save_commit_if_description_changes() {

$repo->description = 'New Description';

$this->database->add_commit( $repo );
$this->database->add_repo_commit( $repo );

$commits = $this->em->find_by( Commit::class, array(
'repo_id' => $repo->ID,
Expand Down Expand Up @@ -179,7 +179,7 @@ public function test_should_save_commit_if_blob_added() {

$repo->blobs = $repo->blobs->add( $blob = $this->em->persist( $blob ) );

$this->database->add_commit( $repo );
$this->database->add_repo_commit( $repo );

$commits = $this->em->find_by( Commit::class, array(
'repo_id' => $repo->ID,
Expand Down Expand Up @@ -234,7 +234,7 @@ public function test_should_save_commit_if_blob_removed() {

$repo->blobs = $repo->blobs->remove_at( 2 );

$this->database->add_commit( $repo );
$this->database->add_repo_commit( $repo );

$commits = $this->em->find_by( Commit::class, array(
'repo_id' => $repo->ID,
Expand Down Expand Up @@ -291,7 +291,7 @@ public function test_should_save_if_blob_added_and_removed() {

$repo->blobs = $repo->blobs->remove_at( 2 )->add( $blob = $this->em->persist( $blob ) );

$this->database->add_commit( $repo );
$this->database->add_repo_commit( $repo );

$commits = $this->em->find_by( Commit::class, array(
'repo_id' => $repo->ID,
Expand Down Expand Up @@ -351,7 +351,7 @@ public function test_should_save_commit_if_blob_code_changes() {

$this->em->persist( $blob );

$this->database->add_commit( $repo );
$this->database->add_repo_commit( $repo );

$commits = $this->em->find_by( Commit::class, array(
'repo_id' => $repo->ID,
Expand Down Expand Up @@ -408,7 +408,7 @@ public function test_should_save_commit_if_blob_filename_changes() {

$this->em->persist( $blob );

$this->database->add_commit( $repo );
$this->database->add_repo_commit( $repo );

$commits = $this->em->find_by( Commit::class, array(
'repo_id' => $repo->ID,
Expand Down Expand Up @@ -467,7 +467,7 @@ public function test_should_save_commit_if_blob_language_changes() {
->find_by( Language::class, array( 'slug' => 'js' ) )
->first();

$this->database->add_commit( $repo );
$this->database->add_repo_commit( $repo );

$commits = $this->em->find_by( Commit::class, array(
'repo_id' => $repo->ID,
Expand Down

0 comments on commit 2c31cb0

Please sign in to comment.