HasHeaders::getHeader()   A
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 2
b 0
f 0
nc 12
nop 2
dl 0
loc 21
rs 9.2222
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Traits;
4
5
trait HasHeaders
6
{
7
8
	/**
9
	 * Gets a single header from an existing email by name.
10
	 *
11
	 * @param $headerName
12
	 *
13
	 * @param string $regex if this is set, value will be evaluated with the give regular expression.
14
	 *
15
	 * @return null|string
16
	 */
17
	public function getHeader($headerName, $regex = null)
18
	{
19
		$headers = $this->getHeaders();
20
21
		$value = null;
22
23
		foreach ($headers as $header) {
24
			if ($header->key === $headerName) {
25
				$value = $header->value;
26
				if (!is_null($regex)) {
27
					preg_match_all($regex, $header->value, $value);
28
				}
29
				break;
30
			}
31
		}
32
33
		if (is_array($value)) {
34
			return isset($value[1]) ? $value[1] : null;
35
		}
36
37
		return $value;
38
	}
39
40
	public abstract function getHeaders();
41
42
}
43