Completed
Push — master ( fb03b4...646b2a )
by smiley
02:45
created

EndpointAbstract::checkEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Class EndpointAbstract
4
 *
5
 * @filesource   EndpointAbstract.php
6
 * @created      06.04.2016
7
 * @package      chillerlan\Threema\Endpoint
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2016 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Threema\Endpoint;
14
15
use chillerlan\Threema\GatewayOptions;
16
use Dotenv\Dotenv;
17
18
/**
19
 *
20
 */
21
abstract class EndpointAbstract implements EndpointInterface{
22
23
	/**
24
	 * @var \chillerlan\Threema\GatewayOptions
25
	 */
26
	protected $gatewayOptions;
27
28
	/**
29
	 * TinyCurlEndpoint constructor.
30
	 *
31
	 * @param \chillerlan\Threema\GatewayOptions $gatewayOptions
32
	 */
33
	public function __construct(GatewayOptions $gatewayOptions){
34
		$this->gatewayOptions = $gatewayOptions;
35
36
		(new Dotenv($gatewayOptions->configPath, $gatewayOptions->configFilename))->load();
37
	}
38
39
	/**
40
	 * @param string $threemaID
41
	 *
42
	 * @return string
43
	 * @throws \chillerlan\Threema\Endpoint\EndpointException
44
	 */
45
	protected function checkThreemaID(string $threemaID):string{
46
47
		if(preg_match('/^[a-z\d\*]{8}$/i', $threemaID)){
48
			return strtoupper($threemaID);
49
		}
50
51
		throw new EndpointException('invalid threema id');
52
	}
53
54
	/**
55
	 * @param string $phoneNo
56
	 *
57
	 * @return string
58
	 * @throws \chillerlan\Threema\Endpoint\EndpointException
59
	 */
60
	protected function checkPhoneNo(string $phoneNo):string{
61
		$phoneNo = preg_replace('/[^\d]/', '', $phoneNo);
62
63
		if(empty($phoneNo)){
64
			throw new EndpointException('invalid phone number');
65
		}
66
67
		return (string)$phoneNo;
68
	}
69
70
	/**
71
	 * @param $email
72
	 *
73
	 * @return string
74
	 * @throws \chillerlan\Threema\Endpoint\EndpointException
75
	 */
76
	protected function checkEmail($email):string{
77
		$email = filter_var(trim($email), FILTER_VALIDATE_EMAIL);
78
79
		if(empty($email)){
80
			throw new EndpointException('invalid email');
81
		}
82
83
		return strtolower($email);
84
	}
85
86
	/**
87
	 * @param string $hash
88
	 *
89
	 * @return string
90
	 * @throws \chillerlan\Threema\Endpoint\EndpointException
91
	 */
92
	protected function checkHash(string $hash):string{
93
94
		if(preg_match('/^[a-f\d]{64}$/i', $hash)){
95
			return $hash;
96
		}
97
98
		throw new EndpointException('invalid hash');
99
	}
100
101
}
102