Skip to content

Commit

Permalink
bugfix: address and proxy class
Browse files Browse the repository at this point in the history
  • Loading branch information
ad3n committed Nov 18, 2017
1 parent e42547e commit c9c0863
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .php_cs.cache

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/Component/Address/Service/DefaultAddressChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace KejawenLab\Application\SemartHris\Component\Address\Service;

use Doctrine\Common\Util\ClassUtils;
use KejawenLab\Application\SemartHris\Component\Address\Model\AddressInterface;
use KejawenLab\Application\SemartHris\Component\Address\Repository\AddressRepositoryFactory;

Expand Down Expand Up @@ -32,7 +33,8 @@ public function unsetDefaultExcept(AddressInterface $address): void
return;
}

$repository = $this->addressRepositoryFactory->getRepositoryFor(get_class($address));
$realClass = ClassUtils::getRealClass(get_class($address));
$repository = $this->addressRepositoryFactory->getRepositoryFor($realClass);
$repository->unsetDefaultExcept($address);

$addressable = $address->getAddressable();
Expand All @@ -47,7 +49,8 @@ public function unsetDefaultExcept(AddressInterface $address): void
*/
public function setRandomDefault(AddressInterface $address): void
{
$repository = $this->addressRepositoryFactory->getRepositoryFor(get_class($address));
$realClass = ClassUtils::getRealClass(get_class($address));
$repository = $this->addressRepositoryFactory->getRepositoryFor($realClass);
$newDefault = $repository->setRandomDefault();

$addressable = $address->getAddressable();
Expand Down
18 changes: 0 additions & 18 deletions src/Repository/AddressRepositoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,12 @@
use KejawenLab\Application\SemartHris\Component\Address\Model\Addressable;
use KejawenLab\Application\SemartHris\Component\Address\Model\AddressInterface;
use KejawenLab\Application\SemartHris\Util\StringUtil;
use KejawenLab\Application\SemartHris\Util\UuidUtil;

/**
* @author Muhamad Surya Iksanudin <[email protected]>
*/
trait AddressRepositoryTrait
{
/**
* @param AddressInterface $address
*/
public function unsetDefaultExcept(AddressInterface $address): void
{
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->from($this->getAddressClass(), 'o');
$queryBuilder->update();
$queryBuilder->set('o.defaultAddress', $queryBuilder->expr()->literal(false));
if ($address->getId() && UuidUtil::isValid($address->getId())) {
$queryBuilder->andWhere($queryBuilder->expr()->neq('o.id', $queryBuilder->expr()->literal($address->getId())));
}

$queryBuilder->getQuery()->execute();
}

public function setRandomDefault(): AddressInterface
{
/** @var AddressInterface $other */
Expand Down
28 changes: 28 additions & 0 deletions src/Repository/CompanyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use KejawenLab\Application\SemartHris\Component\Address\Model\AddressInterface;
use KejawenLab\Application\SemartHris\Component\Address\Repository\AddressRepositoryInterface;
use KejawenLab\Application\SemartHris\Component\Company\Model\CompanyAddressInterface;
use KejawenLab\Application\SemartHris\Component\Company\Model\CompanyInterface;
Expand Down Expand Up @@ -73,6 +74,33 @@ public function findAddress(string $companyAddressId): ? CompanyAddressInterface
return $this->entityManager->getRepository($this->getAddressClass())->find($companyAddressId);
}

/**
* @param AddressInterface $address
*/
public function unsetDefaultExcept(AddressInterface $address): void
{/** @var CompanyAddressInterface $address */
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->from($this->getAddressClass(), 'o');
$queryBuilder->update();
$queryBuilder->set('o.defaultAddress', $queryBuilder->expr()->literal(false));

$company = $address->getCompany();
if ($company && UuidUtil::isValid($company->getId())) {
$queryBuilder->andWhere($queryBuilder->expr()->eq('o.company', $queryBuilder->expr()->literal($company->getId())));
}

if (UuidUtil::isValid($address->getId())) {
$queryBuilder->andWhere($queryBuilder->expr()->neq('o.id', $queryBuilder->expr()->literal($address->getId())));
}

$queryBuilder->getQuery()->execute();

$address->setDefaultAddress(true);
$this->entityManager->persist($address);
$this->entityManager->flush();
}

/**
* @param null|string $sortField
* @param null|string $sortDirection
Expand Down
24 changes: 24 additions & 0 deletions src/Repository/EmployeeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace KejawenLab\Application\SemartHris\Repository;

use Doctrine\ORM\QueryBuilder;
use KejawenLab\Application\SemartHris\Component\Address\Model\AddressInterface;
use KejawenLab\Application\SemartHris\Component\Address\Repository\AddressRepositoryInterface;
use KejawenLab\Application\SemartHris\Component\Company\Model\CompanyInterface;
use KejawenLab\Application\SemartHris\Component\Contract\Repository\ContractableRepositoryInterface;
Expand Down Expand Up @@ -101,6 +102,29 @@ public function findByCompany(string $companyId): array
return $queryBuilder->getQuery()->getResult();
}

/**
* @param AddressInterface $address
*/
public function unsetDefaultExcept(AddressInterface $address): void
{/** @var EmployeeAddressInterface $address */
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->from($this->getAddressClass(), 'o');
$queryBuilder->update();
$queryBuilder->set('o.defaultAddress', $queryBuilder->expr()->literal(false));

$employee = $address->getEmployee();
if ($employee && UuidUtil::isValid($employee->getId())) {
$queryBuilder->andWhere($queryBuilder->expr()->eq('o.employee', $queryBuilder->expr()->literal($employee->getId())));
}

if (UuidUtil::isValid($address->getId())) {
$queryBuilder->andWhere($queryBuilder->expr()->neq('o.id', $queryBuilder->expr()->literal($address->getId())));
}

$queryBuilder->getQuery()->execute();
}

/**
* @param string $id
*
Expand Down
6 changes: 4 additions & 2 deletions src/Security/Provider/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace KejawenLab\Application\SemartHris\Security\Provider;

use Doctrine\Common\Util\ClassUtils;
use KejawenLab\Application\SemartHris\Component\User\Model\UserInterface as BaseUser;
use KejawenLab\Application\SemartHris\Component\User\Repository\UserRepositoryInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
Expand Down Expand Up @@ -52,8 +53,9 @@ public function loadUserByUsername($username)
*/
public function refreshUser(UserInterface $user)
{
if (!$this->supportsClass(get_class($user))) {
throw new UnsupportedUserException(sprintf('Object class "%s" is not supported', get_class($user)));
$realClass = ClassUtils::getRealClass(get_class($user));
if (!$this->supportsClass($realClass)) {
throw new UnsupportedUserException(sprintf('Object class "%s" is not supported', $realClass));
}

return $this->userRepository->findByUsername($user->getUsername());
Expand Down
2 changes: 1 addition & 1 deletion templates/app/company/address.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
var toggleRequest = $.ajax({ type: "GET", url: toggleUrl, data: {} });
toggleRequest.done(function() {
window.location.reload();
window.location.reload(true);
});
toggleRequest.fail(function() {
Expand Down
2 changes: 1 addition & 1 deletion templates/app/company/department.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
var toggleRequest = $.ajax({ type: "GET", url: toggleUrl, data: {} });
toggleRequest.done(function() {
window.location.reload();
window.location.reload(true);
});
toggleRequest.fail(function() {
Expand Down
2 changes: 1 addition & 1 deletion templates/app/employee/address.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
var toggleRequest = $.ajax({ type: "GET", url: toggleUrl, data: {} });
toggleRequest.done(function() {
window.location.reload();
window.location.reload(true);
});
toggleRequest.fail(function() {
Expand Down

0 comments on commit c9c0863

Please sign in to comment.