DomainKeyReaderService::getDomainKey()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 18
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App\Service\DKIM;
12
13
use App\Exception\DKIM\DomainKeyNotFoundException;
14
15
class DomainKeyReaderService
16
{
17
    private DNSResolver $resolver;
18
19
    public function __construct(DNSResolver $resolver)
20
    {
21
        $this->resolver = $resolver;
22
    }
23
24
    /**
25
     * @throws DomainKeyNotFoundException
26
     */
27
    public function getDomainKey(string $domain, string $selector): array
28
    {
29
        $dkimDomain = \sprintf('%s._domainkey.%s', $selector, $domain);
30
        $result = $this->resolver->resolve($dkimDomain);
31
32
        if (isset($result[0]['entries'])) {
33
            $result[0]['txt'] = implode('', $result[0]['entries']);
34
        }
35
36
        $parts = explode(';', trim($result[0]['txt']));
37
        $record = [];
38
39
        foreach ($parts as $part) {
40
            [$key, $val] = explode('=', trim($part), 2);
41
            $record[$key] = $val;
42
        }
43
44
        return $record;
45
    }
46
}
47