Completed
Push — master ( f4567f...baaaa7 )
by Marcel
01:43
created

Router::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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