Skip to content

Commit

Permalink
OXDEV-8778 Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tkcreateit committed Nov 12, 2024
1 parent b1362f6 commit 2e9f009
Show file tree
Hide file tree
Showing 21 changed files with 404 additions and 167 deletions.
33 changes: 8 additions & 25 deletions src/ProductVote/Controller/ArticleDetailsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
namespace OxidEsales\ModuleTemplate\ProductVote\Controller;

use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\ModuleTemplate\ProductVote\Dao\ProductVoteDaoInterface;
use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVote;
use OxidEsales\ModuleTemplate\ProductVote\Service\VoteServiceInterface;

/**
* @extendable-class
Expand All @@ -35,39 +34,23 @@ public function voteDown(): void

public function resetVote(): void
{
$userId = $this->getUserId();
if (!$userId) {
$user = $this->getUser();
if (!($user instanceof User)) {
return;
}

$productVoteDao = $this->getProductVoteDao();
$productVoteDao->resetProductVote($this->getProduct()->getId(), $userId);
$voteService = $this->getService(VoteServiceInterface::class);
$voteService->resetProductVote($this->getProduct(), $user);
}

private function vote(bool $isUp): void
{
$userId = $this->getUserId();
if (!$userId) {
return;
}

$productVoteDao = $this->getProductVoteDao();
$vote = new ProductVote($this->getProduct()->getId(), $userId, $isUp);
$productVoteDao->setProductVote($vote);
}

private function getUserId(): ?string
{
$user = $this->getUser();
if (!($user instanceof User)) {
return null;
return;
}

return $user->getId();
}

private function getProductVoteDao(): ProductVoteDaoInterface
{
return $this->getService(ProductVoteDaoInterface::class);
$voteService = $this->getService(VoteServiceInterface::class);
$voteService->setProductVote($this->getProduct(), $user, $isUp);
}
}
21 changes: 9 additions & 12 deletions src/ProductVote/Dao/ProductVoteDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
use Doctrine\DBAL\Result;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidEsales\ModuleTemplate\ProductVote\DataMapper\ProductVoteDataMapperInterface;
use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVote;
use RuntimeException;
use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVoteInterface;

readonly class ProductVoteDao implements ProductVoteDaoInterface
{
Expand All @@ -23,7 +22,7 @@ public function __construct(
) {
}

public function getProductVote(string $productId, string $userId): ?ProductVote
public function getProductVote(string $productId, string $userId): ?ProductVoteInterface
{
$queryBuilder = $this->queryBuilderFactory->create();
$queryBuilder
Expand All @@ -40,22 +39,20 @@ public function getProductVote(string $productId, string $userId): ?ProductVote
'userId' => $userId,
]);

/** @var Result $result */
$result = $queryBuilder->execute();
if (!($result instanceof Result)) {
throw new RuntimeException('Query returned error.');
}

$row = $result->fetchAssociative();

if ($row === false) {
return null;
}

return $this->dataMapper->map($row);
}

public function setProductVote(ProductVote $vote): void
public function setProductVote(ProductVoteInterface $vote): void
{
$this->resetProductVote($vote->productId, $vote->userId);
$this->resetProductVote($vote->getProductId(), $vote->getUserId());

$queryBuilder = $this->queryBuilderFactory->create();
$queryBuilder
Expand All @@ -68,9 +65,9 @@ public function setProductVote(ProductVote $vote): void
])
->setParameters([
'oxid' => uniqid(),
'productId' => $vote->productId,
'userId' => $vote->userId,
'vote' => (int)$vote->vote,
'productId' => $vote->getProductId(),
'userId' => $vote->getUserId(),
'vote' => (int)$vote->isVoteUp(),
])
->execute();
}
Expand Down
6 changes: 3 additions & 3 deletions src/ProductVote/Dao/ProductVoteDaoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

namespace OxidEsales\ModuleTemplate\ProductVote\Dao;

use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVote;
use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVoteInterface;

interface ProductVoteDaoInterface
{
public function getProductVote(string $productId, string $userId): ?ProductVote;
public function getProductVote(string $productId, string $userId): ?ProductVoteInterface;

public function setProductVote(ProductVote $vote): void;
public function setProductVote(ProductVoteInterface $vote): void;
public function resetProductVote(string $productId, string $userId): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@

namespace OxidEsales\ModuleTemplate\ProductVote\Dao;

use Doctrine\DBAL\Result;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
use OxidEsales\ModuleTemplate\ProductVote\DataMapper\ResultDataMapperInterface;
use OxidEsales\ModuleTemplate\ProductVote\DataType\Result;
use RuntimeException;
use OxidEsales\ModuleTemplate\ProductVote\DataMapper\VoteResultDataMapperInterface;
use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResult;
use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResultInterface;

readonly class ResultDao implements ResultDaoInterface
readonly class VoteResultDao implements VoteResultDaoInterface
{
public function __construct(
private QueryBuilderFactoryInterface $queryBuilderFactory,
private ResultDataMapperInterface $dataMapper,
private VoteResultDataMapperInterface $dataMapper,
) {
}

public function getProductVoteResult(string $productId): Result
public function getProductVoteResult(string $productId): VoteResultInterface
{
$queryBuilder = $this->queryBuilderFactory->create();
$queryBuilder
Expand All @@ -38,15 +39,12 @@ public function getProductVoteResult(string $productId): Result
'productId' => $productId,
]);

/** @var Result $queryResult */
$queryResult = $queryBuilder->execute();
if (!($queryResult instanceof \Doctrine\DBAL\Result)) {
throw new RuntimeException('Query returned error.');
}

$row = $queryResult->fetchAssociative();

if (!$row) {
return new Result($productId, 0, 0);
return new VoteResult($productId, 0, 0);
}
return $this->dataMapper->map($row);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace OxidEsales\ModuleTemplate\ProductVote\Dao;

use OxidEsales\ModuleTemplate\ProductVote\DataType\Result;
use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResultInterface;

interface ResultDaoInterface
interface VoteResultDaoInterface
{
public function getProductVoteResult(string $productId): array|Result;
public function getProductVoteResult(string $productId): VoteResultInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

namespace OxidEsales\ModuleTemplate\ProductVote\DataMapper;

use OxidEsales\ModuleTemplate\ProductVote\DataType\Result;
use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResult;
use OxidEsales\ModuleTemplate\ProductVote\Exception\MapDataTypeException;

readonly class ResultDataMapper implements ResultDataMapperInterface
readonly class VoteResultDataMapper implements VoteResultDataMapperInterface
{
public function map(array $data): Result
public function map(array $data): VoteResult
{
if (!isset($data['ProductId']) || !isset($data['VoteUp']) || !isset($data['VoteDown'])) {
throw new MapDataTypeException();
}

return new Result($data['ProductId'], (int)$data['VoteUp'], (int)$data['VoteDown']);
return new VoteResult($data['ProductId'], (int)$data['VoteUp'], (int)$data['VoteDown']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace OxidEsales\ModuleTemplate\ProductVote\DataMapper;

use OxidEsales\ModuleTemplate\ProductVote\DataType\Result;
use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResult;

interface ResultDataMapperInterface
interface VoteResultDataMapperInterface
{
public function map(array $data): Result;
public function map(array $data): VoteResult;
}
17 changes: 16 additions & 1 deletion src/ProductVote/DataType/ProductVote.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@

namespace OxidEsales\ModuleTemplate\ProductVote\DataType;

readonly class ProductVote
readonly class ProductVote implements ProductVoteInterface
{
public function __construct(
public string $productId,
public string $userId,
public bool $vote,
) {
}

public function getProductId(): string
{
return $this->productId;
}

public function getUserId(): string
{
return $this->userId;
}

public function isVoteUp(): bool
{
return $this->vote;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@

namespace OxidEsales\ModuleTemplate\ProductVote\DataType;

readonly class Result
interface ProductVoteInterface
{
public function __construct(
public string $productId,
public int $voteUp,
public int $voteDown,
) {
}
public function getProductId(): string;
public function getUserId(): string;
public function isVoteUp(): bool;
}
35 changes: 35 additions & 0 deletions src/ProductVote/DataType/VoteResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\ModuleTemplate\ProductVote\DataType;

readonly class VoteResult implements VoteResultInterface
{
public function __construct(
private string $productId,
private int $voteUp,
private int $voteDown,
) {
}

public function getProductId(): string
{
return $this->productId;
}

public function getVoteUp(): int
{
return $this->voteUp;
}

public function getVoteDown(): int
{
return $this->voteDown;
}
}
17 changes: 17 additions & 0 deletions src/ProductVote/DataType/VoteResultInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\ModuleTemplate\ProductVote\DataType;

interface VoteResultInterface
{
public function getProductId(): string;
public function getVoteUp(): int;
public function getVoteDown(): int;
}
48 changes: 48 additions & 0 deletions src/ProductVote/Service/VoteService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\ModuleTemplate\ProductVote\Service;

use OxidEsales\Eshop\Application\Model\Article;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\ModuleTemplate\ProductVote\Dao\ProductVoteDaoInterface;
use OxidEsales\ModuleTemplate\ProductVote\Dao\VoteResultDaoInterface;
use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVote;
use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVoteInterface;
use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResultInterface;

readonly class VoteService implements VoteServiceInterface
{
public function __construct(
private ProductVoteDaoInterface $productVoteDao,
private VoteResultDaoInterface $voteResultDao,
) {
}

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
{
$vote = new ProductVote($product->getId(), $user->getId(), $vote);
$this->productVoteDao->setProductVote($vote);
}

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());
}
}
24 changes: 24 additions & 0 deletions src/ProductVote/Service/VoteServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\ModuleTemplate\ProductVote\Service;

use OxidEsales\Eshop\Application\Model\Article;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\ModuleTemplate\ProductVote\DataType\ProductVoteInterface;
use OxidEsales\ModuleTemplate\ProductVote\DataType\VoteResultInterface;

interface VoteServiceInterface
{
public function getProductVote(Article $product, User $user): ?ProductVoteInterface;
public function setProductVote(Article $product, User $user, bool $vote): void;
public function resetProductVote(Article $product, User $user): void;

public function getProductVoteResult(Article $product): VoteResultInterface;
}
Loading

0 comments on commit 2e9f009

Please sign in to comment.