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

RcptToCapability::manifest()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 0
cts 15
cp 0
rs 9.4285
cc 3
eloc 11
nc 7
nop 2
crap 12
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(), 11, -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
}