-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVoteService.php
More file actions
55 lines (45 loc) · 1.96 KB
/
VoteService.php
File metadata and controls
55 lines (45 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* Copyright © . All rights reserved.
* See LICENSE file for license details.
*/
declare(strict_types=1);
namespace OxidEsales\ExamplesModule\ProductVote\Service;
use OxidEsales\Eshop\Application\Model\Article;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\ExamplesModule\ProductVote\Dao\ProductVoteDaoInterface;
use OxidEsales\ExamplesModule\ProductVote\Dao\VoteResultDaoInterface;
use OxidEsales\ExamplesModule\ProductVote\DataObject\ProductVote;
use OxidEsales\ExamplesModule\ProductVote\DataObject\ProductVoteInterface;
use OxidEsales\ExamplesModule\ProductVote\DataObject\VoteResultInterface;
use OxidEsales\ExamplesModule\ProductVote\Event\ProductVotedEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
readonly class VoteService implements VoteServiceInterface
{
public function __construct(
private ProductVoteDaoInterface $productVoteDao,
private VoteResultDaoInterface $voteResultDao,
private EventDispatcherInterface $eventDispatcher,
) {
}
public function getProductVote(Article $product, User $user): ?ProductVoteInterface
{
return $this->productVoteDao->getProductVote($product->getId(), $user->getId());
}
public function setProductVote(Article $product, User $user, bool $vote): void
{
$productVote = new ProductVote($product->getId(), $user->getId(), $vote);
$this->productVoteDao->setProductVote($productVote);
// Dispatch custom event to allow other modules/subscribers to react to voting
$event = new ProductVotedEvent($productVote);
$this->eventDispatcher->dispatch($event);
}
public function resetProductVote(Article $product, User $user): void
{
$this->productVoteDao->resetProductVote($product->getId(), $user->getId());
}
public function getProductVoteResult(Article $product): VoteResultInterface
{
return $this->voteResultDao->getProductVoteResult($product->getId());
}
}