Completed
Push — master ( a97518...e04cfc )
by Roman
01:40
created

Sender   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 99
Duplicated Lines 2.02 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 2
loc 99
ccs 41
cts 41
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A config() 0 8 1
A send() 0 25 4
A getSignature() 0 4 1
A __construct() 0 5 3
A isSuccess() 0 4 3
A checkConfig() 2 8 4
A check() 0 10 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace RM\SMSender\EuroSms;
4
5
use Nette\Utils\Strings;
6
use RM;
7
use RM\SMSender\BaseSender;
8
use RM\SMSender\ISender;
9
use RM\SMSender\IMessage;
10
use RM\SMSender\Exception;
11
use RM\SMSender\ConfigurationException;
12
use RM\SMSender\GatewayException;
13
use RM\SMSender\MissingParameterException;
14
15
/**
16
 * Sender for service EuroSms.sk
17
 * @method onBeforeSend(IMessage $message)
18
 * @method onSuccess(IMessage $message, $response)
19
 * @method onError(IMessage $message, $response, Exception $e)
20
 */
21 1
class Sender extends BaseSender implements ISender
22
{
23
	CONST URL = 'https://as.eurosms.com/api/v2/Sender';
24
25
	/** @var string */
26
	private $id;
27
28
	/** @var string */
29
	private $key;
30
31
32
	public function __construct(array $config = NULL)
33
	{
34 1
		if (is_array($config) && !empty($config))
35 1
			$this->config($config);
36 1
	}
37
38
	/**
39
	 * @param  array $config
40
	 * @return self
41
	 */
42
	public function config($config)
43
	{
44 1
		$this->checkConfig($config['id'], $config['key']);
45
46 1
		$this->id = $config['id'];
47 1
		$this->key = $config['key'];
48 1
		return $this;
49
	}
50
51
	/**
52
	 * @throws RM\SMSender\Exception
53
	 * @return bool|string ID of Message
54
	 */
55
	public function send(IMessage $message)
56
	{
57 1
		$this->check($message);
58 1
		$this->onBeforeSend($message);
59 1
		$res = $this->getHttpClient()->request('GET', self::URL . '?' . str_replace(urlencode($message->getTo()), $message->getTo(), http_build_query([
60 1
				'action' => ($this->debug ? 'validate' : 'send') . '1SMSHTTP',
61 1
				'iid' => $this->id,
62 1
				'signature' => $this->getSignature($message),
63 1
				'from' => $message->getFrom(),
64 1
				'to' => $message->getTo(),
65 1
				'message' => $message->getText(),
66 1
				'flags' => 0x002,
67
			])));
68 1
		$response = $res->getBody();
69 1
		if ($this->isSuccess($response)) {
70 1
			$this->onSuccess($message, $response);
71
		} else {
72 1
			$e = new GatewayException($response);
73 1
			$this->onError($message, $response, $e);
74 1
			throw $e;
75
		}
76 1
		return ($this->debug)
77 1
			? TRUE
78 1
			: substr(Strings::trim((string)$response), 3);
79
	}
80
81
	public function getSignature(IMessage $message) : string
82
	{
83 1
		return hash_hmac('sha1', $message->getFrom() . $message->getTo() . $message->getText(), $this->key);
84
	}
85
86
	public function isSuccess(string $response)
87
	{
88 1
		return ($this->debug && Strings::startsWith($response, 'VALID_REQUEST')) || Strings::startsWith($response, 'ENQUEUED');
89
	}
90
91
	/**
92
	 * @param  string $id
93
	 * @param  string $key
94
	 * @throws ConfigurationException
95
	 * @return bool
96
	 */
97
	private function checkConfig($id, $key)
98
	{
99 1
		if (!Strings::match($id, '~^\d-[0-9a-zA-Z]{6}$~'))
100 1
			throw new ConfigurationException('Parameter "id" must be in format "\d-[0-9a-zA-Z]{6}".');
101 1 View Code Duplication
		if (strlen($key) !== 8 && strlen($key) !== 9)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102 1
			throw new ConfigurationException('Parameter "key" must have 8 or 9 characters. It has ' . strlen($key) . ' characters.');
103 1
		return TRUE;
104
	}
105
106
	/**
107
	 * @throws MissingParameterException
108
	 */
109
	private function check(IMessage $message) : bool
110
	{
111 1
		if (empty($message->getFrom()))
112 1
			throw new MissingParameterException('Message has empty sender. Use method setFrom().');
113 1
		if (empty($message->getTo()))
114 1
			throw new MissingParameterException('Message has empty recipent number. Use method setTo().');
115 1
		if (empty($message->getText()))
116 1
			throw new MissingParameterException('Message has empty text. Use method setText().');
117 1
		return TRUE;
118
	}
119
}
120