Conditions | 1 |
Paths | 6 |
Total Lines | 83 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
66 | public function factory( |
||
67 | MessageInterface $message, |
||
68 | string $domain, |
||
69 | string $selector, |
||
70 | array $parameters = [] |
||
71 | ): HeaderInterface |
||
72 | { |
||
73 | $bodyHash = $this->sign->hashBody( |
||
74 | $this->canonicalizeBody->canonicalize($message->getBody()) |
||
75 | ); |
||
76 | |||
77 | $headerCanonicalized = []; |
||
78 | $headerNames = []; |
||
79 | foreach ($message->getHeaders() as $headers) { |
||
80 | /** @var HeaderInterface $header */ |
||
81 | foreach ($headers as $header) { |
||
82 | $headerName = strtolower((string)$header->getName()); |
||
83 | if (isset(self::HEADERS_IGNORED[$headerName])) { |
||
84 | break; |
||
85 | } |
||
86 | |||
87 | // make sure we sign only the last header in the list |
||
88 | $headerCanonicalized[$headerName] = $this->canonicalizeHeader->canonicalize($header); |
||
89 | $headerNames[$headerName] = (string)$header->getName(); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | $canonicalization = [$this->canonicalizeHeader->name(), $this->canonicalizeBody->name()]; |
||
94 | |||
95 | $headerValue = (new HeaderValue('v=1')) |
||
96 | ->withParameter(new HeaderValueParameter('a', $this->sign->name())) |
||
97 | ->withParameter(new HeaderValueParameter('b', '', true)) |
||
98 | ->withParameter(new HeaderValueParameter('bh', $bodyHash, true)) |
||
99 | ->withParameter(new HeaderValueParameter('c', implode('/', $canonicalization))) |
||
100 | ->withParameter(new HeaderValueParameter('d', $domain, true)) |
||
101 | ->withParameter(new HeaderValueParameter('h', implode(' : ', $headerNames), true)) |
||
102 | ->withParameter(new HeaderValueParameter('q', 'dns/txt')) |
||
103 | ->withParameter(new HeaderValueParameter('s', $selector)); |
||
104 | |||
105 | foreach ($parameters as $key => $value) { |
||
106 | $headerValue = $headerValue->withParameter(new HeaderValueParameter($key, $value)); |
||
107 | } |
||
108 | |||
109 | $headerCanonicalized[strtolower(self::HEADER_NAME)] = $this->canonicalizeHeader->canonicalize( |
||
110 | new GenericHeader(self::HEADER_NAME, (string) $headerValue) |
||
111 | ); |
||
112 | |||
113 | $signature = base64_encode($this->sign->signHeaders(implode('', $headerCanonicalized))); |
||
114 | $headerValue = $headerValue->withParameter(new HeaderValueParameter('b', trim($signature), true)); |
||
115 | |||
116 | return new class ($headerValue) implements HeaderInterface |
||
117 | { |
||
118 | /** |
||
119 | * @var HeaderValue |
||
120 | */ |
||
121 | private $headerValue; |
||
122 | |||
123 | /** |
||
124 | * DkimSignature constructor. |
||
125 | * @param HeaderValue $headerValue |
||
126 | */ |
||
127 | public function __construct(HeaderValue $headerValue) |
||
128 | { |
||
129 | $this->headerValue = $headerValue; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return HeaderName |
||
134 | */ |
||
135 | public function getName(): HeaderName |
||
136 | { |
||
137 | return new HeaderName(HeaderV1Factory::HEADER_NAME); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return HeaderValue |
||
142 | */ |
||
143 | public function getValue(): HeaderValue |
||
144 | { |
||
145 | return $this->headerValue; |
||
146 | } |
||
147 | }; |
||
148 | } |
||
149 | |||
150 | } |