|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Email Router class |
|
4
|
|
|
* |
|
5
|
|
|
* This file describes the Email Router class |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 5 and 7 |
|
8
|
|
|
* |
|
9
|
|
|
* @author Patrick Boyd / [email protected] |
|
10
|
|
|
* @copyright Copyright (c) 2016, Austin Artistic Reconstruction |
|
11
|
|
|
* @license http://www.apache.org/licenses/ Apache 2.0 License |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Email; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* An class to represent an Email Router |
|
18
|
|
|
*/ |
|
19
|
|
|
class EmailRouter extends \Singleton |
|
20
|
|
|
{ |
|
21
|
|
|
protected function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function routeSingle($destination, $rawMessage) |
|
26
|
|
|
{ |
|
27
|
|
|
$route = $this->getRouteFromDestination($destination); |
|
28
|
|
|
if($route === false || $route === null) |
|
29
|
|
|
{ |
|
30
|
|
|
return false; |
|
31
|
|
|
} |
|
32
|
|
|
return $route->route($destination, $rawMessage); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function routeMany($destinations, $rawMessage) |
|
36
|
|
|
{ |
|
37
|
|
|
$ret = array(); |
|
38
|
|
|
$tmp = false; |
|
|
|
|
|
|
39
|
|
|
$error = false; |
|
40
|
|
|
foreach($destinations as $dest) |
|
41
|
|
|
{ |
|
42
|
|
|
$tmp = $this->routeSingle($dest, $rawMessage); |
|
43
|
|
|
$ret[] = $tmp; |
|
44
|
|
|
if($tmp === false) $error = true; |
|
45
|
|
|
} |
|
46
|
|
|
if($error === true) |
|
47
|
|
|
{ |
|
48
|
|
|
return $ret; |
|
49
|
|
|
} |
|
50
|
|
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getRouteFromDestination($destination) |
|
54
|
|
|
{ |
|
55
|
|
|
$dataSet = \DataSetFactory::get_data_set('email'); |
|
|
|
|
|
|
56
|
|
|
$dataTable = $dataSet['Routes']; |
|
57
|
|
|
if(!(preg_match('/(.*)@(.*)/', $destination, $parts))) |
|
58
|
|
|
{ |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
$routeData = $dataTable->read(array('pattern'=>array('$regex'=>new \MongoRegex('/'.$parts[1].'@/i')))); |
|
62
|
|
|
if($routeData === false) |
|
63
|
|
|
{ |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
$count = count($routeData); |
|
67
|
|
|
if($count === 0) |
|
68
|
|
|
{ |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
else if($count > 1) |
|
72
|
|
|
{ |
|
73
|
|
|
//TODO Best Fit |
|
74
|
|
|
throw new \Exception('Multiple Routes match! Not implemented!'); |
|
75
|
|
|
} |
|
76
|
|
|
$routeData = $routeData[0]; |
|
77
|
|
|
$type = $routeData['type']; |
|
78
|
|
|
if(isset($routeData['data'])) |
|
79
|
|
|
{ |
|
80
|
|
|
return new $type($routeData['data']); |
|
81
|
|
|
} |
|
82
|
|
|
else |
|
83
|
|
|
{ |
|
84
|
|
|
return new $type(); |
|
85
|
|
|
} |
|
86
|
|
|
return false; |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
|
90
|
|
|
?> |
|
|
|
|
|
|
91
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.