Completed
Push — master ( 771e5d...491b8d )
by Jeff
02:21
created

MapGenerator::writePrivateKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
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\Config;
12
13
use App\Entity\Domain;
14
15
class MapGenerator
16
{
17
    private const MAP_FILENAME = 'dkim_selectors.map';
18
    private $path;
19
20
    public function __construct(string $path)
21
    {
22
        $this->path = $path;
23
    }
24
25
    public function generate(Domain ...$domains): void
26
    {
27
        $map = [];
28
29
        foreach ($domains as $domain) {
30
            if ($domain->getDkimEnabled()
31
                && !empty($domain->getDkimPrivateKey())
32
                && !empty($domain->getDkimSelector())) {
33
                $this->writePrivateKey($domain);
34
                $map[] = \sprintf('%s %s', $domain->getName(), $domain->getDkimSelector());
35
            }
36
        }
37
38
        $map[] = '';
39
        $this->writeFile(\sprintf('%s/%s', $this->path, static::MAP_FILENAME), \implode(\PHP_EOL, $map));
40
    }
41
42
    private function writePrivateKey(Domain $domain): void
43
    {
44
        $filename = \sprintf('%s/%s.%s.key', $this->path, $domain->getName(), $domain->getDkimSelector());
45
46
        $this->writeFile($filename, $domain->getDkimPrivateKey());
47
    }
48
49
    private function writeFile(string $filename, string $content): void
50
    {
51
        if (false === \file_put_contents($filename, $content)) {
52
            throw new \LogicException(\sprintf('Cannot write %s', $filename));
53
        }
54
    }
55
}
56