8 code examples of PHP Oro\Bundle\ContactBundle\Entity\Contact extracted from open source projects
/**
* @dataProvider prePersistAndPreUpdateDataProvider
*/
public function testPreUpdate(
Contact $entity,
bool $mockToken = false,
bool $mockUser = false,
bool $detachedUser = null,
bool $reloadUser = null
) {
$oldDate = new \DateTime('2012-12-12 12:12:12');
$oldUser = new User();
$oldUser->setFirstName('oldUser');
$entity->setUpdatedAt($oldDate);
$entity->setUpdatedBy($oldUser);
$newUser = null;
if ($mockUser) {
$newUser = new User();
$newUser->setFirstName('newUser');
}
$this->mockSecurityContext($mockToken, $mockUser, $newUser);
$unitOfWork = $this->createMock(UnitOfWork::class);
$em = $this->createMock(EntityManager::class);
if ($reloadUser) {
$em->expects($this->once())
->method('find')
->with('OroUserBundle:User')
->willReturn($newUser);
} else {
$em->expects($this->never())
->method('find');
}
$em->expects($this->any())
->method('getUnitOfWork')
->willReturn($unitOfWork);
if (null !== $detachedUser) {
$unitOfWork->expects($this->once())
->method('getEntityState')
->with($newUser)
->willReturn($detachedUser ? UnitOfWork::STATE_DETACHED : UnitOfWork::STATE_MANAGED);
}
$unitOfWork->expects($this->exactly(2))
->method('propertyChanged')
->withConsecutive(
[$entity, 'updatedAt', $oldDate, $this->isInstanceOf(\DateTime::class)],
[$entity, 'updatedBy', $oldUser, $newUser]
);
$changeSet = [];
$args = new PreUpdateEventArgs($entity, $em, $changeSet);
$this->contactListener->preUpdate($entity, $args);
$this->assertInstanceOf(\DateTime::class, $entity->getUpdatedAt());
if ($mockToken && $mockUser) {
$this->assertEquals($newUser, $entity->getUpdatedBy());
} else {
$this->assertNull($entity->getUpdatedBy());
}
}
/**
* Gets a list of all phone numbers available for the given Contact object
*
*
* @return array of [phone number, phone owner]
*/
public function getPhoneNumbers(Contact $object): array
{
$result = [];
foreach ($object->getPhones() as $phone) {
$result[] = [$phone->getPhone(), $object];
}
return $result;
}
protected function processSecurityRelations(Contact $entity)
{
// update owner
$owner = $entity->getOwner();
if ($owner) {
$owner = $this->findExistingEntity($owner);
}
if (!$owner) {
$token = $this->tokenStorage->getToken();
if ($token && ($user = $token->getUser()) instanceof User) {
$owner = $user;
}
}
$entity->setOwner($owner);
// update organization
$organization = $entity->getOrganization();
if ($organization) {
$organization = $this->findExistingEntity($organization);
}
if (!$organization) {
$token = $this->tokenStorage->getToken();
if ($token && $token instanceof OrganizationAwareTokenInterface) {
$organization = $token->getOrganization();
}
}
$entity->setOrganization($organization);
}
protected function processSecurityRelations(Contact $entity)
{
// update owner
$owner = $entity->getOwner();
if ($owner) {
$owner = $this->findExistingEntity($owner);
}
if (!$owner) {
$token = $this->tokenStorage->getToken();
if ($token && ($user = $token->getUser()) instanceof User) {
$owner = $user;
}
}
$entity->setOwner($owner);
// update organization
$organization = $entity->getOrganization();
if ($organization) {
$organization = $this->findExistingEntity($organization);
}
if (!$organization) {
$token = $this->tokenStorage->getToken();
if ($token && $token instanceof OrganizationAwareTokenInterface) {
$organization = $token->getOrganization();
}
}
$entity->setOrganization($organization);
}
protected function processMultipleRelations(Contact $entity)
{
// update groups
foreach ($entity->getGroups() as $group) {
$entity->removeGroup($group);
if ($group = $this->findExistingEntity($group)) {
$entity->addGroup($group);
}
}
// clear accounts
foreach ($entity->getAccounts() as $account) {
$entity->removeAccount($account);
}
// update addresses
/** @var ContactAddress $contactAddress */
foreach ($entity->getAddresses() as $contactAddress) {
// update address types
foreach ($contactAddress->getTypes() as $addressType) {
$contactAddress->removeType($addressType);
$existingAddressType = $this->findExistingEntity($addressType);
if ($existingAddressType) {
$contactAddress->addType($existingAddressType);
}
}
}
}
protected function processMultipleRelations(Contact $entity)
{
// update groups
foreach ($entity->getGroups() as $group) {
$entity->removeGroup($group);
if ($group = $this->findExistingEntity($group)) {
$entity->addGroup($group);
}
}
// clear accounts
foreach ($entity->getAccounts() as $account) {
$entity->removeAccount($account);
}
// update addresses
/** @var ContactAddress $contactAddress */
foreach ($entity->getAddresses() as $contactAddress) {
// update address types
foreach ($contactAddress->getTypes() as $addressType) {
$contactAddress->removeType($addressType);
$existingAddressType = $this->findExistingEntity($addressType);
if ($existingAddressType) {
$contactAddress->addType($existingAddressType);
}
}
}
}
protected function processSingleRelations(Contact $entity)
{
// update source
$source = $entity->getSource();
if ($source) {
$entity->setSource($this->findExistingEntity($source));
}
// update method
$method = $entity->getMethod();
if ($method) {
$entity->setMethod($this->findExistingEntity($method));
}
// update assigned to
$assignedTo = $entity->getAssignedTo();
if ($assignedTo) {
$entity->setAssignedTo($this->findExistingEntity($assignedTo));
}
// clear reports to
$entity->setReportsTo(null);
// update created by
$createdBy = $entity->getCreatedBy();
if ($createdBy) {
$entity->setCreatedBy($this->findExistingEntity($createdBy));
}
// update updated by
$updatedBy = $entity->getUpdatedBy();
if ($updatedBy) {
$entity->setUpdatedBy($this->findExistingEntity($updatedBy));
}
}
protected function setUpdatedProperties(Contact $contact, EntityManager $entityManager, bool $update = false)
{
$newUpdatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
$newUpdatedBy = $this->getUser($entityManager);
$unitOfWork = $entityManager->getUnitOfWork();
if ($update) {
$unitOfWork->propertyChanged($contact, 'updatedAt', $contact->getUpdatedAt(), $newUpdatedAt);
$unitOfWork->propertyChanged($contact, 'updatedBy', $contact->getUpdatedBy(), $newUpdatedBy);
}
$contact->setUpdatedAt($newUpdatedAt);
$contact->setUpdatedBy($newUpdatedBy);
}