PHP Code Sniffer

This commit is contained in:
David Garcia 2016-07-24 12:40:50 +01:00
parent 88a3e96733
commit 09eda3df13
16 changed files with 225 additions and 182 deletions

View File

@ -3,23 +3,26 @@ namespace Mailgun\Connection\Exceptions;
class GenericHTTPError extends \Exception
{
protected $httpResponseCode;
protected $httpResponseBody;
public function __construct($message=null, $response_code=null, $response_body=null, $code=0, \Exception $previous=null) {
parent::__construct($message, $code, $previous);
$this->httpResponseCode = $response_code;
$this->httpResponseBody = $response_body;
}
public function getHttpResponseCode() {
return $this->httpResponseCode;
}
public function getHttpResponseBody() {
return $this->httpResponseBody;
}
protected $httpResponseCode;
protected $httpResponseBody;
public function __construct($message=null, $response_code=null, $response_body=null, $code=0, \Exception $previous=null)
{
parent::__construct($message, $code, $previous);
$this->httpResponseCode = $response_code;
$this->httpResponseBody = $response_body;
}
public function getHttpResponseCode()
{
return $this->httpResponseCode;
}
public function getHttpResponseBody()
{
return $this->httpResponseBody;
}
}
?>

View File

@ -1,4 +1,6 @@
<?php
namespace Mailgun\Connection\Exceptions;
class InvalidCredentials extends \Exception{}
class InvalidCredentials extends \Exception
{
}

View File

@ -1,4 +1,6 @@
<?php
namespace Mailgun\Connection\Exceptions;
class MissingEndpoint extends \Exception{}
class MissingEndpoint extends \Exception
{
}

View File

@ -1,4 +1,6 @@
<?php
namespace Mailgun\Connection\Exceptions;
class MissingRequiredParameters extends \Exception{}
class MissingRequiredParameters extends \Exception
{
}

View File

@ -1,4 +1,6 @@
<?php
namespace Mailgun\Connection\Exceptions;
class NoDomainsConfigured extends \Exception{}
class NoDomainsConfigured extends \Exception
{
}

View File

@ -21,6 +21,7 @@ class RestClient
{
/**
* Your API key
*
* @var string
*/
private $apiKey;
@ -37,20 +38,22 @@ class RestClient
/**
* The version of the API to use
*
* @var string
*/
protected $apiVersion = 'v2';
/**
* If we should use SSL or not
*
* @var bool
*/
protected $sslEnabled = true;
/**
* @param string $apiKey
* @param string $apiHost
* @param HttpClient $httpClient
* @param string $apiKey
* @param string $apiHost
* @param HttpClient $httpClient
*/
public function __construct($apiKey, $apiHost, HttpClient $httpClient = null)
{
@ -207,23 +210,23 @@ class RestClient
$httpResponseCode = (int)$responseObj->getStatusCode();
switch ($httpResponseCode) {
case 200:
$data = (string)$responseObj->getBody();
$jsonResponseData = json_decode($data, false);
$result = new \stdClass();
// return response data as json if possible, raw if not
$result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData;
$result->http_response_code = $httpResponseCode;
case 200:
$data = (string)$responseObj->getBody();
$jsonResponseData = json_decode($data, false);
$result = new \stdClass();
// return response data as json if possible, raw if not
$result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData;
$result->http_response_code = $httpResponseCode;
return $result;
case 400:
throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj));
case 401:
throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS);
case 404:
throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT . $this->getResponseExceptionMessage($responseObj));
default:
throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody());
return $result;
case 400:
throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj));
case 401:
throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS);
case 404:
throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT . $this->getResponseExceptionMessage($responseObj));
default:
throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody());
}
}

View File

@ -4,7 +4,8 @@
namespace Mailgun\Constants;
class Api {
class Api
{
const API_USER = "api";
const SDK_VERSION = "1.7";
const SDK_USER_AGENT = "mailgun-sdk-php";

View File

@ -4,7 +4,8 @@
namespace Mailgun\Constants;
class ExceptionMessages {
class ExceptionMessages
{
const EXCEPTION_INVALID_CREDENTIALS = "Your credentials are incorrect.";
const EXCEPTION_GENERIC_HTTP_ERROR = "An HTTP Error has occurred! Check your network connection and try again.";
const EXCEPTION_MISSING_REQUIRED_PARAMETERS = "The parameters passed to the API were invalid. Check your inputs!";

View File

@ -12,7 +12,8 @@ use Mailgun\Messages\Expcetions\InvalidParameterType;
*
* @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Lists/README.md
*/
class OptInHandler{
class OptInHandler
{
/**
* @param string $mailingList
@ -20,34 +21,36 @@ class OptInHandler{
* @param string $recipientAddress
* @return string
*/
public function generateHash($mailingList, $secretAppId, $recipientAddress){
$innerPayload = array('r' => $recipientAddress, 'l' => $mailingList);
$encodedInnerPayload = base64_encode(json_encode($innerPayload));
public function generateHash($mailingList, $secretAppId, $recipientAddress)
{
$innerPayload = array('r' => $recipientAddress, 'l' => $mailingList);
$encodedInnerPayload = base64_encode(json_encode($innerPayload));
$innerHash = hash_hmac("sha1", $encodedInnerPayload, $secretAppId);
$outerPayload = array('h' => $innerHash, 'p' => $encodedInnerPayload);
$innerHash = hash_hmac("sha1", $encodedInnerPayload, $secretAppId);
$outerPayload = array('h' => $innerHash, 'p' => $encodedInnerPayload);
return urlencode(base64_encode(json_encode($outerPayload)));
}
return urlencode(base64_encode(json_encode($outerPayload)));
}
/**
* @param string $secretAppId
* @param string $uniqueHash
* @return array|bool
*/
public function validateHash($secretAppId, $uniqueHash){
$decodedOuterPayload = json_decode(base64_decode(urldecode($uniqueHash)), true);
public function validateHash($secretAppId, $uniqueHash)
{
$decodedOuterPayload = json_decode(base64_decode(urldecode($uniqueHash)), true);
$decodedHash = $decodedOuterPayload['h'];
$innerPayload = $decodedOuterPayload['p'];
$decodedHash = $decodedOuterPayload['h'];
$innerPayload = $decodedOuterPayload['p'];
$decodedInnerPayload = json_decode(base64_decode($innerPayload), true);
$computedInnerHash = hash_hmac("sha1", $innerPayload, $secretAppId);
$decodedInnerPayload = json_decode(base64_decode($innerPayload), true);
$computedInnerHash = hash_hmac("sha1", $innerPayload, $secretAppId);
if($computedInnerHash == $decodedHash){
return array('recipientAddress' => $decodedInnerPayload['r'], 'mailingList' => $decodedInnerPayload['l']);
}
if($computedInnerHash == $decodedHash) {
return array('recipientAddress' => $decodedInnerPayload['r'], 'mailingList' => $decodedInnerPayload['l']);
}
return false;
}
return false;
}
}

View File

@ -16,7 +16,8 @@ use Mailgun\Messages\MessageBuilder;
*
* @link https://github.com/mailgun/mailgun-php/blob/master/README.md
*/
class Mailgun{
class Mailgun
{
/**
* @var RestClient
@ -30,8 +31,8 @@ class Mailgun{
/**
* @param string|null $apiKey
* @param HttpClient $httpClient
* @param string $apiEndpoint
* @param HttpClient $httpClient
* @param string $apiEndpoint
*/
public function __construct(
$apiKey = null,
@ -47,17 +48,18 @@ class Mailgun{
* MIME string. If sending MIME, the string must be passed in to the 3rd
* position of the function call.
*
* @param string $workingDomain
* @param array $postData
* @param array $postFiles
* @param string $workingDomain
* @param array $postData
* @param array $postFiles
* @return \stdClass
* @throws Exceptions\MissingRequiredMIMEParameters
*/
public function sendMessage($workingDomain, $postData, $postFiles = array()){
if(is_array($postFiles)){
public function sendMessage($workingDomain, $postData, $postFiles = array())
{
if(is_array($postFiles)) {
return $this->post("$workingDomain/messages", $postData, $postFiles);
}
else if(is_string($postFiles)){
else if(is_string($postFiles)) {
$tempFile = tempnam(sys_get_temp_dir(), "MG_TMP_MIME");
$fileHandle = fopen($tempFile, "w");
@ -83,10 +85,11 @@ class Mailgun{
* If this function returns FALSE, you must not process the request.
* You should reject the request with status code 403 Forbidden.
*
* @param array|null $postData
* @param array|null $postData
* @return bool
*/
public function verifyWebhookSignature($postData = NULL) {
public function verifyWebhookSignature($postData = null)
{
if($postData === null) {
$postData = $_POST;
}
@ -106,20 +109,22 @@ class Mailgun{
/**
* @param string $endpointUrl
* @param array $postData
* @param array $files
* @param array $postData
* @param array $files
* @return \stdClass
*/
public function post($endpointUrl, $postData = array(), $files = array()){
public function post($endpointUrl, $postData = array(), $files = array())
{
return $this->restClient->post($endpointUrl, $postData, $files);
}
/**
* @param string $endpointUrl
* @param array $queryString
* @param array $queryString
* @return \stdClass
*/
public function get($endpointUrl, $queryString = array()){
public function get($endpointUrl, $queryString = array())
{
return $this->restClient->get($endpointUrl, $queryString);
}
@ -127,16 +132,18 @@ class Mailgun{
* @param string $endpointUrl
* @return \stdClass
*/
public function delete($endpointUrl){
public function delete($endpointUrl)
{
return $this->restClient->delete($endpointUrl);
}
/**
* @param string $endpointUrl
* @param array $putData
* @param array $putData
* @return \stdClass
*/
public function put($endpointUrl, $putData){
public function put($endpointUrl, $putData)
{
return $this->restClient->put($endpointUrl, $putData);
}
@ -167,23 +174,26 @@ class Mailgun{
/**
* @return MessageBuilder
*/
public function MessageBuilder(){
public function MessageBuilder()
{
return new MessageBuilder();
}
/**
* @return OptInHandler
*/
public function OptInHandler(){
public function OptInHandler()
{
return new OptInHandler();
}
/**
* @param string $workingDomain
* @param bool $autoSend
* @param bool $autoSend
* @return BatchMessage
*/
public function BatchMessage($workingDomain, $autoSend = true){
public function BatchMessage($workingDomain, $autoSend = true)
{
return new BatchMessage($this->restClient, $workingDomain, $autoSend);
}
}

View File

@ -13,134 +13,140 @@ use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
*
* @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md
*/
class BatchMessage extends MessageBuilder{
class BatchMessage extends MessageBuilder
{
/**
* @var array
*/
private $batchRecipientAttributes;
private $batchRecipientAttributes;
/**
* @var boolean
*/
private $autoSend;
private $autoSend;
/**
* @var \Mailgun\Connection\RestClient
*/
private $restClient;
private $restClient;
/**
* @var string
*/
private $workingDomain;
private $workingDomain;
/**
* @var array
*/
private $messageIds = array();
private $messageIds = array();
/**
* @var string
*/
private $endpointUrl;
private $endpointUrl;
/**
* @param \Mailgun\Connection\RestClient $restClient
* @param string $workingDomain
* @param boolean $autoSend
* @param string $workingDomain
* @param boolean $autoSend
*/
public function __construct($restClient, $workingDomain, $autoSend){
$this->batchRecipientAttributes = array();
$this->autoSend = $autoSend;
$this->restClient = $restClient;
$this->workingDomain = $workingDomain;
$this->endpointUrl = $workingDomain . "/messages";
}
public function __construct($restClient, $workingDomain, $autoSend)
{
$this->batchRecipientAttributes = array();
$this->autoSend = $autoSend;
$this->restClient = $restClient;
$this->workingDomain = $workingDomain;
$this->endpointUrl = $workingDomain . "/messages";
}
/**
* @param string $headerName
* @param string $address
* @param array $variables
* @param array $variables
* @throws MissingRequiredMIMEParameters
* @throws TooManyParameters
*/
protected function addRecipient($headerName, $address, $variables){
if(array_key_exists($headerName, $this->counters['recipients'])){
if($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT){
if($this->autoSend == false){
throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS);
}
$this->sendMessage();
}
}
protected function addRecipient($headerName, $address, $variables)
{
if(array_key_exists($headerName, $this->counters['recipients'])) {
if($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT) {
if($this->autoSend == false) {
throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS);
}
$this->sendMessage();
}
}
$compiledAddress = $this->parseAddress($address, $variables);
$compiledAddress = $this->parseAddress($address, $variables);
if(isset($this->message[$headerName])){
array_push($this->message[$headerName], $compiledAddress);
}
elseif($headerName == "h:reply-to"){
$this->message[$headerName] = $compiledAddress;
}
else{
$this->message[$headerName] = array($compiledAddress);
}
if(isset($this->message[$headerName])) {
array_push($this->message[$headerName], $compiledAddress);
}
elseif($headerName == "h:reply-to") {
$this->message[$headerName] = $compiledAddress;
}
else{
$this->message[$headerName] = array($compiledAddress);
}
if(array_key_exists($headerName, $this->counters['recipients'])){
$this->counters['recipients'][$headerName] += 1;
if(!array_key_exists("id", $variables)){
$variables['id'] = $this->counters['recipients'][$headerName];
}
}
$this->batchRecipientAttributes["$address"] = $variables;
}
if(array_key_exists($headerName, $this->counters['recipients'])) {
$this->counters['recipients'][$headerName] += 1;
if(!array_key_exists("id", $variables)) {
$variables['id'] = $this->counters['recipients'][$headerName];
}
}
$this->batchRecipientAttributes["$address"] = $variables;
}
/**
* @param array $message
* @param array $files
* @throws MissingRequiredMIMEParameters
*/
public function sendMessage($message = array(), $files = array()){
if(count($message) < 1){
$message = $this->message;
$files = $this->files;
}
if(!array_key_exists("from", $message)){
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
elseif(!array_key_exists("to", $message)){
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
elseif(!array_key_exists("subject", $message)){
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))){
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
else{
$message["recipient-variables"] = json_encode($this->batchRecipientAttributes);
$response = $this->restClient->post($this->endpointUrl, $message, $files);
$this->batchRecipientAttributes = array();
$this->counters['recipients']['to'] = 0;
$this->counters['recipients']['cc'] = 0;
$this->counters['recipients']['bcc'] = 0;
unset($this->message["to"]);
array_push($this->messageIds, $response->http_response_body->id);
}
}
public function sendMessage($message = array(), $files = array())
{
if(count($message) < 1) {
$message = $this->message;
$files = $this->files;
}
if(!array_key_exists("from", $message)) {
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
elseif(!array_key_exists("to", $message)) {
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
elseif(!array_key_exists("subject", $message)) {
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))) {
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
else{
$message["recipient-variables"] = json_encode($this->batchRecipientAttributes);
$response = $this->restClient->post($this->endpointUrl, $message, $files);
$this->batchRecipientAttributes = array();
$this->counters['recipients']['to'] = 0;
$this->counters['recipients']['cc'] = 0;
$this->counters['recipients']['bcc'] = 0;
unset($this->message["to"]);
array_push($this->messageIds, $response->http_response_body->id);
}
}
/**
* @throws MissingRequiredMIMEParameters
*/
public function finalize(){
$this->sendMessage();
}
public function finalize()
{
$this->sendMessage();
}
/**
* @return string[]
*/
public function getMessageIds(){
return $this->messageIds;
}
public function getMessageIds()
{
return $this->messageIds;
}
}

View File

@ -1,4 +1,6 @@
<?php
namespace Mailgun\Messages\Exceptions;
class InvalidParameter extends \Exception{}
class InvalidParameter extends \Exception
{
}

View File

@ -1,4 +1,6 @@
<?php
namespace Mailgun\Messages\Exceptions;
class InvalidParameterType extends \Exception{}
class InvalidParameterType extends \Exception
{
}

View File

@ -1,4 +1,6 @@
<?php
namespace Mailgun\Messages\Exceptions;
class MissingRequiredMIMEParameters extends \Exception{}
class MissingRequiredMIMEParameters extends \Exception
{
}

View File

@ -1,4 +1,6 @@
<?php
namespace Mailgun\Messages\Exceptions;
class TooManyParameters extends \Exception{}
class TooManyParameters extends \Exception
{
}

View File

@ -50,9 +50,9 @@ class MessageBuilder
);
/**
* @param array $params
* @param array $params
* @param string $key
* @param mixed $default
* @param mixed $default
* @return mixed
*/
protected function safeGet($params, $key, $default)
@ -82,7 +82,7 @@ class MessageBuilder
/**
* @param string $address
* @param array $variables
* @param array $variables
* @return string
*/
protected function parseAddress($address, $variables)
@ -101,7 +101,7 @@ class MessageBuilder
/**
* @param string $headerName
* @param string $address
* @param array $variables
* @param array $variables
*/
protected function addRecipient($headerName, $address, $variables)
{
@ -120,7 +120,7 @@ class MessageBuilder
}
/**
* @param string $address
* @param string $address
* @param array|null $variables
* @return mixed
* @throws TooManyParameters
@ -136,7 +136,7 @@ class MessageBuilder
}
/**
* @param string $address
* @param string $address
* @param array|null $variables
* @return mixed
* @throws TooManyParameters
@ -152,7 +152,7 @@ class MessageBuilder
}
/**
* @param string $address
* @param string $address
* @param array|null $variables
* @return mixed
* @throws TooManyParameters
@ -168,7 +168,7 @@ class MessageBuilder
}
/**
* @param string $address
* @param string $address
* @param array|null $variables
* @return mixed
*/
@ -180,7 +180,7 @@ class MessageBuilder
}
/**
* @param string $address
* @param string $address
* @param array|null $variables
* @return mixed
*/
@ -207,7 +207,7 @@ class MessageBuilder
/**
* @param string $headerName
* @param mixed $headerData
* @param mixed $headerData
* @return mixed
*/
public function addCustomHeader($headerName, $headerData)
@ -249,7 +249,7 @@ class MessageBuilder
}
/**
* @param string $attachmentPath
* @param string $attachmentPath
* @param string|null $attachmentName
* @return bool
*/
@ -274,7 +274,7 @@ class MessageBuilder
}
/**
* @param string $inlineImagePath
* @param string $inlineImagePath
* @param string|null $inlineImageName
*
* @return bool|true
@ -412,7 +412,7 @@ class MessageBuilder
}
/**
* @param string $timeDate
* @param string $timeDate
* @param string|null $timeZone
* @return string
*/
@ -433,7 +433,7 @@ class MessageBuilder
/**
* @param string $customName
* @param mixed $data
* @param mixed $data
*/
public function addCustomData($customName, $data)
{
@ -442,7 +442,7 @@ class MessageBuilder
/**
* @param string $parameterName
* @param mixed $data
* @param mixed $data
* @return mixed
*/
public function addCustomParameter($parameterName, $data)