Skip to content

Commit 04f7964

Browse files
committed
init common
0 parents  commit 04f7964

23 files changed

Lines changed: 1267 additions & 0 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.idea
2+
*.phar
3+
vendor

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Dmitriy Larionov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Whitespace-only changes.

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "dumkaaa/boxberry-api-common",
3+
"type": "library",
4+
"description": "Базовые компоненты для реализации Boxberry API",
5+
"keywords": [
6+
"boxberry",
7+
"api",
8+
"delivery"
9+
],
10+
"homepage": "https://github.com/Dumkaaa/boxberry-api-common",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Dmitriy Larionov",
15+
"email": "dumkaaa72@gmail.com"
16+
},
17+
{
18+
"name": "Github Contributors",
19+
"homepage": "https://github.com/Dumkaaa/boxberry-api-common/contributors"
20+
}
21+
],
22+
"require": {
23+
"php": ">=5.5.9",
24+
"php-http/client-implementation": "^1",
25+
"php-http/message": "^1.5",
26+
"php-http/discovery": "^1.2.1",
27+
"symfony/http-foundation": "^2.1|^3|^4",
28+
"symfony/validator": "~3"
29+
},
30+
"require-dev": {
31+
"php-http/guzzle6-adapter": "^1.1"
32+
},
33+
"autoload": {
34+
"psr-4": {
35+
"Dumkaaa\\Boxberry\\Api\\Common\\": "src"
36+
}
37+
}
38+
}

src/AbstractDelivery.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
namespace Dumkaaa\Boxberry\Api\Common;
4+
5+
use Dumkaaa\Boxberry\Api\Common\Http\Client;
6+
use Dumkaaa\Boxberry\Api\Common\Message\Request\AbstractRequest;
7+
use Dumkaaa\Boxberry\Api\Common\Message\Response\AbstractResponse;
8+
use Symfony\Component\HttpFoundation\ParameterBag;
9+
use Symfony\Component\HttpFoundation\Request as HttpRequest;
10+
11+
/**
12+
* Базовый класс достаки
13+
*
14+
* @package Dumkaaa\Boxberry\Api\Common
15+
*/
16+
abstract class AbstractDelivery implements DeliveryInterface
17+
{
18+
/**
19+
* Параметры достаки
20+
*
21+
* @var \Symfony\Component\HttpFoundation\ParameterBag
22+
*/
23+
protected $parameters;
24+
25+
/**
26+
* PSR-7 Http клиент
27+
*
28+
* @var Client
29+
*/
30+
protected $httpClient;
31+
32+
/**
33+
* Объект запроса
34+
*
35+
* @var HttpRequest
36+
*/
37+
protected $httpRequest;
38+
39+
/**
40+
* Создает объект доставки
41+
*
42+
* @param Client|null $httpClient
43+
* @param HttpRequest|null $httpRequest
44+
*/
45+
public function __construct(Client $httpClient = null, HttpRequest $httpRequest = null)
46+
{
47+
$this->httpClient = $httpClient ?: $this->getDefaultHttpClient();
48+
$this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest();
49+
$this->initialize();
50+
}
51+
52+
/**
53+
* @return Client
54+
*/
55+
protected function getDefaultHttpClient()
56+
{
57+
return new Client();
58+
}
59+
60+
/**
61+
* @return HttpRequest
62+
*/
63+
protected function getDefaultHttpRequest()
64+
{
65+
return HttpRequest::createFromGlobals();
66+
}
67+
68+
/**
69+
* @inheritdoc
70+
*/
71+
public function initialize(array $parameters = [])
72+
{
73+
$this->parameters = new ParameterBag;
74+
// set default parameters
75+
foreach ($this->getDefaultParameters() as $key => $value) {
76+
if (is_array($value)) {
77+
$this->parameters->set($key, reset($value));
78+
} else {
79+
$this->parameters->set($key, $value);
80+
}
81+
}
82+
Helper::initialize($this, $parameters);
83+
84+
return $this;
85+
}
86+
87+
/**
88+
* @inheritdoc
89+
*/
90+
public function getDefaultParameters()
91+
{
92+
return [];
93+
}
94+
95+
/**
96+
* @inheritdoc
97+
*/
98+
public function getShortName()
99+
{
100+
return Helper::getDeliveryShortName(get_class($this));
101+
}
102+
103+
/**
104+
* @param string $key
105+
*
106+
* @return mixed
107+
*/
108+
public function getParameter($key)
109+
{
110+
return $this->parameters->get($key);
111+
}
112+
113+
/**
114+
* @param string $key
115+
* @param mixed $value
116+
*
117+
* @return $this
118+
*/
119+
public function setParameter($key, $value)
120+
{
121+
$this->parameters->set($key, $value);
122+
123+
return $this;
124+
}
125+
126+
/**
127+
* @see AbstractResponse
128+
*
129+
* @param string $class
130+
* @param array $parameters
131+
*
132+
* @return AbstractRequest
133+
*/
134+
protected function createRequest($class, array $parameters)
135+
{
136+
/** @var AbstractRequest $obj */
137+
$obj = new $class($this->httpClient, $this->httpRequest);
138+
139+
return $obj->initialize(array_replace($this->getParameters(), $parameters));
140+
}
141+
142+
/**
143+
* @return array
144+
*/
145+
public function getParameters()
146+
{
147+
return $this->parameters->all();
148+
}
149+
}

src/Boxberry.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Dumkaaa\Boxberry\Api\Common;
4+
5+
use Dumkaaa\Boxberry\Api\Common\Http\Client;
6+
7+
/**
8+
* Class Boxberry
9+
*
10+
* @codingStandardsIgnoreStart
11+
* @method static DeliveryInterface create(string $class, Client $httpClient = null,
12+
* \Symfony\Component\HttpFoundation\Request $httpRequest = null)
13+
* @codingStandardsIgnoreEnd
14+
* @package Dumkaaa\Boxberry\Api\Common
15+
*/
16+
class Boxberry
17+
{
18+
/**
19+
* @var DeliveryFactory
20+
*/
21+
private static $factory;
22+
23+
/**
24+
* @param $method
25+
* @param $parameters
26+
*
27+
* @return mixed
28+
*/
29+
public static function __callStatic($method, $parameters)
30+
{
31+
$factory = self::getFactory();
32+
33+
return call_user_func_array([$factory, $method], $parameters);
34+
}
35+
36+
/**
37+
* @return DeliveryFactory
38+
*/
39+
public static function getFactory()
40+
{
41+
if (self::$factory === null) {
42+
self::$factory = new DeliveryFactory;
43+
}
44+
45+
return self::$factory;
46+
}
47+
48+
/**
49+
* @param DeliveryFactory|null $factory
50+
*/
51+
public static function setFactory(DeliveryFactory $factory = null)
52+
{
53+
self::$factory = $factory;
54+
}
55+
}

src/DeliveryFactory.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
4+
namespace Dumkaaa\Boxberry\Api\Common;
5+
6+
use Dumkaaa\Boxberry\Api\Common\Exception\RuntimeException;
7+
use Dumkaaa\Boxberry\Api\Common\Http\Client;
8+
use Symfony\Component\HttpFoundation\Request as HttpRequest;
9+
10+
class DeliveryFactory
11+
{
12+
/**
13+
* @var array
14+
*/
15+
private $deliveries = [];
16+
17+
/**
18+
* Создает новый объект доставки
19+
*
20+
* @param string $class
21+
* @param Client|null $httpClient
22+
* @param HttpRequest|null $httpRequest
23+
*
24+
* @return DeliveryInterface
25+
* @throws RuntimeException
26+
*/
27+
public function create($class, Client $httpClient = null, HttpRequest $httpRequest = null)
28+
{
29+
$class = Helper::getDeliveryClassName($class);
30+
if (!class_exists($class)) {
31+
throw new RuntimeException("Class '$class' not found");
32+
}
33+
34+
return new $class($httpClient, $httpRequest);
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function all()
41+
{
42+
return $this->deliveries;
43+
}
44+
45+
/**
46+
* @param array $deliveries
47+
*/
48+
public function replace(array $deliveries)
49+
{
50+
$this->deliveries = $deliveries;
51+
}
52+
53+
/**
54+
* @param $className
55+
*/
56+
public function register($className)
57+
{
58+
if (!in_array($className, $this->deliveries)) {
59+
$this->deliveries[] = $className;
60+
}
61+
}
62+
}

src/DeliveryInterface.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
4+
namespace Dumkaaa\Boxberry\Api\Common;
5+
6+
interface DeliveryInterface
7+
{
8+
/**
9+
* Возвращает имя используемого сервиса
10+
* • b2c - для интернет магазинов;
11+
* • с2c - ПиП (письма и посылки);
12+
*
13+
* @return string
14+
*/
15+
public function getName();
16+
17+
/**
18+
* Возвращает короткое имя класса используемого сервиса
19+
*
20+
* @see DeliveryFactory
21+
* @return string
22+
*/
23+
public function getShortName();
24+
25+
/**
26+
* Возвращает параметры по умолчанию для сервиса в формате:
27+
* array(
28+
* 'token' => '',
29+
* 'endPoint' => '',
30+
* );
31+
*
32+
* @return array
33+
*/
34+
public function getDefaultParameters();
35+
36+
/**
37+
* Заполняет сервис переданными параметрами
38+
*
39+
* $parameters = [
40+
* 'api_token' string (Required) Ключ доступа
41+
* 'endPoint' string (Required) Урл api
42+
* ]
43+
*
44+
* @param array $parameters Входные параметры (см. выше)
45+
*
46+
* @return $this
47+
*/
48+
public function initialize(array $parameters = []);
49+
50+
/**
51+
* Возвращает все параметры сервиса
52+
*
53+
* @return array
54+
*/
55+
public function getParameters();
56+
}

0 commit comments

Comments
 (0)