Completed
Pull Request — master (#64)
by Frederik
03:38 queued 01:09
created

ArrayBackend::store()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 5
nop 3
crap 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Smtp\Backend;
5
6
use Genkgo\Mail\EmailAddress;
7
use Genkgo\Mail\MessageInterface;
8
use Genkgo\Mail\Protocol\Smtp\BackendInterface;
9
10
final class ArrayBackend implements BackendInterface
11
{
12
    /**
13
     * @var array<string, string>
14
     */
15
    private $addresses;
16
17
    /**
18
     * @var \ArrayAccess<string, mixed>
19
     */
20
    private $backend;
21
22
    /**
23
     * @param array<int, string> $addresses
24
     * @param \ArrayAccess<string, mixed> $backend
25
     */
26 13
    public function __construct(array $addresses, \ArrayAccess $backend)
27
    {
28 13
        $addresses = \array_combine(
29 13
            $addresses,
30 13
            \array_fill(0, \count($addresses), true)
31
        );
32
33 13
        if ($addresses === false) {
34
            throw new \UnexpectedValueException('Cannot combine arrays');
35
        }
36
37 13
        $this->addresses = $addresses;
38 13
        $this->backend = $backend;
39 13
    }
40
41
    /**
42
     * @param EmailAddress $mailbox
43
     * @return bool
44
     */
45 3
    public function contains(EmailAddress $mailbox): bool
46
    {
47 3
        return isset($this->addresses[(string)$mailbox]);
48
    }
49
50
    /**
51
     * @param EmailAddress $mailbox
52
     * @param MessageInterface $message
53
     * @param string $folder
54
     */
55 3
    public function store(EmailAddress $mailbox, MessageInterface $message, string $folder): void
56
    {
57 3
        if (!isset($this->addresses[(string)$mailbox])) {
58 1
            throw new \UnexpectedValueException('Unknown mailbox');
59
        }
60
61 2
        if (!isset($this->backend[(string)$mailbox])) {
62 2
            $this->backend[(string)$mailbox] = [];
63
        }
64
65 2
        if (!isset($this->backend[(string)$mailbox][$folder])) {
66 2
            $this->backend[(string)$mailbox][$folder] = [];
67
        }
68
69 2
        $this->backend[(string)$mailbox][$folder][] = $message;
70 2
    }
71
}
72