Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 15 | class MailgunEventRepository extends EntityRepository |
||
| 16 | { |
||
| 17 | public function getEventCount($criteria) |
||
| 23 | |||
| 24 | public function getEvents($criteria, $orderBy, $limit, $offset) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param array $criteria |
||
| 42 | * @return QueryBuilder |
||
| 43 | */ |
||
| 44 | private function getEventsQuery($criteria) |
||
| 78 | |||
| 79 | View Code Duplication | public function getEventTypes() |
|
| 94 | |||
| 95 | View Code Duplication | public function getDomains() |
|
| 111 | |||
| 112 | View Code Duplication | public function getRecipients() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Get last known sender IP based on stored events list |
||
| 131 | * @return string|null |
||
| 132 | */ |
||
| 133 | public function getLastKnownSenderIp() |
||
| 134 | { |
||
| 135 | $q = $this->getEntityManager()->createQueryBuilder() |
||
| 136 | ->select('e.messageHeaders as mh') |
||
| 137 | ->from($this->getEntityName(), 'e') |
||
| 138 | ->orderBy('e.timestamp', 'desc') |
||
| 139 | ->where('e.event IN (:events)') |
||
| 140 | ->setParameter('events', array('opened', 'delivered', 'bounced', 'dropped', 'complained')) |
||
| 141 | ->setMaxResults(50) |
||
| 142 | ->getQuery(); |
||
| 143 | |||
| 144 | foreach($q->execute() as $next) { |
||
| 145 | if ($next['mh']){ |
||
| 146 | foreach ($next['mh'] as $nextHeader) { |
||
| 147 | if ($nextHeader[0] == 'X-Mailgun-Sending-Ip') { |
||
| 148 | return $nextHeader[1]; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | return null; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getFieldsToOrderBy() |
||
| 157 | { |
||
| 158 | return array( |
||
| 159 | 'campaignId', |
||
| 160 | 'campaignName', |
||
| 161 | 'city', |
||
| 162 | 'clientName', |
||
| 163 | 'clientOs', |
||
| 164 | 'clientType', |
||
| 165 | 'country', |
||
| 166 | 'description', |
||
| 167 | 'deviceType', |
||
| 168 | 'domain', |
||
| 169 | 'error', |
||
| 170 | 'errorCode', |
||
| 171 | 'event', |
||
| 172 | 'ip', |
||
| 173 | 'mailingList', |
||
| 174 | 'messageHeaders', |
||
| 175 | 'messageId', |
||
| 176 | 'notification', |
||
| 177 | 'reason', |
||
| 178 | 'recipient', |
||
| 179 | 'region', |
||
| 180 | 'tag', |
||
| 181 | 'timestamp', |
||
| 182 | 'type', |
||
| 183 | 'userAgent', |
||
| 184 | 'url', |
||
| 185 | ); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the most important events in the following priority |
||
| 190 | * 1. Errors (rejected, failed) |
||
| 191 | * 2. Warnings (complained, unsubscribed) |
||
| 192 | * 3. Infos (accepted, delivered, opened, clicked, stored) |
||
| 193 | * |
||
| 194 | * @param integer $count max number of events to fetch |
||
| 195 | */ |
||
| 196 | public function getImportantEvents($count) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get events by severity/type |
||
| 222 | * @param string $severity [info, warning, error] |
||
| 223 | * @return Ambigous <multitype:, unknown> |
||
| 224 | */ |
||
| 225 | public function getEventsBySeverity($severity = MailgunEvent::SEVERITY_INFO, $maxResults = 0) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get last MailgunEvent |
||
| 256 | * |
||
| 257 | * @return MailgunEvent |
||
| 258 | */ |
||
| 259 | public function getLastEvent() |
||
| 276 | } |
||
| 277 |