| Conditions | 10 |
| Paths | 2 |
| Total Lines | 94 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 70 | public function recalculate(AbstractLogger $logger) |
||
| 71 | { |
||
| 72 | $logger->notice('Recalculating contacting activities...'); |
||
| 73 | $logger->info(sprintf('<info>Processing started at %s</info>', date('Y-m-d H:i:s'))); |
||
| 74 | |||
| 75 | /** @var ConfigProvider $activityConfigProvider */ |
||
| 76 | $activityConfigProvider = $this->getContainer()->get('oro_entity_config.provider.activity'); |
||
| 77 | |||
| 78 | /** @var ActivityContactProvider $activityContactProvider */ |
||
| 79 | $activityContactProvider = $this->getContainer()->get('orocrm_activity_contact.provider'); |
||
| 80 | $contactingActivityClasses = $activityContactProvider->getSupportedActivityClasses(); |
||
| 81 | |||
| 82 | $entityConfigsWithApplicableActivities = $activityConfigProvider->filter( |
||
| 83 | function (ConfigInterface $entity) use ($contactingActivityClasses) { |
||
| 84 | return |
||
| 85 | $entity->get('activities') |
||
| 86 | && array_intersect($contactingActivityClasses, $entity->get('activities')); |
||
| 87 | } |
||
| 88 | ); |
||
| 89 | |||
| 90 | if ($entityConfigsWithApplicableActivities) { |
||
|
|
|||
| 91 | $logger->info( |
||
| 92 | sprintf( |
||
| 93 | '<comment>Total found %d entities with enabled contacting activities</comment>', |
||
| 94 | count($entityConfigsWithApplicableActivities) |
||
| 95 | ) |
||
| 96 | ); |
||
| 97 | $this->em = $this->getContainer()->get('doctrine')->getManager(); |
||
| 98 | $this->activityListRepository = $this->em->getRepository(ActivityList::ENTITY_NAME); |
||
| 99 | |||
| 100 | /** @var ActivityListener $activityListener */ |
||
| 101 | $activityListener = $this->getContainer()->get('orocrm_activity_contact.listener.activity_listener'); |
||
| 102 | /** @var ActivityListFilterHelper $activityListHelper */ |
||
| 103 | $activityListHelper = $this->getContainer()->get('oro_activity_list.filter.helper'); |
||
| 104 | |||
| 105 | foreach ($entityConfigsWithApplicableActivities as $activityScopeConfig) { |
||
| 106 | $entityClassName = $activityScopeConfig->getId()->getClassName(); |
||
| 107 | if (TargetExcludeList::isExcluded($entityClassName)) { |
||
| 108 | continue; |
||
| 109 | } |
||
| 110 | $offset = 0; |
||
| 111 | $startTimestamp = time(); |
||
| 112 | $allRecordIds = $this->getTargetIds($entityClassName); |
||
| 113 | $this->resetRecordsWithoutActivities($entityClassName, $allRecordIds); |
||
| 114 | while ($allRecords = $this->getRecordsToRecalculate($entityClassName, $allRecordIds, $offset)) { |
||
| 115 | $needsFlush = false; |
||
| 116 | foreach ($allRecords as $record) { |
||
| 117 | $this->resetRecordStatistic($record); |
||
| 118 | /** @var QueryBuilder $qb */ |
||
| 119 | $qb = $this->activityListRepository->getBaseActivityListQueryBuilder( |
||
| 120 | $entityClassName, |
||
| 121 | $record->getId() |
||
| 122 | ); |
||
| 123 | $activityListHelper->addFiltersToQuery( |
||
| 124 | $qb, |
||
| 125 | ['activityType' => ['value' => $contactingActivityClasses]] |
||
| 126 | ); |
||
| 127 | |||
| 128 | /** @var ActivityList[] $activities */ |
||
| 129 | $activities = $qb->getQuery()->getResult(); |
||
| 130 | if ($activities) { |
||
| 131 | foreach ($activities as $activityListItem) { |
||
| 132 | /** @var object $activity */ |
||
| 133 | $activity = $this->em->getRepository($activityListItem->getRelatedActivityClass()) |
||
| 134 | ->find($activityListItem->getRelatedActivityId()); |
||
| 135 | |||
| 136 | $activityListener->onAddActivity(new ActivityEvent($activity, $record)); |
||
| 137 | } |
||
| 138 | $this->em->persist($record); |
||
| 139 | $needsFlush = true; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | if ($needsFlush) { |
||
| 143 | $this->em->flush(); |
||
| 144 | } |
||
| 145 | $this->em->clear(); |
||
| 146 | $offset += self::BATCH_SIZE; |
||
| 147 | } |
||
| 148 | |||
| 149 | $endTimestamp = time(); |
||
| 150 | $logger->info( |
||
| 151 | sprintf( |
||
| 152 | 'Entity "%s", %d records processed (<comment>%d sec.</comment>).', |
||
| 153 | $entityClassName, |
||
| 154 | count($allRecordIds), |
||
| 155 | ($endTimestamp - $startTimestamp) |
||
| 156 | ) |
||
| 157 | ); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | $logger->info(sprintf('<info>Processing finished at %s</info>', date('Y-m-d H:i:s'))); |
||
| 161 | |||
| 162 | return self::STATUS_SUCCESS; |
||
| 163 | } |
||
| 164 | |||
| 280 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.