link_outcome_store->find_fixable( $link_id, $fix->id ); if ( null === $link || ! $fix->action->applies_to( $link ) ) { return $this->with_link_done( $fix ); } $pending = $this->pending_posts( $fix, $link ); $is_last_part = count( $pending ) <= $places_allowed; if ( $is_last_part ) { $this->link_outcome_store->mark( $link->id, $fix->id, $fix->action->outcome(), FixAction::Redirect === $fix->action ? $fix->new_url : null ); } foreach ( array_slice( $pending, 0, $places_allowed ) as $post_id ) { $entry = $this->post_content_writer->rewrite( $link->id, $post_id, $this->edit_for( $fix, $link ) ); $fix = $fix->with_progress( $fix->progress->with_place( $entry ) ); $this->fix_repository->save_progress( $fix ); } if ( ! $is_last_part ) { return $fix; } if ( $fix->progress->has_unchanged_place_of( $link->id ) ) { $this->link_outcome_store->list_again( $link->id ); } return $this->with_link_done( $fix ); } private function with_link_done( Fix $fix ): Fix { $links_done = $fix->progress->links_done + 1; return $fix->with_progress( $fix->progress->with_links_done( $links_done ) ); } /** @return list */ private function pending_posts( Fix $fix, Link $link ): array { if ( ! $fix->action->changes_posts() ) { return array(); } return $this->fix_places->pending_posts( $link->id, $fix->progress->posts_journaled_for( $link->id ) ); } private function edit_for( Fix $fix, Link $link ): LinkEdit { if ( FixAction::Remove === $fix->action ) { return LinkEdit::removal( $link->url ); } $replacement_url = FixAction::Update === $fix->action ? $link->final_url : $fix->new_url; if ( null === $replacement_url ) { throw new UnexpectedValueException( 'The fix has no address to put in place of the link.' ); } return LinkEdit::replacement( $link->url, $replacement_url ); } }