Merge pull request #22648 from colemanw/removeApiv4PreSaveSubscriber
[civicrm-core.git] / CRM / Mailing / BAO / BouncePattern.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Mailing_BAO_BouncePattern extends CRM_Mailing_DAO_BouncePattern {
18
19 /**
25606795 20 * Pseudo-constant pattern array.
7e8c8317 21 * @var array
6a488035 22 */
7e8c8317 23 public static $_patterns = NULL;
6a488035 24
6a488035 25 /**
fe482240 26 * Build the static pattern array.
6a488035
TO
27 */
28 public static function buildPatterns() {
be2fb01f 29 self::$_patterns = [];
6a488035
TO
30 $bp = new CRM_Mailing_BAO_BouncePattern();
31 $bp->find();
32
33 while ($bp->fetch()) {
34 self::$_patterns[$bp->bounce_type_id][] = $bp->pattern;
35 }
36
37 foreach (self::$_patterns as $type => $patterns) {
38 if (count($patterns) == 1) {
39 self::$_patterns[$type] = '{(' . $patterns[0] . ')}im';
40 }
41 else {
42 self::$_patterns[$type] = '{(' . implode(')|(', $patterns) . ')}im';
43 }
44 }
45 }
46
47 /**
48 * Try to match the string to a bounce type.
49 *
90c8230e
TO
50 * @param string $message
51 * The message to be matched.
6a488035 52 *
a6c01b45
CW
53 * @return array
54 * Tuple (bounce_type, bounce_reason)
6a488035 55 */
3e416f24 56 public static function match($message) {
6a488035
TO
57 // clean up $message and replace all white space by a single space, CRM-4767
58 $message = preg_replace('/\s+/', ' ', $message);
59
60 if (self::$_patterns == NULL) {
61 self::buildPatterns();
62 }
63
64 foreach (self::$_patterns as $type => $re) {
65 if (preg_match($re, $message, $matches)) {
be2fb01f 66 $bounce = [
6a488035
TO
67 'bounce_type_id' => $type,
68 'bounce_reason' => $message,
be2fb01f 69 ];
6a488035
TO
70 return $bounce;
71 }
72 }
73
be2fb01f 74 $bounce = [
6a488035
TO
75 'bounce_type_id' => NULL,
76 'bounce_reason' => $message,
be2fb01f 77 ];
6a488035
TO
78
79 return $bounce;
80 }
96025800 81
6a488035 82}