Completed
Pull Request — master (#17)
by Frederik
02:00
created

RcptToCapability   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 48
c 0
b 0
f 0
ccs 0
cts 23
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A manifest() 0 17 3
A advertise() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Smtp\Capability;
5
6
use Genkgo\Mail\EmailAddress;
7
use Genkgo\Mail\Protocol\ConnectionInterface;
8
use Genkgo\Mail\Protocol\Smtp\BackendInterface;
9
use Genkgo\Mail\Protocol\Smtp\CapabilityInterface;
10
use Genkgo\Mail\Protocol\Smtp\Session;
11
12
final class RcptToCapability implements CapabilityInterface
13
{
14
15
    /**
16
     * @var BackendInterface
17
     */
18
    private $backend;
19
20
    /**
21
     * RcptToCapability constructor.
22
     * @param BackendInterface $backend
23
     */
24
    public function __construct(BackendInterface $backend)
25
    {
26
        $this->backend = $backend;
27
    }
28
29
    /**
30
     * @param ConnectionInterface $connection
31
     * @param Session $session
32
     * @return Session
33
     */
34
    public function manifest(ConnectionInterface $connection, Session $session): Session
35
    {
36
        try {
37
            $address = new EmailAddress(substr($session->getCommand(), 9, -1));
38
39
            if ($this->backend->contains($address) === false) {
40
                $connection->send('550 Unknown mailbox');
41
                return $session;
42
            }
43
44
            $connection->send('250 OK');
45
            return (clone $session)->withRecipient($address);
46
        } catch (\InvalidArgumentException $e) {
47
            $connection->send('501 Invalid recipient');
48
            return $session;
49
        }
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function advertise(): string
56
    {
57
        return 'RCPT TO';
58
    }
59
}