Skip to content

Commit 505ceb6

Browse files
authored
Deprecate and replace findResource() method (#3727)
1 parent eddf749 commit 505ceb6

48 files changed

Lines changed: 776 additions & 348 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

module/VuFind/config/module.config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@
466466
'VuFind\OAuth2\Repository\ScopeRepository' => 'VuFind\OAuth2\Repository\RepositoryWithOAuth2ConfigFactory',
467467
'VuFind\QRCode\Loader' => 'VuFind\QRCode\LoaderFactory',
468468
'VuFind\RateLimiter\RateLimiterManager' => 'VuFind\RateLimiter\RateLimiterManagerFactory',
469+
'VuFind\Ratings\RatingsService' => 'VuFind\Ratings\RatingsServiceFactory',
469470
'VuFind\Recommend\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',
470471
'VuFind\Record\Cache' => 'VuFind\Record\CacheFactory',
471472
'VuFind\Record\FallbackLoader\PluginManager' => 'VuFind\ServiceManager\AbstractPluginManagerFactory',

module/VuFind/src/VuFind/AjaxHandler/CommentRecord.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
use VuFind\Controller\Plugin\Captcha;
3535
use VuFind\Db\Entity\UserEntityInterface;
3636
use VuFind\Db\Service\CommentsServiceInterface;
37-
use VuFind\Db\Table\Resource;
3837
use VuFind\I18n\Translator\TranslatorAwareInterface;
38+
use VuFind\Ratings\RatingsService;
3939
use VuFind\Record\Loader as RecordLoader;
40+
use VuFind\Record\ResourcePopulator;
4041

4142
use function intval;
4243

@@ -56,22 +57,24 @@ class CommentRecord extends AbstractBase implements TranslatorAwareInterface
5657
/**
5758
* Constructor
5859
*
59-
* @param Resource $table Resource database table
60+
* @param ResourcePopulator $resourcePopulator Resource populator service
6061
* @param CommentsServiceInterface $commentsService Comments database service
6162
* @param Captcha $captcha Captcha controller plugin
6263
* @param ?UserEntityInterface $user Logged in user (or null)
6364
* @param bool $enabled Are comments enabled?
6465
* @param RecordLoader $recordLoader Record loader
6566
* @param AccountCapabilities $accountCapabilities Account capabilities helper
67+
* @param RatingsService $ratingsService Ratings service
6668
*/
6769
public function __construct(
68-
protected Resource $table,
70+
protected ResourcePopulator $resourcePopulator,
6971
protected CommentsServiceInterface $commentsService,
7072
protected Captcha $captcha,
7173
protected ?UserEntityInterface $user,
7274
protected bool $enabled,
7375
protected RecordLoader $recordLoader,
74-
protected AccountCapabilities $accountCapabilities
76+
protected AccountCapabilities $accountCapabilities,
77+
protected RatingsService $ratingsService
7578
) {
7679
}
7780

@@ -132,7 +135,7 @@ public function handleRequest(Params $params)
132135
);
133136
}
134137

135-
$resource = $this->table->findResource($id, $source);
138+
$resource = $this->resourcePopulator->getOrCreateResourceForRecordId($id, $source);
136139
$commentId = $this->commentsService->addComment(
137140
$comment,
138141
$this->user,
@@ -145,7 +148,8 @@ public function handleRequest(Params $params)
145148
&& ('' !== $rating
146149
|| $this->accountCapabilities->isRatingRemovalAllowed())
147150
) {
148-
$driver->addOrUpdateRating(
151+
$this->ratingsService->saveRating(
152+
$driver,
149153
$this->user->getId(),
150154
'' === $rating ? null : intval($rating)
151155
);

module/VuFind/src/VuFind/AjaxHandler/CommentRecordFactory.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
3434
use Psr\Container\ContainerExceptionInterface as ContainerException;
3535
use Psr\Container\ContainerInterface;
36+
use VuFind\Ratings\RatingsService;
37+
use VuFind\Record\ResourcePopulator;
3638

3739
/**
3840
* Factory for CommentRecord AJAX handler.
@@ -69,21 +71,18 @@ public function __invoke(
6971
if (!empty($options)) {
7072
throw new \Exception('Unexpected options passed to factory.');
7173
}
72-
$tablePluginManager = $container->get(\VuFind\Db\Table\PluginManager::class);
73-
$servicePluginManager = $container->get(
74-
\VuFind\Db\Service\PluginManager::class
75-
);
74+
$servicePluginManager = $container->get(\VuFind\Db\Service\PluginManager::class);
7675
$controllerPluginManager = $container->get('ControllerPluginManager');
7776
$capabilities = $container->get(\VuFind\Config\AccountCapabilities::class);
7877
return new $requestedName(
79-
$tablePluginManager->get(\VuFind\Db\Table\Resource::class),
78+
$container->get(ResourcePopulator::class),
8079
$servicePluginManager->get(\VuFind\Db\Service\CommentsServiceInterface::class),
81-
$controllerPluginManager
82-
->get(\VuFind\Controller\Plugin\Captcha::class),
80+
$controllerPluginManager->get(\VuFind\Controller\Plugin\Captcha::class),
8381
$container->get(\VuFind\Auth\Manager::class)->getUserObject(),
8482
$capabilities->getCommentSetting() !== 'disabled',
8583
$container->get(\VuFind\Record\Loader::class),
86-
$container->get(\VuFind\Config\AccountCapabilities::class)
84+
$container->get(\VuFind\Config\AccountCapabilities::class),
85+
$container->get(RatingsService::class)
8786
);
8887
}
8988
}

module/VuFind/src/VuFind/AjaxHandler/GetRecordRating.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
namespace VuFind\AjaxHandler;
3131

3232
use Laminas\Mvc\Controller\Plugin\Params;
33+
use VuFind\Ratings\RatingsService;
3334
use VuFind\Record\Loader as RecordLoader;
3435
use VuFind\View\Helper\Root\Record as RecordHelper;
3536

@@ -44,32 +45,18 @@
4445
*/
4546
class GetRecordRating extends AbstractBase
4647
{
47-
/**
48-
* Record loader
49-
*
50-
* @var RecordLoader
51-
*/
52-
protected $recordLoader;
53-
54-
/**
55-
* Record helper
56-
*
57-
* @var RecordHelper
58-
*/
59-
protected $recordHelper;
60-
6148
/**
6249
* Constructor
6350
*
64-
* @param RecordLoader $loader Record loader
65-
* @param RecordHelper $helper Record helper
51+
* @param RecordLoader $recordLoader Record loader
52+
* @param RecordHelper $recordHelper Record helper
53+
* @param RatingsService $ratingsService Ratings service
6654
*/
6755
public function __construct(
68-
RecordLoader $loader,
69-
RecordHelper $helper
56+
protected RecordLoader $recordLoader,
57+
protected RecordHelper $recordHelper,
58+
protected RatingsService $ratingsService
7059
) {
71-
$this->recordLoader = $loader;
72-
$this->recordHelper = $helper;
7360
}
7461

7562
/**
@@ -90,7 +77,7 @@ public function handleRequest(Params $params)
9077
$html = ($this->recordHelper)($driver)->renderTemplate('rating.phtml');
9178
return $this->formatResponse(
9279
[
93-
'ratingData' => $driver->getRatingData(),
80+
'ratingData' => $this->ratingsService->getRatingData($driver),
9481
'html' => $html,
9582
]
9683
);

module/VuFind/src/VuFind/AjaxHandler/GetRecordRatingFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
3434
use Psr\Container\ContainerExceptionInterface as ContainerException;
3535
use Psr\Container\ContainerInterface;
36+
use VuFind\Ratings\RatingsService;
3637

3738
/**
3839
* Factory for GetRecordRating AJAX handler.
@@ -71,7 +72,8 @@ public function __invoke(
7172
}
7273
return new $requestedName(
7374
$container->get(\VuFind\Record\Loader::class),
74-
$container->get('ViewRenderer')->plugin('record')
75+
$container->get('ViewRenderer')->plugin('record'),
76+
$container->get(RatingsService::class)
7577
);
7678
}
7779
}

module/VuFind/src/VuFind/AjaxHandler/TagRecord.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
use Laminas\Mvc\Controller\Plugin\Params;
3333
use VuFind\Db\Entity\UserEntityInterface;
34-
use VuFind\Db\Service\TagService;
3534
use VuFind\I18n\Translator\TranslatorAwareInterface;
3635
use VuFind\Record\Loader;
3736
use VuFind\Tags;
@@ -54,15 +53,13 @@ class TagRecord extends AbstractBase implements TranslatorAwareInterface
5453
/**
5554
* Constructor
5655
*
57-
* @param Loader $loader Record loader
58-
* @param TagService $tagService Tag database service
59-
* @param Tags $tagParser Tag parser
60-
* @param ?UserEntityInterface $user Logged in user (or null)
56+
* @param Loader $loader Record loader
57+
* @param Tags $tagHelper Tag helper
58+
* @param ?UserEntityInterface $user Logged in user (or null)
6159
*/
6260
public function __construct(
6361
protected Loader $loader,
64-
protected TagService $tagService,
65-
protected Tags $tagParser,
62+
protected Tags $tagHelper,
6663
protected ?UserEntityInterface $user
6764
) {
6865
}
@@ -92,11 +89,10 @@ public function handleRequest(Params $params)
9289
$serviceMethod = ('false' === $params->fromPost('remove', 'false'))
9390
? 'addTagsToRecord'
9491
: 'deleteTagsFromRecord';
95-
$this->tagService->$serviceMethod(
96-
$driver->getUniqueID(),
97-
$driver->getSourceIdentifier(),
92+
$this->tagHelper->$serviceMethod(
93+
$driver,
9894
$this->user,
99-
$this->tagParser->parse($tag)
95+
$this->tagHelper->parse($tag)
10096
);
10197
}
10298

module/VuFind/src/VuFind/AjaxHandler/TagRecordFactory.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ public function __invoke(
6969
if (!empty($options)) {
7070
throw new \Exception('Unexpected options passed to factory.');
7171
}
72-
$dbServiceManager = $container->get(\VuFind\Db\Service\PluginManager::class);
7372
return new $requestedName(
7473
$container->get(\VuFind\Record\Loader::class),
75-
$dbServiceManager->get(\VuFind\Db\Service\TagServiceInterface::class),
7674
$container->get(\VuFind\Tags::class),
7775
$container->get(\VuFind\Auth\Manager::class)->getUserObject()
7876
);

module/VuFind/src/VuFind/Controller/AbstractRecord.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@
2929

3030
namespace VuFind\Controller;
3131

32-
use VuFind\Db\Service\TagServiceInterface;
3332
use VuFind\Exception\BadRequest as BadRequestException;
3433
use VuFind\Exception\Forbidden as ForbiddenException;
3534
use VuFind\Exception\Mail as MailException;
35+
use VuFind\Ratings\RatingsService;
36+
use VuFind\Record\ResourcePopulator;
3637
use VuFind\RecordDriver\AbstractBase as AbstractRecordDriver;
38+
use VuFind\Tags;
3739
use VuFindSearch\ParamBag;
3840

3941
use function in_array;
@@ -163,13 +165,8 @@ public function addcommentAction()
163165
// something has gone wrong (or user submitted blank form) and we
164166
// should do nothing:
165167
if (!empty($comment)) {
166-
$table = $this->getTable('Resource');
167-
$resource = $table->findResource(
168-
$driver->getUniqueId(),
169-
$driver->getSourceIdentifier(),
170-
true,
171-
$driver
172-
);
168+
$populator = $this->serviceLocator->get(ResourcePopulator::class);
169+
$resource = $populator->getOrCreateResourceForDriver($driver);
173170
$commentsService = $this->getDbService(
174171
\VuFind\Db\Service\CommentsServiceInterface::class
175172
);
@@ -180,7 +177,8 @@ public function addcommentAction()
180177
$driver->isRatingAllowed()
181178
&& '0' !== ($rating = $this->params()->fromPost('rating', '0'))
182179
) {
183-
$driver->addOrUpdateRating($user->getId(), intval($rating));
180+
$ratingsService = $this->serviceLocator->get(RatingsService::class);
181+
$ratingsService->saveRating($driver, $user->getId(), intval($rating));
184182
}
185183

186184
$this->flashMessenger()->addMessage('add_comment_success', 'success');
@@ -241,12 +239,11 @@ public function addtagAction()
241239

242240
// Save tags, if any:
243241
if ($tags = $this->params()->fromPost('tag')) {
244-
$tagParser = $this->serviceLocator->get(\VuFind\Tags::class);
245-
$this->getDbService(TagServiceInterface::class)->addTagsToRecord(
246-
$driver->getUniqueID(),
247-
$driver->getSourceIdentifier(),
242+
$tagHelper = $this->serviceLocator->get(Tags::class);
243+
$tagHelper->addTagsToRecord(
244+
$driver,
248245
$user,
249-
$tagParser->parse($tags)
246+
$tagHelper->parse($tags)
250247
);
251248
$this->flashMessenger()
252249
->addMessage(['msg' => 'add_tag_success'], 'success');
@@ -281,9 +278,8 @@ public function deletetagAction()
281278

282279
// Save tags, if any:
283280
if ($tag = $this->params()->fromPost('tag')) {
284-
$this->getDbService(TagServiceInterface::class)->deleteTagsFromRecord(
285-
$driver->getUniqueID(),
286-
$driver->getSourceIdentifier(),
281+
$this->serviceLocator->get(Tags::class)->deleteTagsFromRecord(
282+
$driver,
287283
$user,
288284
[$tag]
289285
);
@@ -323,7 +319,9 @@ public function ratingAction()
323319
) {
324320
throw new BadRequestException('error_inconsistent_parameters');
325321
}
326-
$driver->addOrUpdateRating(
322+
$ratingsService = $this->serviceLocator->get(RatingsService::class);
323+
$ratingsService->saveRating(
324+
$driver,
327325
$user->getId(),
328326
'' === $rating ? null : intval($rating)
329327
);
@@ -335,12 +333,10 @@ public function ratingAction()
335333
}
336334

337335
// Display the "add rating" form:
338-
$view = $this->createViewModel(
339-
[
340-
'currentRating' => $user ? $driver->getRatingData($user->getId()) : null,
341-
]
342-
);
343-
return $view;
336+
$currentRating = $user
337+
? $this->serviceLocator->get(RatingsService::class)->getRatingData($driver, $user->getId())
338+
: null;
339+
return $this->createViewModel(compact('currentRating'));
344340
}
345341

346342
/**
@@ -420,9 +416,8 @@ protected function processSave()
420416
// Perform the save operation:
421417
$driver = $this->loadRecord();
422418
$post = $this->getRequest()->getPost()->toArray();
423-
$tagParser = $this->serviceLocator->get(\VuFind\Tags::class);
424-
$post['mytags']
425-
= $tagParser->parse($post['mytags'] ?? '');
419+
$tagParser = $this->serviceLocator->get(Tags::class);
420+
$post['mytags'] = $tagParser->parse($post['mytags'] ?? '');
426421
$favorites = $this->serviceLocator
427422
->get(\VuFind\Favorites\FavoritesService::class);
428423
$results = $favorites->save($post, $user, $driver);

0 commit comments

Comments
 (0)