Completed
Push — master ( 1469eb...32b27e )
by Frederik
02:28
created

Parameters::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Genkgo\Mail\Dkim;
4
5
use Genkgo\Mail\Header\HeaderValue;
6
use Genkgo\Mail\Header\HeaderValueParameter;
7
8
/**
9
 * Class Parameters
10
 * @package Genkgo\Mail\Dkim
11
 */
12
final class Parameters
13
{
14
15
    /**
16
     * @var string
17
     */
18
    private $selector;
19
20
    /**
21
     * @var string
22
     */
23
    private $domain;
24
25
    /**
26
     * @var \DateTimeImmutable
27
     */
28
    private $signatureTimestamp;
29
30
    /**
31
     * @var \DateTimeImmutable
32
     */
33
    private $signatureExpiration;
34
35
36
    /**
37
     * Parameters constructor.
38
     * @param string $selector
39
     * @param string $domain
40
     */
41 9
    public function __construct(string $selector, string $domain)
42
    {
43 9
        $this->selector = $selector;
44 9
        $this->domain = $domain;
45 9
    }
46
47
    /**
48
     * @param \DateTimeImmutable $moment
49
     * @return Parameters
50
     */
51 2
    public function withSignatureTimestamp(\DateTimeImmutable $moment): self
52
    {
53 2
        $clone = clone $this;
54 2
        $clone->signatureTimestamp = $moment;
55 2
        return $clone;
56
    }
57
58
    /**
59
     * @param \DateTimeImmutable $moment
60
     * @return Parameters
61
     */
62 1
    public function withSignatureExpiration(\DateTimeImmutable $moment): self
63
    {
64 1
        $clone = clone $this;
65 1
        $clone->signatureExpiration = $moment;
66 1
        return $clone;
67
    }
68
69
    /**
70
     * @return HeaderValue
71
     */
72 9
    public function newHeaderValue(): HeaderValue
73
    {
74 9
        $selector = \idn_to_ascii($this->selector, 0, INTL_IDNA_VARIANT_UTS46);
75
76
        // @codeCoverageIgnoreStart
77
        if ($selector === false) {
78
            throw new \UnexpectedValueException('Cannot encode selector as A-label');
79
        }
80
81
        $domain = \idn_to_ascii($this->domain, 0, INTL_IDNA_VARIANT_UTS46);
82
        if ($domain === false) {
83
            throw new \UnexpectedValueException('Cannot encode domain as A-label');
84
        }
85
        // @codeCoverageIgnoreEnd
86
87 9
        $headerValue = (new HeaderValue('v=1'))
88 9
            ->withParameter((new HeaderValueParameter('q', 'dns/txt'))->withSpecials(''))
89 9
            ->withParameter((new HeaderValueParameter('d', $domain))->withSpecials(''), true)
90 9
            ->withParameter((new HeaderValueParameter('s', $selector))->withSpecials(''));
91
92 9
        if ($this->signatureTimestamp !== null) {
93
            $signatureTimestamp = $this
94 2
                ->signatureTimestamp
95 2
                ->setTimezone(new \DateTimeZone('UTC'))
96 2
                ->format('U');
97
98 2
            $headerValue = $headerValue->withParameter((new HeaderValueParameter('t', $signatureTimestamp)));
99
        }
100
101 9
        if ($this->signatureExpiration !== null) {
102
            $signatureExpiration = $this
103 1
                ->signatureExpiration
104 1
                ->setTimezone(new \DateTimeZone('UTC'))
105 1
                ->format('U');
106
107 1
            $headerValue = $headerValue->withParameter((new HeaderValueParameter('x', $signatureExpiration)));
108
        }
109
110 9
        return $headerValue;
111
    }
112
}