Completed
Push — master ( abdadc...adaee3 )
by Marcel
01:41
created

Router::callMailboxes()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6346
c 0
b 0
f 0
cc 7
nc 9
nop 1
1
<?php
2
3
namespace BeyondCode\Mailbox\Routing;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Container\Container;
7
use BeyondCode\Mailbox\InboundEmail;
8
9
class Router
10
{
11
    /** @var RouteCollection */
12
    protected $routes;
13
14
    /** @var Route */
15
    protected $fallbackRoute;
16
17
    /** @var Route */
18
    protected $catchAllRoute;
19
20
    /** @var Container */
21
    protected $container;
22
23
    public function __construct(Container $container = null)
24
    {
25
        $this->container = $container ?: new Container;
26
27
        $this->routes = new RouteCollection;
28
    }
29
30
    public function from(string $pattern, $action) : Route
31
    {
32
        return $this->addRoute(Route::FROM, $pattern, $action);
33
    }
34
35
    public function to(string $pattern, $action) : Route
36
    {
37
        return $this->addRoute(Route::TO, $pattern, $action);
38
    }
39
40
    public function cc(string $pattern, $action) : Route
41
    {
42
        return $this->addRoute(Route::CC, $pattern, $action);
43
    }
44
45
    public function subject(string $pattern, $action) : Route
46
    {
47
        return $this->addRoute(Route::SUBJECT, $pattern, $action);
48
    }
49
50
    public function fallback($action)
51
    {
52
        $this->fallbackRoute = $this->createRoute(Route::FALLBACK, '', $action);
53
    }
54
55
    public function catchAll($action)
56
    {
57
        $this->catchAllRoute = $this->createRoute(Route::CATCH_ALL, '', $action);
58
    }
59
60
    protected function addRoute(string $subject, string $pattern, $action) : Route
61
    {
62
        $route = $this->createRoute($subject, $pattern, $action);
63
64
        $this->routes->add($route);
65
66
        return $route;
67
    }
68
69
    protected function createRoute(string $subject, string $pattern, $action) : Route
70
    {
71
        return (new Route($subject, $pattern, $action))
72
            ->setContainer($this->container);
73
    }
74
75
    public function callMailboxes(InboundEmail $email)
76
    {
77
        if ($email->isValid()) {
78
            $matchedRoutes = $this->routes->match($email)->map(function (Route $route) use ($email) {
79
                $route->run($email);
80
            });
81
82
            if ($matchedRoutes->isEmpty() && $this->fallbackRoute) {
83
                $matchedRoutes[] = $this->fallbackRoute;
84
                $this->fallbackRoute->run($email);
85
            }
86
87
            if ($this->catchAllRoute) {
88
                $matchedRoutes[] = $this->catchAllRoute;
89
                $this->catchAllRoute->run($email);
90
            }
91
92
            if ($this->shouldStoreInboundEmails() && $this->shouldStoreAllInboundEmails($matchedRoutes)) {
93
                $this->storeEmail($email);
94
            }
95
        }
96
    }
97
98
    protected function shouldStoreInboundEmails(): bool
99
    {
100
        return config('mailbox.store_incoming_emails_for_days') > 0;
101
    }
102
103
    protected function shouldStoreAllInboundEmails(Collection $matchedRoutes): bool
104
    {
105
        return $matchedRoutes->isNotEmpty() ? true : ! config('mailbox.only_store_matching_emails');
106
    }
107
108
    protected function storeEmail(InboundEmail $email)
109
    {
110
        $email->save();
111
    }
112
}
113