<?php | |
namespace XXX\Core\Customer\Services; | |
use XXX\Core\Appraisal\Entities\Order; | |
use XXX\Core\Appraiser\Entities\Appraiser; | |
use XXX\Core\Customer\Criteria\SorterResolver; | |
use XXX\Core\Customer\Entities\AdditionalDocumentType; | |
use XXX\Core\Customer\Entities\AdditionalStatus; | |
use XXX\Core\Customer\Entities\Customer; | |
use XXX\Core\Customer\Entities\DocumentSupportedFormats; | |
use XXX\Core\Customer\Entities\JobType; | |
use XXX\Core\Customer\Entities\Settings; | |
use XXX\Core\Customer\Options\FetchCustomerOptions; | |
use XXX\Core\Customer\Persistables\CustomerPersistable; | |
use XXX\Core\Customer\Validation\CustomerValidator; | |
use XXX\Core\Invitation\Entities\Invitation; | |
use XXX\Core\Invitation\Enums\Status; | |
use XXX\Core\Shared\Interfaces\TokenGeneratorInterface; | |
use XXX\Core\Support\Criteria\Sorting\Sorter; | |
use XXX\Core\Support\Service\AbstractService; | |
use XXX\Core\User\Interfaces\PasswordEncryptorInterface; | |
use XXX\Core\User\Services\UserService; | |
/** | |
* @author Igor Vorobiov <igor.vorobioff@gmail.com> | |
*/ | |
class CustomerService extends AbstractService | |
{ | |
/** | |
* @var UserService | |
*/ | |
private $userService; | |
/** | |
* @param UserService $userService | |
*/ | |
public function initialize(UserService $userService) | |
{ | |
$this->userService = $userService; | |
} | |
/** | |
* @return Customer[] | |
*/ | |
public function getAll() | |
{ | |
return $this->entityManager->getRepository(Customer::class)->findAll(); | |
} | |
/** | |
* @param int $appraiserId | |
* @param FetchCustomerOptions $options | |
* @return Customer[] | |
*/ | |
public function getAllByAppraiserId($appraiserId, FetchCustomerOptions $options = null) | |
{ | |
if ($options === null){ | |
$options = new FetchCustomerOptions(); | |
} | |
$builder = $this->entityManager->createQueryBuilder(); | |
$builder | |
->select('c') | |
->from(Customer::class, 'c') | |
->where($builder->expr()->isMemberOf(':appraiser', 'c.appraisers')) | |
->setParameter('appraiser', $appraiserId); | |
(new Sorter())->apply($builder, $options->getSortables(), new SorterResolver()); | |
return $builder->getQuery()->getResult(); | |
} | |
/** | |
* @param CustomerPersistable $persistable | |
* @return Customer[] | |
*/ | |
public function create(CustomerPersistable $persistable) | |
{ | |
(new CustomerValidator($this->userService))->validate($persistable); | |
$customer = new Customer(); | |
$this->transfer($persistable, $customer, [ | |
'ignore' => ['password'] | |
]); | |
/** | |
* @var PasswordEncryptorInterface $encryptor | |
*/ | |
$encryptor = $this->container->get(PasswordEncryptorInterface::class); | |
$customer->setPassword($encryptor->encrypt($persistable->getPassword())); | |
$this->entityManager->persist($customer); | |
$this->entityManager->flush(); | |
$settings = new Settings(); | |
$settings->setCustomer($customer); | |
/** | |
* @var TokenGeneratorInterface $generator | |
*/ | |
$generator = $this->container->get(TokenGeneratorInterface::class); | |
$customer->setSecret1($generator->generate()); | |
$customer->setSecret2($generator->generate()); | |
$this->entityManager->persist($settings); | |
$this->entityManager->flush(); | |
$customer->setSettings($settings); | |
return $customer; | |
} | |
/** | |
* @param int $id | |
* @return bool | |
*/ | |
public function exists($id) | |
{ | |
return $this->entityManager->getRepository(Customer::class)->exists(['id' => $id]); | |
} | |
/** | |
* @param int $customerId | |
* @param int $invitationId | |
* @return bool | |
*/ | |
public function hasInvitation($customerId, $invitationId) | |
{ | |
return $this->entityManager | |
->getRepository(Invitation::class) | |
->exists(['customer' => $customerId, 'id' => $invitationId]); | |
} | |
/** | |
* @param int $customerId | |
* @param int $ascAppraiserId | |
* @return bool | |
*/ | |
public function hasPendingInvitationForAscAppraiser($customerId, $ascAppraiserId) | |
{ | |
return $this->entityManager | |
->getRepository(Invitation::class) | |
->exists(['ascAppraiser' => $ascAppraiserId, 'customer' => $customerId, 'status' => Status::PENDING]); | |
} | |
/** | |
* @param int $customerId | |
* @param int $appraiserId | |
* @return bool | |
*/ | |
public function hasPendingInvitationForAppraiser($customerId, $appraiserId) | |
{ | |
return $this->entityManager | |
->getRepository(Invitation::class) | |
->exists(['appraiser' => $appraiserId, 'customer' => $customerId]); | |
} | |
/** | |
* @param int $customerId | |
* @param int $orderId | |
* @return bool | |
*/ | |
public function hasOrder($customerId, $orderId) | |
{ | |
return $this->entityManager | |
->getRepository(Order::class) | |
->exists(['customer' => $customerId, 'id' => $orderId]); | |
} | |
/** | |
* @param int $customerId | |
* @return Appraiser[] | |
*/ | |
public function getAllAppraisers($customerId) | |
{ | |
/** | |
* @var Customer $customer | |
*/ | |
$customer = $this->entityManager->find(Customer::class, $customerId); | |
return $customer->getAppraisers(); | |
} | |
/** | |
* @param int $customerId | |
* @param int $appraiserId | |
* @return bool | |
*/ | |
public function isRelatedWithAppraiser($customerId, $appraiserId) | |
{ | |
return $this->entityManager | |
->getRepository(Customer::class) | |
->exists(['appraisers' => ['HAVE MEMBER', $appraiserId], 'id' => $customerId]); | |
} | |
/** | |
* @param int $customerId | |
* @param int $formatsId | |
* @return bool | |
*/ | |
public function hasDocumentSupportedFormats($customerId, $formatsId) | |
{ | |
return $this->entityManager | |
->getRepository(DocumentSupportedFormats::class) | |
->exists(['id' => $formatsId, 'customer' => $customerId]); | |
} | |
/** | |
* @param int $customerId | |
* @param int $typeId | |
* @return bool | |
*/ | |
public function hasAdditionalDocumentType($customerId, $typeId) | |
{ | |
return $this->entityManager | |
->getRepository(AdditionalDocumentType::class) | |
->exists(['id' => $typeId, 'customer' => $customerId]); | |
} | |
/** | |
* @param int $customerId | |
* @param int $jobTypeId | |
* @return bool | |
*/ | |
public function hasVisibleJobType($customerId, $jobTypeId) | |
{ | |
return $this->entityManager | |
->getRepository(JobType::class) | |
->exists(['customer' => $customerId, 'id' => $jobTypeId, 'isHidden' => false]); | |
} | |
/** | |
* @param int $customerId | |
* @param array $jobTypeIds | |
* @return bool | |
*/ | |
public function hasVisibleJobTypes($customerId, array $jobTypeIds) | |
{ | |
return count($jobTypeIds) === $this->entityManager | |
->getRepository(JobType::class) | |
->count(['customer' => $customerId, 'id' => ['in', $jobTypeIds], 'isHidden' => false]); | |
} | |
/** | |
* @param int $customerId | |
* @param int $additionalStatusId | |
* @return bool | |
*/ | |
public function hasActiveAdditionalStatus($customerId, $additionalStatusId) | |
{ | |
return $this->entityManager->getRepository(AdditionalStatus::class) | |
->exists(['customer' => $customerId, 'id' => $additionalStatusId, 'isActive' => true]); | |
} | |
/** | |
* @param int $customerId | |
* @param string $title | |
* @param int $exclude | |
* @return bool | |
*/ | |
public function hasActiveAdditionalStatusByTitle($customerId, $title, $exclude = null) | |
{ | |
$criteria = ['customer' => $customerId, 'title' => $title, 'isActive' => true]; | |
if ($exclude !== null){ | |
$criteria['id'] = ['!=', $exclude]; | |
} | |
return $this->entityManager | |
->getRepository(AdditionalStatus::class) | |
->exists($criteria); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment