Skip to content

Deprecate and replace findResource() method#3727

Merged
demiankatz merged 14 commits intovufind-org:devfrom
demiankatz:refactor-findresource
Jun 5, 2024
Merged

Deprecate and replace findResource() method#3727
demiankatz merged 14 commits intovufind-org:devfrom
demiankatz:refactor-findresource

Conversation

@demiankatz
Copy link
Member

@demiankatz demiankatz commented May 31, 2024

The Resource table's findResource() method does far too many different things, resulting in confusing code. This PR replaces it with three different methods covering its primary use cases, clarifying the code and allowing better organization.

Accomplishing this required some significant related refactoring:

  • Separating ratings functionality from the record drivers by introducing \VuFind\Ratings\RatingsService and \VuFind\View\Helper\Root\Ratings
  • Refactoring add/delete tag functionality out of \VuFind\Db\Service\TagService and into \VuFind\Tags; this turns out to be higher-level logic that doesn't belong in a low-level database service. Moving this code also required me to restore the associated deprecated record driver code, since it is no longer possible to redirect those deprecated methods to their replacements.

@demiankatz demiankatz marked this pull request as draft May 31, 2024 14:55
protected function fetchSingleRecord($id)
{
$resource = $this->table->findResource($id, 'Summon');
$resource = $this->resourceService->getResourceByRecordId($id, 'Summon');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was actually a bug that this was previously creating resource rows when looking up IDs.

*
* @return void
*/
public function saveRating(RecordDriver $driver, int $userId, ?int $rating): void
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it confusing to have addOrUpdateRating methods at multiple layers of the code, so I gave this a different name to help differentiate it.

*
* @return array
*
* @deprecated Use \VuFind\Ratings\RatingsService::getRatingData()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't redirect these methods without adding new record driver dependencies, so I'm just deprecating them as-is. I'd really love to get rid of them and their associated property, but we'll just have to wait until release 11.

*/
public function __invoke()
{
return $this->service;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a wrapper helper to expose the ratings service to the views is probably not a great design, but this falls in the category of "I don't want to change too many things all at once." I at least think it's better to have an explicit helper doing this work rather than running business logic through the record drivers, so it's a step in the right direction.

{
$resource = $this->resourcePopulator->getOrCreateResourceForDriver($driver);
foreach ($tags as $tag) {
$resource->addTag($tag, $user);
Copy link
Member Author

@demiankatz demiankatz May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and the deleteTag method below) will need to be refactored into \VuFind\Db\Service\TagService... but that's a project for a separate PR!

@demiankatz demiankatz added this to the 10.0 milestone May 31, 2024
@demiankatz demiankatz added the architecture pull requests that involve significant refactoring / architectural changes label May 31, 2024
@demiankatz demiankatz requested review from EreMaijala and aleksip May 31, 2024 18:28
@demiankatz demiankatz marked this pull request as ready for review May 31, 2024 18:29
@demiankatz
Copy link
Member Author

All tests are passing in both sandal and sandal5 themes. I believe this is ready for review!

Copy link
Contributor

@EreMaijala EreMaijala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me and tests are passing!

@demiankatz demiankatz removed the request for review from aleksip June 5, 2024 12:42
@demiankatz demiankatz merged commit 505ceb6 into vufind-org:dev Jun 5, 2024
@demiankatz demiankatz deleted the refactor-findresource branch June 5, 2024 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

architecture pull requests that involve significant refactoring / architectural changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants