Completed
Push — master ( ab9622...fc618d )
by Patrick
05:38
created

EmailRouter::getRouteFromDestination()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 6
nop 1
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
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;
0 ignored issues
show
Unused Code introduced by
$tmp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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');
0 ignored issues
show
Bug introduced by
The method get_data_set() does not seem to exist on object<DataSetFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
87
    }
88
}
89
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
90
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
91