Completed
Push — master ( 5429ef...771e5d )
by Jeff
04:44 queued 02:38
created

DomainKeyReaderService::getDomainKey()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 2
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
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
    /** @var DNSResolver */
18
    private $resolver;
19
20
    public function __construct(DNSResolver $resolver)
21
    {
22
        $this->resolver = $resolver;
23
    }
24
25
    /**
26
     * @param string $domain
27
     * @param string $selector
28
     *
29
     * @throws DomainKeyNotFoundException
30
     *
31
     * @return array
32
     */
33
    public function getDomainKey(string $domain, string $selector): array
34
    {
35
        $dkimDomain = \sprintf('%s._domainkey.%s', $selector, $domain);
36
        $result = $this->resolver->resolve($dkimDomain);
37
38
        if (isset($result[0]['entries'])) {
39
            $result[0]['txt'] = implode('', $result[0]['entries']);
40
        }
41
42
        $parts = explode(';', trim($result[0]['txt']));
43
        $record = [];
44
45
        foreach ($parts as $part) {
46
            [$key, $val] = explode('=', trim($part), 2);
47
            $record[$key] = $val;
48
        }
49
50
        return $record;
51
    }
52
}
53