Browse Source

Initial commit with InAppEvent endpoint.

master
José Lorente 6 years ago
commit
0922c4dea8
  1. 13
      LICENSE
  2. 28
      composer.json
  3. 44
      src/AmountConverter.php
  4. 52
      src/Api/InAppEvent.php
  5. 250
      src/Appsflyer.php
  6. 133
      src/Config.php
  7. 69
      src/ConfigInterface.php
  8. 203
      src/Core/Api.php
  9. 119
      src/Core/ApiInterface.php
  10. 33
      src/Exception/ApiLimitExceededException.php
  11. 145
      src/Exception/AppsflyerException.php
  12. 25
      src/Exception/BadRequestException.php
  13. 125
      src/Exception/Handler.php
  14. 25
      src/Exception/InvalidRequestException.php
  15. 25
      src/Exception/MissingParameterException.php
  16. 25
      src/Exception/NotFoundException.php
  17. 25
      src/Exception/ServerErrorException.php
  18. 25
      src/Exception/UnauthorizedException.php
  19. 106
      src/Pager.php
  20. 65
      src/Utility.php

13
LICENSE

@ -0,0 +1,13 @@
The BSD 3-Clause License
Copyright (c) 2011-2018, Cartalyst LLC
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of Cartalyst LLC and its libraries nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
composer.json

@ -0,0 +1,28 @@
{
"name": "jlorente/appsflyer",
"description": "An Appsflyer API package.",
"keywords": [
"php",
"jlorente",
"appsflyer"
],
"license": "BSD-3-Clause",
"authors": [
{
"name": "José Lorente",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.6",
"guzzlehttp/guzzle": "~6.0"
},
"require-dev": {
"mockery/mockery": "~0.9",
"phpunit/phpunit": "~4.8"
},
"autoload": {
"psr-4": { "Jlorente\\Appsflyer\\": "src/" }
},
"minimum-stability": "stable"
}

44
src/AmountConverter.php

@ -0,0 +1,44 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer;
class AmountConverter
{
/**
* Converts the given number into cents.
*
* @param mixed $number
* @return string
*/
public static function convert($number)
{
$number = preg_replace('/\,/i', '', $number);
$number = preg_replace('/([^0-9\.\-])/i', '', $number);
if (! is_numeric($number)) {
return '0.00';
}
$isCents = (bool) preg_match('/^0.\d+$/', $number);
return ($isCents ? '0' : null).number_format($number * 100., 0, '.', '');
}
}

52
src/Api/InAppEvent.php

@ -0,0 +1,52 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Api;
use Jlorente\Appsflyer\Core\Api;
/**
* Class InAppEvent.
*
* @author Jose Lorente <jose.lorente.martin@gmail.com>
*/
class InAppEvent extends Api
{
/**
* {@inheritdoc}
*/
public function baseUrl()
{
return 'https://api2.appsflyer.com';
}
/**
* Creates a new in-app event.
*
* @param string $appId
* @param array $parameters
* @return array
*/
public function create($appId, array $parameters = [])
{
return $this->_post("inappevent/$appId", $parameters);
}
}

250
src/Appsflyer.php

@ -0,0 +1,250 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer;
use ReflectionClass;
class Appsflyer
{
/**
* The package version.
*
* @var string
*/
const VERSION = '1.0.0';
/**
* The Config repository instance.
*
* @var \Jlorente\Appsflyer\ConfigInterface
*/
protected $config;
/**
* The amount converter class and method name.
*
* @var string
*/
protected static $amountConverter = '\\Jlorente\\Appsflyer\\AmountConverter::convert';
/**
* Constructor.
*
* @param string $devKey
* @param string $apiToken
* @return void
*/
public function __construct($devKey = null, $apiToken = null)
{
$this->config = new Config(self::VERSION, $devKey, $apiToken);
}
/**
* Create a new Appsflyer API instance.
*
* @param string $devKey
* @param string $apiToken
* @return \Jlorente\Appsflyer\Appsflyer
*/
public static function make($devKey = null, $apiToken = null)
{
return new static($devKey, $apiToken);
}
/**
* Returns the current package version.
*
* @return string
*/
public static function getVersion()
{
return self::VERSION;
}
/**
* Returns the Config repository instance.
*
* @return \Jlorente\Appsflyer\ConfigInterface
*/
public function getConfig()
{
return $this->config;
}
/**
* Sets the Config repository instance.
*
* @param \Jlorente\Appsflyer\ConfigInterface $config
* @return $this
*/
public function setConfig(ConfigInterface $config)
{
$this->config = $config;
return $this;
}
/**
* Returns the Appsflyer API key.
*
* @return string
*/
public function getApiKey()
{
return $this->config->getApiKey();
}
/**
* Sets the Appsflyer API key.
*
* @param string $devKey
* @return $this
*/
public function setApiKey($devKey)
{
$this->config->setApiKey($devKey);
return $this;
}
/**
* Returns the Appsflyer API version.
*
* @return string
*/
public function getApiToken()
{
return $this->config->getApiToken();
}
/**
* Sets the Appsflyer API version.
*
* @param string $apiToken
* @return $this
*/
public function setApiToken($apiToken)
{
$this->config->setApiToken($apiToken);
return $this;
}
/**
* Returns the amount converter class and method name.
*
* @return string
*/
public static function getAmountConverter()
{
return static::$amountConverter;
}
/**
* Sets the amount converter class and method name.
*
* @param $amountConverter string
* @return void
*/
public static function setAmountConverter($amountConverter)
{
static::$amountConverter = $amountConverter;
}
/**
* Disables the amount converter.
*
* @return void
*/
public static function disableAmountConverter()
{
static::setAmountConverter(null);
}
/**
* Returns the default amount converter.
*
* @return string
*/
public static function getDefaultAmountConverter()
{
return '\\Jlorente\\Appsflyer\\AmountConverter::convert';
}
/**
* Sets the default amount converter;
*
* @return void
*/
public static function setDefaultAmountConverter()
{
static::setAmountConverter(
static::getDefaultAmountConverter()
);
}
/**
* Dynamically handle missing methods.
*
* @param string $method
* @param array $parameters
* @return \Jlorente\Appsflyer\Api\ApiInterface
*/
public function __call($method, array $parameters)
{
if ($this->isIteratorRequest($method)) {
$apiInstance = $this->getApiInstance(substr($method, 0, -11));
return (new Pager($apiInstance))->fetch($parameters);
}
return $this->getApiInstance($method);
}
/**
* Determines if the request is an iterator request.
*
* @return bool
*/
protected function isIteratorRequest($method)
{
return substr($method, -8) === 'Iterator';
}
/**
* Returns the Api class instance for the given method.
*
* @param string $method
* @return \Jlorente\Appsflyer\Api\ApiInterface
* @throws \BadMethodCallException
*/
protected function getApiInstance($method)
{
$class = "\\Jlorente\\Appsflyer\\Api\\" . ucwords($method);
if (class_exists($class) && !(new ReflectionClass($class))->isAbstract()) {
return new $class($this->config);
}
throw new \BadMethodCallException("Undefined method [{$method}] called.");
}
}

133
src/Config.php

@ -0,0 +1,133 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer;
class Config implements ConfigInterface
{
/**
* The current package version.
*
* @var string
*/
protected $version;
/**
* The Appsflyer API key.
*
* @var string
*/
protected $devKey;
/**
* The Appsflyer API token.
*
* @var string
*/
protected $apiToken;
/**
* The managed account id.
*
* @var string
*/
protected $accountId;
/**
* Constructor.
*
* @param string $version
* @param string $devKey
* @param string $apiToken
* @return void
* @throws \RuntimeException
*/
public function __construct($version, $devKey, $apiToken)
{
$this->setVersion($version);
$this->setDevKey($devKey ?: getenv('APPSFLYER_DEV_KEY'));
$this->setApiToken($apiToken ?: getenv('APPSFLYER_API_TOKEN'));
if (!$this->devKey) {
throw new \RuntimeException('The Appsflyer dev_key is not defined!');
}
if (!$this->devKey) {
throw new \RuntimeException('The Appsflyer dev_key is not defined!');
}
}
/**
* {@inheritdoc}
*/
public function getVersion()
{
return $this->version;
}
/**
* {@inheritdoc}
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* {@inheritdoc}
*/
public function getDevKey()
{
return $this->devKey;
}
/**
* {@inheritdoc}
*/
public function setDevKey($devKey)
{
$this->devKey = $devKey;
return $this;
}
/**
* {@inheritdoc}
*/
public function getApiToken()
{
return $this->apiVersion;
}
/**
* {@inheritdoc}
*/
public function setApiToken($apiToken)
{
$this->apiVersion = $apiToken;
return $this;
}
}

69
src/ConfigInterface.php

@ -0,0 +1,69 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer;
interface ConfigInterface
{
/**
* Returns the current package version.
*
* @return string
*/
public function getVersion();
/**
* Sets the current package version.
*
* @param string $version
* @return $this
*/
public function setVersion($version);
/**
* Returns the Appsflyer API key.
*
* @return string
*/
public function getApiKey();
/**
* Sets the Appsflyer API key.
*
* @param string $apiKey
* @return $this
*/
public function setApiKey($apiKey);
/**
* Returns the Appsflyer API version.
*
* @return string
*/
public function getApiToken();
/**
* Sets the Appsflyer API version.
*
* @param string $apiVersion
* @return $this
*/
public function setApiToken($apiVersion);
}

203
src/Core/Api.php

@ -0,0 +1,203 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Core;
use GuzzleHttp\Client;
use GuzzleHttp\Middleware;
use GuzzleHttp\HandlerStack;
use Jlorente\Appsflyer\Utility;
use Jlorente\Appsflyer\ConfigInterface;
use Psr\Http\Message\RequestInterface;
use Jlorente\Appsflyer\Exception\Handler;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\TransferException;
abstract class Api implements ApiInterface
{
/**
* The Config repository instance.
*
* @var \Jlorente\Appsflyer\ConfigInterface
*/
protected $config;
/**
* Number of items to return per page.
*
* @var null|int
*/
protected $perPage;
/**
* Constructor.
*
* @param \Jlorente\Appsflyer\ConfigInterface $client
* @return void
*/
public function __construct(ConfigInterface $config)
{
$this->config = $config;
}
/**
* {@inheritdoc}
*/
public function baseUrl()
{
return 'https://hq.appsflyer.com';
}
/**
* {@inheritdoc}
*/
public function getPerPage()
{
return $this->perPage;
}
/**
* {@inheritdoc}
*/
public function setPerPage($perPage)
{
$this->perPage = (int) $perPage;
return $this;
}
/**
* {@inheritdoc}
*/
public function _get($url = null, $parameters = [])
{
if ($perPage = $this->getPerPage()) {
$parameters['limit'] = $perPage;
}
return $this->execute('get', $url, $parameters);
}
/**
* {@inheritdoc}
*/
public function _head($url = null, array $parameters = [])
{
return $this->execute('head', $url, $parameters);
}
/**
* {@inheritdoc}
*/
public function _delete($url = null, array $parameters = [])
{
return $this->execute('delete', $url, $parameters);
}
/**
* {@inheritdoc}
*/
public function _put($url = null, array $parameters = [])
{
return $this->execute('put', $url, $parameters);
}
/**
* {@inheritdoc}
*/
public function _patch($url = null, array $parameters = [])
{
return $this->execute('patch', $url, $parameters);
}
/**
* {@inheritdoc}
*/
public function _post($url = null, array $parameters = [])
{
return $this->execute('post', $url, $parameters);
}
/**
* {@inheritdoc}
*/
public function _options($url = null, array $parameters = [])
{
return $this->execute('options', $url, $parameters);
}
/**
* {@inheritdoc}
*/
public function execute($httpMethod, $url, array $parameters = [])
{
try {
$parameters = Utility::prepareParameters($parameters);
$response = $this->getClient()->{$httpMethod}('/' . $url, ['query' => $parameters]);
return json_decode((string) $response->getBody(), true);
} catch (ClientException $e) {
new Handler($e);
}
}
/**
* Returns an Http client instance.
*
* @return \GuzzleHttp\Client
*/
protected function getClient()
{
return new Client([
'base_uri' => $this->baseUrl(), 'handler' => $this->createHandler()
]);
}
/**
* Create the client handler.
*
* @return \GuzzleHttp\HandlerStack
*/
protected function createHandler()
{
$stack = HandlerStack::create();
$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
$config = $this->config;
$request = $request->withHeader('authentication', base64_encode($config->getApiKey()));
return $request;
}));
$stack->push(Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null, TransferException $exception = null) {
return $retries < 3 && ($exception instanceof ConnectException || ($response && $response->getStatusCode() >= 500));
}, function ($retries) {
return (int) pow(2, $retries) * 1000;
}));
return $stack;
}
}

119
src/Core/ApiInterface.php

@ -0,0 +1,119 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Core;
interface ApiInterface
{
/**
* Returns the API base url.
*
* @return string
*/
public function baseUrl();
/**
* Returns the number of items to return per page.
*
* @return void
*/
public function getPerPage();
/**
* Sets the number of items to return per page.
*
* @param int $perPage
* @return $this
*/
public function setPerPage($perPage);
/**
* Send a GET request.
*
* @param string $url
* @param array $parameters
* @return array
*/
public function _get($url = null, $parameters = []);
/**
* Send a HEAD request.
*
* @param string $url
* @param array $parameters
* @return array
*/
public function _head($url = null, array $parameters = []);
/**
* Send a DELETE request.
*
* @param string $url
* @param array $parameters
* @return array
*/
public function _delete($url = null, array $parameters = []);
/**
* Send a PUT request.
*
* @param string $url
* @param array $parameters
* @return array
*/
public function _put($url = null, array $parameters = []);
/**
* Send a PATCH request.
*
* @param string $url
* @param array $parameters
* @return array
*/
public function _patch($url = null, array $parameters = []);
/**
* Send a POST request.
*
* @param string $url
* @param array $parameters
* @return array
*/
public function _post($url = null, array $parameters = []);
/**
* Send an OPTIONS request.
*
* @param string $url
* @param array $parameters
* @return array
*/
public function _options($url = null, array $parameters = []);
/**
* Executes the HTTP request.
*
* @param string $httpMethod
* @param string $url
* @param array $parameters
* @return array
*/
public function execute($httpMethod, $url, array $parameters = []);
}

33
src/Exception/ApiLimitExceededException.php

@ -0,0 +1,33 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Exception;
class ApiLimitExceededException extends AppsflyerException
{
/**
* {@inheritdoc}
*/
public function __construct()
{
parent::__construct('You have reached the Appsflyer Api rate limit!');
}
}

145
src/Exception/AppsflyerException.php

@ -0,0 +1,145 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Exception;
class AppsflyerException extends \Exception
{
/**
* The error code returned by Appsflyer.
*
* @var string
*/
protected $errorCode;
/**
* The error type returned by Appsflyer.
*
* @var string
*/
protected $errorType;
/**
* The missing parameter returned by Appsflyer with the error.
*
* @var string
*/
protected $missingParameter;
/**
* The raw output returned by Appsflyer in case of exception.
*
* @var string
*/
protected $rawOutput;
/**
* Returns the error type returned by Appsflyer.
*
* @return string
*/
public function getErrorCode()
{
return $this->errorCode;
}
/**
* Sets the error type returned by Appsflyer.
*
* @param string $errorCode
* @return $this
*/
public function setErrorCode($errorCode)
{
$this->errorCode = $errorCode;
return $this;
}
/**
* Returns the error type returned by Appsflyer.
*
* @return string
*/
public function getErrorType()
{
return $this->errorType;
}
/**
* Sets the error type returned by Appsflyer.
*
* @param string $errorType
* @return $this
*/
public function setErrorType($errorType)
{
$this->errorType = $errorType;
return $this;
}
/**
* Returns missing parameter returned by Appsflyer with the error.
*
* @return string
*/
public function getMissingParameter()
{
return $this->missingParameter;
}
/**
* Sets the missing parameter returned by Appsflyer with the error.
*
* @param string $missingParameter
* @return $this
*/
public function setMissingParameter($missingParameter)
{
$this->missingParameter = $missingParameter;
return $this;
}
/**
* Returns raw output returned by Appsflyer in case of exception.
*
* @return string
*/
public function getRawOutput()
{
return $this->rawOutput;
}
/**
* Sets the raw output parameter returned by Appsflyer in case of exception.
*
* @param string $rawOutput
* @return $this
*/
public function setRawOutput($rawOutput)
{
$this->rawOutput = $rawOutput;
return $this;
}
}

25
src/Exception/BadRequestException.php

@ -0,0 +1,25 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Exception;
class BadRequestException extends AppsflyerException
{
}

125
src/Exception/Handler.php

@ -0,0 +1,125 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Exception;
use GuzzleHttp\Exception\ClientException;
class Handler
{
/**
* List of mapped exceptions and their corresponding error types.
*
* @var array
*/
protected $exceptionsByErrorType = [
// Card errors are the most common type of error you should expect to handle
'card_error' => 'CardError',
];
/**
* List of mapped exceptions and their corresponding status codes.
*
* @var array
*/
protected $exceptionsByStatusCode = [
// Often missing a required parameter
400 => 'BadRequest',
// Invalid Appsflyer API key provided
401 => 'Unauthorized',
// Parameters were valid but request failed
402 => 'InvalidRequest',
// The requested item doesn't exist
404 => 'NotFound',
// Something went wrong on Appsflyer's end
500 => 'ServerError',
502 => 'ServerError',
503 => 'ServerError',
504 => 'ServerError',
];
/**
* Constructor.
*
* @param \GuzzleHttp\Exception\ClientException $exception
* @return void
* @throws \Jlorente\Appsflyer\Exception\AppsflyerException
*/
public function __construct(ClientException $exception)
{
$response = $exception->getResponse();
$statusCode = $response->getStatusCode();
$rawOutput = json_decode($response->getBody(true), true);
$error = isset($rawOutput['error']) ? $rawOutput['error'] : [];
$errorCode = isset($error['code']) ? $error['code'] : null;
$errorType = isset($error['type']) ? $error['type'] : null;
$message = isset($error['message']) ? $error['message'] : null;
$missingParameter = isset($error['param']) ? $error['param'] : null;
$this->handleException(
$message, $statusCode, $errorType, $errorCode, $missingParameter, $rawOutput
);
}
/**
* Guesses the FQN of the exception to be thrown.
*
* @param string $message
* @param int $statusCode
* @param string $errorType
* @param string $errorCode
* @param string $missingParameter
* @return void
* @throws \Jlorente\Appsflyer\Exception\AppsflyerException
*/
protected function handleException($message, $statusCode, $errorType, $errorCode, $missingParameter, $rawOutput)
{
if ($statusCode === 400 && $errorCode === 'rate_limit') {
$class = 'ApiLimitExceeded';
} elseif ($statusCode === 400 && $errorType === 'invalid_request_error') {
$class = 'MissingParameter';
} elseif (array_key_exists($errorType, $this->exceptionsByErrorType)) {
$class = $this->exceptionsByErrorType[$errorType];
} elseif (array_key_exists($statusCode, $this->exceptionsByStatusCode)) {
$class = $this->exceptionsByStatusCode[$statusCode];
} else {
$class = 'Appsflyer';
}
$class = "\\Jlorente\\Appsflyer\\Exception\\{$class}Exception";
$instance = new $class($message, $statusCode);
$instance->setErrorCode($errorCode);
$instance->setErrorType($errorType);
$instance->setMissingParameter($missingParameter);
$instance->setRawOutput($rawOutput);
throw $instance;
}
}

25
src/Exception/InvalidRequestException.php

@ -0,0 +1,25 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Exception;
class InvalidRequestException extends AppsflyerException
{
}

25
src/Exception/MissingParameterException.php

@ -0,0 +1,25 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Exception;
class MissingParameterException extends AppsflyerException
{
}

25
src/Exception/NotFoundException.php

@ -0,0 +1,25 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Exception;
class NotFoundException extends AppsflyerException
{
}

25
src/Exception/ServerErrorException.php

@ -0,0 +1,25 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Exception;
class ServerErrorException extends AppsflyerException
{
}

25
src/Exception/UnauthorizedException.php

@ -0,0 +1,25 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer\Exception;
class UnauthorizedException extends AppsflyerException
{
}

106
src/Pager.php

@ -0,0 +1,106 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer;
use Jlorente\Appsflyer\Api\ApiInterface;
class Pager
{
/**
* The Api instance.
*
* @var \Jlorente\Appsflyer\Api\ApiInterface $api
*/
protected $api;
/**
* The next request token.
*
* @var array
*/
protected $nextToken;
/**
* Constructor.
*
* @param \Jlorente\Appsflyer\Api\ApiInterface $api
* @return void
*/
public function __construct(ApiInterface $api)
{
$this->api = $api;
}
/**
* Fetches all the objects of the given api.
*
* @param array $parameters
* @return array
*/
public function fetch(array $parameters = [])
{
$this->api->setPerPage(100);
$results = $this->processRequest($parameters);
while ($this->nextToken) {
$results = array_merge($results, $this->processRequest($parameters));
}
return $results;
}
/**
* Processes the api request.
*
* @param array $parameters
* @return array
*/
protected function processRequest(array $parameters = [])
{
if ($this->nextToken) {
$parameters['starting_after'] = $this->nextToken;
}
if (isset($parameters[0])) {
$id = $parameters[0];
unset($parameters[0]);
if (isset($parameters[1])) {
$parameters = $parameters[1];
unset($parameters[1]);
}
$parameters = [$id, $parameters];
} else {
$parameters = [$parameters];
}
$result = call_user_func_array([$this->api, 'all'], $parameters);
$this->nextToken = $result['has_more'] ? end($result['data'])['id'] : false;
return $result['data'];
}
}

65
src/Utility.php

@ -0,0 +1,65 @@
<?php
/**
* Part of the Appsflyer package.
*
* NOTICE OF LICENSE
*
* Licensed under the 3-clause BSD License.
*
* This source file is subject to the 3-clause BSD License that is
* bundled with this package in the LICENSE file.
*
* @package Appsflyer
* @version 1.0.0
* @author Jose Lorente
* @license BSD License (3-clause)
* @copyright (c) 2018, Jose Lorente
*/
namespace Jlorente\Appsflyer;
class Utility
{
/**
* Prepares the given parameters.
*
* @param array $parameters
* @return array
*/
public static function prepareParameters(array $parameters)
{
$toConvert = [ 'amount', 'price' ];
if (self::needsAmountConversion($parameters)) {
if ($converter = Stripe::getAmountConverter()) {
foreach ($toConvert as $to) {
if (isset($parameters[$to])) {
$parameters[$to] = forward_static_call_array(
$converter, [ $parameters[$to] ]
);
}
}
}
}
$parameters = array_map(function ($parameter) {
return is_bool($parameter) ? ($parameter === true ? 'true' : 'false') : $parameter;
}, $parameters);
return preg_replace('/\%5B\d+\%5D/', '%5B%5D', http_build_query($parameters));;
}
protected static function needsAmountConversion(array $parameters)
{
$hasCurrency = isset($parameters['currency']);
$currencies = [
'BIF', 'DJF', 'JPY', 'KRW', 'PYG',
'VND', 'XAF', 'XPF', 'CLP', 'GNF',
'KMF', 'MGA', 'RWF', 'VUV', 'XOF',
];
return ! $hasCurrency || ($hasCurrency && ! in_array($parameters['currency'], $currencies));
}
}
Loading…
Cancel
Save