1
0
mirror of synced 2025-02-21 06:23:16 +03:00

logger support

This commit is contained in:
Pavel 2020-09-29 14:18:53 +03:00
parent 3caa7e7afa
commit 6d0260d642
13 changed files with 266 additions and 77 deletions

View File

@ -25,7 +25,8 @@
"jms/serializer": "^3.9", "jms/serializer": "^3.9",
"shieldon/psr-http": "^1.2", "shieldon/psr-http": "^1.2",
"doctrine/annotations": "^1.10", "doctrine/annotations": "^1.10",
"doctrine/cache": "^1.10" "doctrine/cache": "^1.10",
"psr/log": "^1.1"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^9.3", "phpunit/phpunit": "^9.3",

View File

@ -3,36 +3,36 @@
/** /**
* PHP version 7.3 * PHP version 7.3
* *
* @category ClientFactory * @category ClientBuilder
* @package RetailCrm\Factory * @package RetailCrm\Builder
* @author RetailCRM <integration@retailcrm.ru> * @author RetailCRM <integration@retailcrm.ru>
* @license MIT * @license MIT
* @link http://retailcrm.ru * @link http://retailcrm.ru
* @see http://help.retailcrm.ru * @see http://help.retailcrm.ru
*/ */
namespace RetailCrm\Factory; namespace RetailCrm\Builder;
use Psr\Container\ContainerInterface;
use RetailCrm\Component\Constants; use RetailCrm\Component\Constants;
use RetailCrm\Component\ServiceLocator; use RetailCrm\Component\ServiceLocator;
use RetailCrm\Factory\RequestFactory;
use RetailCrm\Interfaces\AppDataInterface; use RetailCrm\Interfaces\AppDataInterface;
use RetailCrm\Interfaces\AuthenticatorInterface; use RetailCrm\Interfaces\AuthenticatorInterface;
use RetailCrm\Interfaces\BuilderInterface;
use RetailCrm\Interfaces\ContainerAwareInterface; use RetailCrm\Interfaces\ContainerAwareInterface;
use RetailCrm\Interfaces\FactoryInterface;
use RetailCrm\TopClient\Client; use RetailCrm\TopClient\Client;
use RetailCrm\Traits\ContainerAwareTrait; use RetailCrm\Traits\ContainerAwareTrait;
/** /**
* Class ClientFactory * Class ClientBuilder
* *
* @category ClientFactory * @category ClientBuilder
* @package RetailCrm\Factory * @package RetailCrm\Builder
* @author RetailDriver LLC <integration@retailcrm.ru> * @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT * @license MIT
* @link http://retailcrm.ru * @link http://retailcrm.ru
* @see https://help.retailcrm.ru * @see https://help.retailcrm.ru
*/ */
class ClientFactory implements ContainerAwareInterface, FactoryInterface class ClientBuilder implements ContainerAwareInterface, BuilderInterface
{ {
use ContainerAwareTrait; use ContainerAwareTrait;
@ -43,24 +43,19 @@ class ClientFactory implements ContainerAwareInterface, FactoryInterface
protected $authenticator; protected $authenticator;
/** /**
* @param \Psr\Container\ContainerInterface $container * @return static
*
* @return \RetailCrm\Factory\ClientFactory
*/ */
public static function withContainer(ContainerInterface $container): ClientFactory public static function create(): self
{ {
$factory = new self(); return new self();
$factory->setContainer($container);
return $factory;
} }
/** /**
* @param \RetailCrm\Interfaces\AppDataInterface $appData * @param \RetailCrm\Interfaces\AppDataInterface $appData
* *
* @return ClientFactory * @return ClientBuilder
*/ */
public function setAppData(AppDataInterface $appData): ClientFactory public function setAppData(AppDataInterface $appData): ClientBuilder
{ {
$this->appData = $appData; $this->appData = $appData;
return $this; return $this;
@ -81,7 +76,7 @@ class ClientFactory implements ContainerAwareInterface, FactoryInterface
* @return \RetailCrm\TopClient\Client * @return \RetailCrm\TopClient\Client
* @throws \RetailCrm\Component\Exception\ValidationException * @throws \RetailCrm\Component\Exception\ValidationException
*/ */
public function create(): Client public function build(): Client
{ {
$client = new Client($this->appData, $this->authenticator); $client = new Client($this->appData, $this->authenticator);
$client->setHttpClient($this->container->get(Constants::HTTP_CLIENT)); $client->setHttpClient($this->container->get(Constants::HTTP_CLIENT));

View File

@ -3,27 +3,27 @@
/** /**
* PHP version 7.3 * PHP version 7.3
* *
* @category EnvironmentFactory * @category ContainerBuilder
* @package RetailCrm\Factory * @package RetailCrm\Builder
* @author RetailCRM <integration@retailcrm.ru> * @author RetailCRM <integration@retailcrm.ru>
* @license MIT * @license MIT
* @link http://retailcrm.ru * @link http://retailcrm.ru
* @see http://help.retailcrm.ru * @see http://help.retailcrm.ru
*/ */
namespace RetailCrm\Factory; namespace RetailCrm\Builder;
use JMS\Serializer\GraphNavigatorInterface;
use JMS\Serializer\Handler\HandlerRegistry;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializerInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Http\Client\ClientInterface; use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use RetailCrm\Component\Constants; use RetailCrm\Component\Constants;
use RetailCrm\Component\DependencyInjection\Container; use RetailCrm\Component\DependencyInjection\Container;
use RetailCrm\Component\Environment; use RetailCrm\Component\Environment;
use RetailCrm\Component\ServiceLocator; use RetailCrm\Component\ServiceLocator;
use RetailCrm\Interfaces\FactoryInterface; use RetailCrm\Factory\FileItemFactory;
use RetailCrm\Factory\RequestFactory;
use RetailCrm\Factory\SerializerFactory;
use RetailCrm\Interfaces\BuilderInterface;
use RetailCrm\Service\RequestDataFilter; use RetailCrm\Service\RequestDataFilter;
use RetailCrm\Service\RequestSigner; use RetailCrm\Service\RequestSigner;
use Shieldon\Psr17\StreamFactory; use Shieldon\Psr17\StreamFactory;
@ -32,55 +32,77 @@ use Symfony\Component\Validator\Validator\TraceableValidator;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
/** /**
* Class EnvironmentFactory * Class ContainerBuilder
* *
* @category EnvironmentFactory * @category ContainerBuilder
* @package RetailCrm\Factory * @package RetailCrm\Builder
* @author RetailDriver LLC <integration@retailcrm.ru> * @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT * @license MIT
* @link http://retailcrm.ru * @link http://retailcrm.ru
* @see https://help.retailcrm.ru * @see https://help.retailcrm.ru
*/ */
class ContainerFactory implements FactoryInterface class ContainerBuilder implements BuilderInterface
{ {
/** /**
* @var string $env * @var string $env
*/ */
public $env; private $env;
/** /**
* @var \Psr\Http\Client\ClientInterface $httpClient * @var \Psr\Http\Client\ClientInterface $httpClient
*/ */
public $httpClient; private $httpClient;
/**
* @var \Psr\Log\LoggerInterface $logger
*/
private $logger;
/**
* @return static
*/
public static function create(): self
{
return new self();
}
/** /**
* @param string $environmentType * @param string $environmentType
* *
* @return ContainerFactory * @return ContainerBuilder
*/ */
public static function withEnv(string $environmentType = Environment::DEV): ContainerFactory public function setEnv(string $environmentType = Environment::DEV): self
{ {
$factory = new self(); $this->env = $environmentType;
$factory->env = $environmentType; return $this;
return $factory;
} }
/** /**
* @param \Psr\Http\Client\ClientInterface $httpClient * @param \Psr\Http\Client\ClientInterface $httpClient
* *
* @return \RetailCrm\Factory\ContainerFactory * @return \RetailCrm\Builder\ContainerBuilder
*/ */
public function withClient(ClientInterface $httpClient): ContainerFactory public function setClient(ClientInterface $httpClient): ContainerBuilder
{ {
$this->httpClient = $httpClient; $this->httpClient = $httpClient;
return $this; return $this;
} }
/**
* @param \Psr\Log\LoggerInterface $logger
*
* @return ContainerBuilder
*/
public function setLogger(LoggerInterface $logger): ContainerBuilder
{
$this->logger = $logger;
return $this;
}
/** /**
* @return \Psr\Container\ContainerInterface * @return \Psr\Container\ContainerInterface
*/ */
public function create(): ContainerInterface public function build(): ContainerInterface
{ {
$container = new Container(); $container = new Container();
@ -106,6 +128,7 @@ class ContainerFactory implements FactoryInterface
protected function setProdServices(Container $container): void protected function setProdServices(Container $container): void
{ {
$container->set(Constants::HTTP_CLIENT, $this->httpClient); $container->set(Constants::HTTP_CLIENT, $this->httpClient);
$container->set(Constants::LOGGER, $this->logger ?: new NullLogger());
$container->set( $container->set(
Constants::VALIDATOR, Constants::VALIDATOR,
Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator() Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator()

View File

@ -64,20 +64,6 @@ class AppData implements AppDataInterface
$this->appSecret = $appSecret; $this->appSecret = $appSecret;
} }
/**
* Constructor shortcut
*
* @param string $serviceUrl
* @param string $appKey
* @param string $appSecret
*
* @return \RetailCrm\Component\AppData
*/
public static function create(string $serviceUrl, string $appKey, string $appSecret): AppDataInterface
{
return new self($serviceUrl, $appKey, $appSecret);
}
/** /**
* @return string * @return string
*/ */

View File

@ -26,6 +26,7 @@ class Constants
{ {
public const HTTP_CLIENT = 'httpClient'; public const HTTP_CLIENT = 'httpClient';
public const SERIALIZER = 'serializer'; public const SERIALIZER = 'serializer';
public const LOGGER = 'logger';
public const VALIDATOR = 'validator'; public const VALIDATOR = 'validator';
public const TOP_VERSION = 'top-sdk-php-20180326'; public const TOP_VERSION = 'top-sdk-php-20180326';
public const SIGN_TYPE_MD5 = 'md5'; public const SIGN_TYPE_MD5 = 'md5';

View File

@ -0,0 +1,59 @@
<?php
/**
* PHP version 7.3
*
* @category AbstractLogger
* @package RetailCrm\Component\Logger
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Component\Logger;
use Psr\Log\AbstractLogger as BaseAbstractLogger;
/**
* Class AbstractLogger
*
* @category AbstractLogger
* @package RetailCrm\Component\Logger
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
abstract class AbstractLogger extends BaseAbstractLogger
{
/**
* @param mixed $level
* @param string $message
* @param array $context
*
* @return string
*/
protected function formatMessage($level, $message, $context = [])
{
return sprintf(
'[%s] %s %s',
$level,
$message,
$this->encodeContext($context)
);
}
/**
* @param array $context
*
* @return false|string
*/
protected function encodeContext(array $context = [])
{
try {
return json_encode($context, JSON_THROW_ON_ERROR);
} catch (\Exception $exception) {
return '';
}
}
}

View File

@ -0,0 +1,49 @@
<?php
/**
* PHP version 7.3
*
* @category FileLogger
* @package RetailCrm\Component\Logger
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Component\Logger;
/**
* Class FileLogger
*
* @category FileLogger
* @package RetailCrm\Component\Logger
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class FileLogger extends AbstractLogger
{
/** @var string $ */
private $fileName;
/**
* FileLogger constructor.
*
* @param string $fileName
*/
public function __construct(string $fileName)
{
$this->fileName = $fileName;
}
/**
* @param mixed $level
* @param string $message
* @param array $context
*/
public function log($level, $message, array $context = [])
{
error_log($this->formatMessage($level, $message, $context), 3, $this->fileName);
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* PHP version 7.3
*
* @category StdioLogger
* @package RetailCrm\Component\Logger
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Component\Logger;
/**
* Class StdioLogger
*
* @category StdioLogger
* @package RetailCrm\Component\Logger
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class StdoutLogger extends AbstractLogger
{
/**
* @param mixed $level
* @param string $message
* @param array $context
*/
public function log($level, $message, array $context = [])
{
fwrite(STDOUT, $this->formatMessage($level, $message, $context) . PHP_EOL);
}
}

View File

@ -0,0 +1,32 @@
<?php
/**
* PHP version 7.3
*
* @category BuilderInterface
* @package RetailCrm\Interfaces
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Interfaces;
/**
* Interface BuilderInterface
*
* @category BuilderInterface
* @package RetailCrm\Interfaces
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
interface BuilderInterface
{
/**
* @return object
*/
public function build();
}

View File

@ -32,7 +32,7 @@ interface ContainerAwareInterface
* *
* @return mixed * @return mixed
*/ */
public function setContainer(ContainerInterface $container): void; public function setContainer(ContainerInterface $container);
/** /**
* @return \Psr\Container\ContainerInterface * @return \Psr\Container\ContainerInterface

View File

@ -36,10 +36,13 @@ trait ContainerAwareTrait
/** /**
* @param \Psr\Container\ContainerInterface $container * @param \Psr\Container\ContainerInterface $container
*
* @return \RetailCrm\Traits\ContainerAwareTrait
*/ */
public function setContainer(ContainerInterface $container): void public function setContainer(ContainerInterface $container): self
{ {
$this->container = $container; $this->container = $container;
return $this;
} }
/** /**

View File

@ -4,10 +4,11 @@ namespace RetailCrm\Test;
use DateTime; use DateTime;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use RetailCrm\Builder\ContainerBuilder;
use RetailCrm\Component\AppData; use RetailCrm\Component\AppData;
use RetailCrm\Component\Authenticator\TokenAuthenticator; use RetailCrm\Component\Authenticator\TokenAuthenticator;
use RetailCrm\Component\Environment; use RetailCrm\Component\Environment;
use RetailCrm\Factory\ContainerFactory; use RetailCrm\Component\Logger\StdoutLogger;
use RetailCrm\Factory\FileItemFactory; use RetailCrm\Factory\FileItemFactory;
use RetailCrm\Interfaces\AppDataInterface; use RetailCrm\Interfaces\AppDataInterface;
use RetailCrm\Interfaces\AuthenticatorInterface; use RetailCrm\Interfaces\AuthenticatorInterface;
@ -22,7 +23,7 @@ use RetailCrm\Interfaces\AuthenticatorInterface;
* @link http://retailcrm.ru * @link http://retailcrm.ru
* @see https://help.retailcrm.ru * @see https://help.retailcrm.ru
*/ */
class TestCase extends \PHPUnit\Framework\TestCase abstract class TestCase extends \PHPUnit\Framework\TestCase
{ {
private $container; private $container;
@ -34,9 +35,11 @@ class TestCase extends \PHPUnit\Framework\TestCase
protected function getContainer($recreate = false): ContainerInterface protected function getContainer($recreate = false): ContainerInterface
{ {
if (null === $this->container || $recreate) { if (null === $this->container || $recreate) {
$this->container = ContainerFactory::withEnv(Environment::TEST) $this->container = ContainerBuilder::create()
->withClient(new \GuzzleHttp\Client()) ->setEnv(Environment::TEST)
->create(); ->setClient(new \GuzzleHttp\Client())
->setLogger(new StdoutLogger())
->build();
} }
return $this->container; return $this->container;

View File

@ -3,40 +3,41 @@
/** /**
* PHP version 7.4 * PHP version 7.4
* *
* @category ClientFactoryTest * @category ClientBuilderTest
* @package RetailCrm\Tests\Component * @package RetailCrm\Tests\Builder
* @author RetailCRM <integration@retailcrm.ru> * @author RetailCRM <integration@retailcrm.ru>
* @license MIT * @license MIT
* @link http://retailcrm.ru * @link http://retailcrm.ru
* @see http://help.retailcrm.ru * @see http://help.retailcrm.ru
*/ */
namespace RetailCrm\Tests\Component; namespace RetailCrm\Tests\Builder;
use RetailCrm\Component\AppData; use RetailCrm\Component\AppData;
use RetailCrm\Component\Authenticator\TokenAuthenticator; use RetailCrm\Component\Authenticator\TokenAuthenticator;
use RetailCrm\Component\ServiceLocator; use RetailCrm\Component\ServiceLocator;
use RetailCrm\Factory\ClientFactory; use RetailCrm\Builder\ClientBuilder;
use RetailCrm\Test\TestCase; use RetailCrm\Test\TestCase;
use RetailCrm\TopClient\Client; use RetailCrm\TopClient\Client;
/** /**
* Class ClientFactoryTest * Class ClientBuilderTest
* *
* @category ClientFactoryTest * @category ClientBuilderTest
* @package RetailCrm\Tests\Component * @package RetailCrm\Tests\Builder
* @author RetailDriver LLC <integration@retailcrm.ru> * @author RetailDriver LLC <integration@retailcrm.ru>
* @license MIT * @license MIT
* @link http://retailcrm.ru * @link http://retailcrm.ru
* @see https://help.retailcrm.ru * @see https://help.retailcrm.ru
*/ */
class ClientFactoryTest extends TestCase class ClientBuilderTest extends TestCase
{ {
public function testCreateClient() public function testCreateClient()
{ {
$client = ClientFactory::withContainer($this->getContainer()) $client = ClientBuilder::create()
->setAppData(AppData::create(AppData::OVERSEAS_ENDPOINT, 'appKey', 'helloworld')) ->setContainer($this->getContainer())
->setAppData(new AppData(AppData::OVERSEAS_ENDPOINT, 'appKey', 'helloworld'))
->setAuthenticator(new TokenAuthenticator('appKey', 'token')) ->setAuthenticator(new TokenAuthenticator('appKey', 'token'))
->create(); ->build();
self::assertInstanceOf(Client::class, $client); self::assertInstanceOf(Client::class, $client);
self::assertInstanceOf(ServiceLocator::class, $client->getServiceLocator()); self::assertInstanceOf(ServiceLocator::class, $client->getServiceLocator());