I am trying to build a function that allow me insert|update new entities but I have a problem trying to apply DRY principle of software development, aimed at reducing repetition of information of all kinds. This is what I have:
foreach ($data as $row) {
$entity = $this->_em->getRepository('PDOneBundle:Target')->findOneBy(
array('veeva_account_id' => $row['Id'])
);
$lastModifiedAt = new \DateTime($row['LastModifiedDate']);
// Constraints
if ($row['Id'] != null && $row['FirstName'] != null && $row['LastName']) {
// CREATE
if (!is_object($entity)) {
$entity = new Entity\Target();
$entity->setVeevaAccountId($row['Id']);
$entity->setVeevaAccountId($row['Id']);
$entity->setNpi($row['NPI_vod__c']);
$entity->setFirst(ucfirst(strtolower($row['FirstName'])));
$entity->setLast(ucfirst(strtolower($row['LastName'])));
$entity->setTitle($row['Title__c']);
$entity->setDisplayName(
ucfirst(strtolower($row['FirstName'])).' '.ucfirst(
strtolower($row['LastName'])
)
);
$entity->setLastSyncAt(new \DateTime());
// Persisting the current user
$this->_em->persist($entity);
// Each 1000 items persisted we flush everything
if (($i % $this->_batchSize) === 0) {
echo 'Flushing batch...'."\n";
echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
$this->_em->flush();
$this->_em->clear();
echo 'After batch...'."\n";
echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
}
++$i;
}
$lastSyncAt = $entity->getLastSyncAt();
// UPDATE
if ($lastModifiedAt > $lastSyncAt) {
// Updating info
$entity->setVeevaAccountId($row['Id']);
$entity->setNpi($row['NPI_vod__c']);
$entity->setFirst(ucfirst(strtolower($row['FirstName'])));
$entity->setLast(ucfirst(strtolower($row['LastName'])));
$entity->setTitle($row['Title__c']);
$entity->setDisplayName(
ucfirst(strtolower($row['FirstName'])).' '.ucfirst(
strtolower($row['LastName'])
)
);
$entity->setLastSyncAt(new \DateTime());
// Persisting the current user
$this->_em->persist($entity);
// Each 1000 items persisted we flush everything
if (($i % $this->_batchSize) === 0) {
echo 'Flushing batch...'."\n";
echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
$this->_em->flush();
$this->_em->clear();
echo 'After batch...'."\n";
echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
}
++$i;
}
}
}
As you may notice this piece of code:
$this->_em->persist($entity);
// Each 1000 items persisted we flush everything
if (($i % $this->_batchSize) === 0) {
echo 'Flushing batch...'."\n";
echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
$this->_em->flush();
$this->_em->clear();
echo 'After batch...'."\n";
echo 'Memory: '.$this->getReadableSize(memory_get_usage())."\n";
}
++$i;
Is present twice, then what comes to my mind is check if Doctrine has something to persist|flush and then get out the code and put below the conditionals but I don't know if that is possible either how, so any advices?
PS: Code logic changes are welcome, feel free to make me suggestions in order to achieve the same and improve my code