Completed
Pull Request — master (#18)
by Frederik
02:18
created

Parameters::newHeaderValue()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.439
c 0
b 0
f 0
cc 5
eloc 24
nc 6
nop 0
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
    public function __construct(string $selector, string $domain)
42
    {
43
        $this->selector = $selector;
44
        $this->domain = $domain;
45
    }
46
47
    /**
48
     * @param \DateTimeImmutable $moment
49
     * @return Parameters
50
     */
51
    public function withSignatureTimestamp(\DateTimeImmutable $moment): self
52
    {
53
        $clone = clone $this;
54
        $clone->signatureTimestamp = $moment;
55
        return $clone;
56
    }
57
58
    /**
59
     * @param \DateTimeImmutable $moment
60
     * @return Parameters
61
     */
62
    public function withSignatureExpiration(\DateTimeImmutable $moment): self
63
    {
64
        $clone = clone $this;
65
        $clone->signatureExpiration = $moment;
66
        return $clone;
67
    }
68
69
    /**
70
     * @return HeaderValue
71
     */
72
    public function newHeaderValue(): HeaderValue
73
    {
74
        $selector = idn_to_ascii($this->selector);
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);
82
        if ($domain === false) {
83
            throw new \UnexpectedValueException('Cannot encode domain as A-label');
84
        }
85
        // @codeCoverageIgnoreEnd
86
87
        $headerValue = (new HeaderValue('v=1'))
88
            ->withParameter((new HeaderValueParameter('q', 'dns/txt'))->withSpecials(''))
89
            ->withParameter((new HeaderValueParameter('d', $domain))->withSpecials(''), true)
90
            ->withParameter((new HeaderValueParameter('s', $selector))->withSpecials(''));
91
92
        if ($this->signatureTimestamp !== null) {
93
            $signatureTimestamp = $this
94
                ->signatureTimestamp
95
                ->setTimezone(new \DateTimeZone('UTC'))
96
                ->format('U');
97
98
            $headerValue = $headerValue->withParameter((new HeaderValueParameter('t', $signatureTimestamp)));
99
        }
100
101
        if ($this->signatureExpiration !== null) {
102
            $signatureExpiration = $this
103
                ->signatureExpiration
104
                ->setTimezone(new \DateTimeZone('UTC'))
105
                ->format('U');
106
107
            $headerValue = $headerValue->withParameter((new HeaderValueParameter('x', $signatureExpiration)));
108
        }
109
110
        return $headerValue;
111
    }
112
}