Skip to content

Commit

Permalink
Merge pull request #1 from spryker-eco/feature/eco-1924/dev-sevensenders
Browse files Browse the repository at this point in the history
Release 1.0.0
  • Loading branch information
alex-galych authored Oct 18, 2018
2 parents 1170833 + 130634d commit 1f8a791
Show file tree
Hide file tree
Showing 46 changed files with 1,849 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*.sublime-*
*.AppleDouble
*.AppleDB
*.AppleDesktop
*.AppleDesktop
2 changes: 1 addition & 1 deletion .license
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* MIT License
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/
15 changes: 15 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
build:
environment:
php:
version: 7.1

checks:
php:
code_rating: true

filter:
excluded_paths:
- config/*
- tests/*
- src/Generated/*
- src/Pyz/*
47 changes: 47 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
language: php

notifications:
email: false

sudo: required

matrix:
fast_finish: true
include:
- php: 7.1

services:
- postgresql
- redis
- rabbitmq

addons:
postgresql: 9.4

apt:
packages:
- graphviz

hosts:
- zed.de.spryker.test
- www.de.spryker.test

env:
global:
- APPLICATION_ENV=devtest
- APPLICATION_STORE=DE
- MODULE_DIR=module
- SHOP_DIR=current
- MODULE_NAME=sevensenders

cache:
directories:
- $SHOP_DIR/current/vendor
- $HOME/.composer/cache

before_install:
- echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini

script:
- git clone -b 0.1.1 https://github.com/spryker-eco/eco-ci.git ecoci
- ./ecoci/build/travis.sh
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 SPRYKER SYSTEMS GMBH
Copyright (c) 2016-present Spryker Systems GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
24 changes: 24 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace: Sevensenders

include:
- tests/SprykerEcoTest/Zed/Sevensenders

paths:
tests: tests
support: .
log: tests/_output
data: tests/_data
envs: tests/_envs

settings:
bootstrap: _bootstrap.php
suite_class: \PHPUnit_Framework_TestSuite
colors: true
memory_limit: 1024M
log: true

coverage:
enabled: true
whitelist:
include:
- 'src/*'
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "spryker-eco/sevensenders",
"description": "Sevensenders Module",
"require": {
"php": ">=7.1",
"spryker/kernel": "^3.0.0",
"spryker/guzzle": "^2.2.0",
"spryker/oms": "^8.0.0",
"spryker/customer": "^7.10.0",
"spryker/mail": "^4.0.0"
},
"require-dev": {
"spryker/code-sniffer": "*",
"spryker/monolog": "*",
"spryker/queue": "*",
"spryker/testify": "*"
},
"autoload": {
"psr-4": {
"SprykerEco\\": "src/SprykerEco/"
}
},
"minimum-stability": "dev",
"autoload-dev": {
"psr-4": {
"SprykerTest\\": "tests/SprykerEcoTest/"
}
}
}
3 changes: 3 additions & 0 deletions phpstan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"defaultLevel": 5
}
17 changes: 17 additions & 0 deletions src/SprykerEco/Shared/Sevensenders/SevensendersConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace SprykerEco\Shared\Sevensenders;

/**
* Declares global environment configuration keys. Do not use it for other class constants.
*/
interface SevensendersConstants
{
public const API_URL = 'SEVENSENDERS:API_URL';
public const API_KEY = 'SEVENSENDERS:API_KEY';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<transfers xmlns="http://xsd.spryker.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://static.spryker.com http://static.spryker.com/transfer-01.xsd">

<transfer name="SevensendersRequest">
<property name="payload" type="array"/>
</transfer>

<transfer name="SevensendersResponse">
<property name="requestPayload" type="string"/>
<property name="responsePayload" type="string"/>
<property name="status" type="int"/>
<property name="status" type="int"/>
</transfer>
</transfers>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace SprykerEco\Zed\Sevensenders\Business\Api\Adapter;

use Generated\Shared\Transfer\SevensendersRequestTransfer;
use Generated\Shared\Transfer\SevensendersResponseTransfer;

interface AdapterInterface
{
/**
* @param \Generated\Shared\Transfer\SevensendersRequestTransfer $transfer
* @param string $resource
*
* @return \Generated\Shared\Transfer\SevensendersResponseTransfer
*/
public function send(SevensendersRequestTransfer $transfer, string $resource): SevensendersResponseTransfer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace SprykerEco\Zed\Sevensenders\Business\Api\Adapter;

use Generated\Shared\Transfer\SevensendersRequestTransfer;
use Generated\Shared\Transfer\SevensendersResponseTransfer;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\RequestOptions;
use SprykerEco\Zed\Sevensenders\Business\Exception\SevensendersApiBadCredentialsException;
use SprykerEco\Zed\Sevensenders\Business\Exception\SevensendersApiHttpRequestException;
use SprykerEco\Zed\Sevensenders\Dependency\Service\SevensendersToUtilEncodingServiceInterface;
use SprykerEco\Zed\Sevensenders\SevensendersConfig;

class SevensendersApiAdapter implements AdapterInterface
{
protected const DEFAULT_TIMEOUT = 45;

public const ORDER_RESOURCE = 'orders';
public const SHIPMENT_RESOURCE = 'shipments';
public const AUTH_RESOURCE = 'token';

protected const RESPONSE_KEY_TOKEN = 'token';

protected const REQUEST_KEY_ACCESS_KEY = 'access_key';

protected const DEFAULT_HEADERS = [
'Content-Type' => 'application/json',
];

/**
* @var \GuzzleHttp\Client
*/
protected $client;

/**
* @var \SprykerEco\Zed\Sevensenders\SevensendersConfig
*/
protected $config;

/**
* @var \SprykerEco\Zed\Sevensenders\Dependency\Service\SevensendersToUtilEncodingServiceInterface
*/
protected $utilEncodingService;

/**
* @param \SprykerEco\Zed\Sevensenders\SevensendersConfig $config
* @param \SprykerEco\Zed\Sevensenders\Dependency\Service\SevensendersToUtilEncodingServiceInterface $utilEncodingService
*/
public function __construct(SevensendersConfig $config, SevensendersToUtilEncodingServiceInterface $utilEncodingService)
{
$this->config = $config;
$this->utilEncodingService = $utilEncodingService;
$this->client = new Client([
RequestOptions::TIMEOUT => static::DEFAULT_TIMEOUT,
]);
}

/**
* @param \Generated\Shared\Transfer\SevensendersRequestTransfer $transfer
* @param string $resource
*
* @throws \SprykerEco\Zed\Sevensenders\Business\Exception\SevensendersApiHttpRequestException
*
* @return \Generated\Shared\Transfer\SevensendersResponseTransfer
*/
public function send(SevensendersRequestTransfer $transfer, string $resource): SevensendersResponseTransfer
{
try {
$options = $this->prepareOptions($transfer);
$response = $this->client->post(
$this->getUrl($resource),
$options
);
} catch (RequestException $requestException) {
throw new SevensendersApiHttpRequestException(
$requestException->getMessage(),
$requestException->getCode(),
$requestException
);
}

$responseTransfer = new SevensendersResponseTransfer();
$responseTransfer->setStatus($response->getStatusCode());
$responseTransfer->setRequestPayload($options[RequestOptions::BODY]);
$responseTransfer->setResponsePayload($response->getBody()->getContents());

return $responseTransfer;
}

/**
* @throws \SprykerEco\Zed\Sevensenders\Business\Exception\SevensendersApiBadCredentialsException
*
* @return string
*/
protected function auth(): string
{
$response = $this->utilEncodingService->decodeJson($this->client->post($this->getUrl(static::AUTH_RESOURCE), [
RequestOptions::BODY => $this->utilEncodingService->encodeJson([
static::REQUEST_KEY_ACCESS_KEY => $this->config->getApiKey(),
]),
])->getBody()->getContents());

if (!array_key_exists(static::RESPONSE_KEY_TOKEN, $response)) {
throw new SevensendersApiBadCredentialsException('Bad credentials', 401);
}

return 'Bearer ' . $response[static::RESPONSE_KEY_TOKEN];
}

/**
* @param string $resource
*
* @return string
*/
protected function getUrl(string $resource): string
{
return $this->config->getApiUrl() . $resource;
}

/**i
*
* @param \Generated\Shared\Transfer\SevensendersRequestTransfer $transfer
*
* @return array
*/
protected function prepareOptions(SevensendersRequestTransfer $transfer): array
{
$options[RequestOptions::BODY] = $this->utilEncodingService->encodeJson($transfer->getPayload());
$options[RequestOptions::HEADERS] = $this->prepareHeaders();

return $options;
}

/**
* @return array
*/
protected function prepareHeaders(): array
{
$auth = [
'Authorization' => $this->auth(),
];

return array_merge(static::DEFAULT_HEADERS, $auth);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace SprykerEco\Zed\Sevensenders\Business\Exception;

use Exception;

class SevensendersApiBadCredentialsException extends Exception
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* MIT License
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

namespace SprykerEco\Zed\Sevensenders\Business\Exception;

use Exception;

class SevensendersApiHttpRequestException extends Exception
{
}
Loading

0 comments on commit 1f8a791

Please sign in to comment.