1
0
mirror of synced 2024-11-28 15:46:04 +03:00

Update code base

This commit is contained in:
Кривич Сергей 2022-07-20 14:38:42 +03:00
parent 22c3971bc8
commit 897df39d96
25 changed files with 28 additions and 359 deletions

View File

@ -5,16 +5,8 @@ namespace RetailCrm\ServiceBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* Class Configuration
*
* @package RetailCrm\ServiceBundle\DependencyInjection
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc }
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('retail_crm_service');

View File

@ -14,17 +14,8 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Reference;
/**
* Class RetailCrmServiceExtension
*
* @package RetailCrm\ServiceBundle\DependencyInjection
*/
class RetailCrmServiceExtension extends Extension
{
/**
* @param array $configs
* @param ContainerBuilder $container
*/
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = $this->getConfiguration($configs, $container);

View File

@ -5,33 +5,18 @@ namespace RetailCrm\ServiceBundle\Exceptions;
use InvalidArgumentException;
use Throwable;
/**
* Class InvalidRequestArgumentException
*
* @package RetailCrm\ServiceBundle\Exceptions
*/
class InvalidRequestArgumentException extends InvalidArgumentException
{
private $validateErrors;
/**
* InvalidRequestArgumentException constructor.
* @param string $message
* @param int $code
* @param array $errors
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, iterable $errors = [], Throwable $previous = null)
public function __construct(string $message = "", int $code = 0, array $errors = [], Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->validateErrors = $errors;
}
/**
* @return iterable
*/
public function getValidateErrors(): iterable
public function getValidateErrors(): array
{
return $this->validateErrors;
}

View File

@ -2,91 +2,54 @@
namespace RetailCrm\ServiceBundle\Messenger;
/**
* Class Message
*
* @package RetailCrm\ServiceBundle\Messenger
*/
abstract class CommandMessage
{
/** @var string */
protected $commandName;
protected string $commandName;
/** @var array */
protected $options = [];
protected array $options = [];
/** @var array */
protected $arguments = [];
protected array $arguments = [];
/**
* @return string
*/
public function getCommandName(): string
{
return $this->commandName;
}
/**
* @param string $commandName
*/
public function setCommandName(string $commandName): void
{
$this->commandName = $commandName;
}
/**
* @return array
*/
public function getOptions(): array
{
return $this->options;
}
/**
* @param array $options
*/
public function setOptions(array $options): void
{
$this->options = $options;
}
/**
* @return array
*/
public function getArguments(): array
{
return $this->arguments;
}
/**
* @param array $arguments
*/
public function setArguments(array $arguments): void
{
$this->arguments = $arguments;
}
/**
* @param string $key
* @param string $value
*/
public function addOption(string $key, string $value): void
{
$this->options[$key] = $value;
}
/**
* @param string $key
* @param string $value
*/
public function addArgument(string $key, string $value): void
{
$this->arguments[$key] = $value;
}
/**
* @return array
*/
public function getFormattedOptions(): array
{
$options = [];
@ -97,11 +60,6 @@ abstract class CommandMessage
return $options;
}
/**
* For lockable message
*
* @return array
*/
public function __serialize(): array
{
return [

View File

@ -6,31 +6,13 @@ use RetailCrm\ServiceBundle\Messenger\MessageHandler\JobRunner;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Exception;
/**
* Class MessageHandler
*
* @package RetailCrm\ServiceBundle\Messenger
*/
class MessageHandler implements MessageHandlerInterface
{
/**
* @var JobRunner
*/
private $runner;
/**
* CommandQueueHandler constructor.
*
* @param JobRunner $runner
*/
public function __construct(JobRunner $runner)
public function __construct(private JobRunner $runner)
{
$this->runner = $runner;
}
/**
* @param CommandMessage $message
*
* @throws Exception
*/
public function __invoke(CommandMessage $message): void

View File

@ -9,38 +9,17 @@ use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
/**
* Class InNewProcessRunner
*
* @package RetailCrm\ServiceBundle\Messenger\MessageHandler
*/
class InNewProcessRunner implements JobRunner
{
/** @var int Default timeout for process */
public const DEFAULT_TIMEOUT = 3600;
/**
* @var LoggerInterface
*/
private $logger;
private LoggerInterface $logger;
/**
* @var KernelInterface
*/
private $kernel;
private KernelInterface $kernel;
/**
* @var int
*/
private $timeout = self::DEFAULT_TIMEOUT;
private int $timeout = self::DEFAULT_TIMEOUT;
/**
* CommandQueueHandler constructor.
*
* @param LoggerInterface $logger
* @param KernelInterface $kernel
* @param int|null $timeout
*/
public function __construct(
LoggerInterface $logger,
KernelInterface $kernel,
@ -54,9 +33,6 @@ class InNewProcessRunner implements JobRunner
}
}
/**
* {@inheritdoc}
*/
public function run(CommandMessage $message): void
{
$phpBinaryPath = (new PhpExecutableFinder)->find();
@ -92,11 +68,6 @@ class InNewProcessRunner implements JobRunner
}
}
/**
* @param CommandMessage $message
*
* @return array
*/
private function getOptions(CommandMessage $message): array
{
$options = [];

View File

@ -4,15 +4,7 @@ namespace RetailCrm\ServiceBundle\Messenger\MessageHandler;
use RetailCrm\ServiceBundle\Messenger\CommandMessage;
/**
* Interface JobRunner
*
* @package RetailCrm\ServiceBundle\Messenger\MessageHandler
*/
interface JobRunner
{
/**
* @param CommandMessage $message
*/
public function run(CommandMessage $message): void;
}

View File

@ -9,38 +9,12 @@ use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Class SimpleConsoleRunner
*
* @package RetailCrm\ServiceBundle\Messenger\MessageHandler
*/
class SimpleConsoleRunner implements JobRunner
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var KernelInterface
*/
private $kernel;
/**
* CommandQueueHandler constructor.
*
* @param LoggerInterface $logger
* @param KernelInterface $kernel
*/
public function __construct(LoggerInterface $logger, KernelInterface $kernel)
public function __construct(private LoggerInterface $logger, private KernelInterface $kernel)
{
$this->logger = $logger;
$this->kernel = $kernel;
}
/**
* {@inheritdoc}
*/
public function run(CommandMessage $message): void
{
$application = new Application($this->kernel);

View File

@ -2,11 +2,6 @@
namespace RetailCrm\ServiceBundle\Messenger\Middleware;
/**
* Interface LockableMessage
*
* @package RetailCrm\ServiceBundle\Messenger\Middleware
*/
interface LockableMessage
{
public function __serialize(): array;

View File

@ -9,29 +9,13 @@ use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Throwable;
/**
* Class LockableMessageMiddleware
*
* @package RetailCrm\ServiceBundle\Messenger\Middleware
*/
class LockableMessageMiddleware implements MiddlewareInterface
{
/**
* @var LockFactory
*/
private $lockFactory;
public function __construct(LockFactory $lockFactory)
public function __construct(private LockFactory $lockFactory)
{
$this->lockFactory = $lockFactory;
}
/**
* @param Envelope $envelope
* @param StackInterface $stack
*
* @return Envelope
*
* @throws Throwable
*/
public function handle(Envelope $envelope, StackInterface $stack): Envelope
@ -56,11 +40,6 @@ class LockableMessageMiddleware implements MiddlewareInterface
return $stack->next()->handle($envelope, $stack);
}
/**
* @param LockableMessage $message
*
* @return string
*/
private function objectHash(LockableMessage $message): string
{
return hash('crc32', serialize($message));

View File

@ -2,25 +2,11 @@
namespace RetailCrm\ServiceBundle\Models;
/**
* Class Error
*
* @package RetailCrm\ServiceBundle\Models
*/
class Error
{
/**
* @var string
*/
public $code;
public string $code;
/**
* @var string
*/
public $message;
public string $message;
/**
* @var array
*/
public $details;
public array $details;
}

View File

@ -7,32 +7,12 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;
/**
* Class ErrorJsonResponseFactory
*
* @package RetailCrm\ServiceBundle\Response
*/
class ErrorJsonResponseFactory
{
private $serializer;
/**
* ErrorJsonResponseFactory constructor.
*
* @param SerializerInterface $serializer
*/
public function __construct(SerializerInterface $serializer)
public function __construct(private SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
/**
* @param Error $error
* @param int $statusCode
* @param array $headers
*
* @return Response
*/
public function create(Error $error, int $statusCode = Response::HTTP_BAD_REQUEST, array $headers = []): Response
{
return JsonResponse::fromJsonString(

View File

@ -2,28 +2,9 @@
namespace RetailCrm\ServiceBundle\Serializer;
/**
* Interface Adapter
*
* @package RetailCrm\ServiceBundle\Serializer
*/
interface Adapter
{
/**
* @param string $data
* @param string $type
* @param string $format
*
* @return object
*/
public function deserialize(string $data, string $type, string $format = 'json'): object;
/**
* @param array $data
* @param string $type
* @param string|null $format
*
* @return object
*/
public function arrayToObject(array $data, string $type, ?string $format = null): object;
}

View File

@ -6,50 +6,26 @@ use JMS\Serializer\ArrayTransformerInterface;
use JMS\Serializer\Context;
use JMS\Serializer\SerializerInterface;
/**
* Class JMSSerializerAdapter
*
* @package RetailCrm\ServiceBundle\Serializer
*/
class JMSSerializerAdapter implements Adapter
{
private $serializer;
private $transformer;
private $context;
/**
* JMSSerializerAdapter constructor.
*
* @param SerializerInterface $serializer
* @param ArrayTransformerInterface $transformer
*/
public function __construct(
SerializerInterface $serializer,
ArrayTransformerInterface $transformer
private SerializerInterface $serializer,
private ArrayTransformerInterface $transformer
) {
$this->serializer = $serializer;
$this->transformer = $transformer;
}
/**
* {@inheritdoc }
*/
public function deserialize(string $data, string $type, string $format = 'json'): object
{
return $this->serializer->deserialize($data, $type, $format, $this->context);
}
/**
* {@inheritdoc }
*/
public function arrayToObject(array $data, string $type, ?string $format = null): object
{
return $this->transformer->fromArray($data, $type, $this->context);
}
/**
* @param Context $context
*/
public function setContext(Context $context): void
{
$this->context = $context;

View File

@ -5,48 +5,24 @@ namespace RetailCrm\ServiceBundle\Serializer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
/**
* Class SymfonySerializerAdapter
*
* @package RetailCrm\ServiceBundle\Serializer
*/
class SymfonySerializerAdapter implements Adapter
{
private $serializer;
private $denormalizer;
private $context = [];
private array $context = [];
/**
* SymfonySerializerAdapter constructor.
*
* @param SerializerInterface $serializer
* @param DenormalizerInterface $denormalizer
*/
public function __construct(SerializerInterface $serializer, DenormalizerInterface $denormalizer)
public function __construct(private SerializerInterface $serializer, private DenormalizerInterface $denormalizer)
{
$this->serializer = $serializer;
$this->denormalizer = $denormalizer;
}
/**
* {@inheritdoc }
*/
public function deserialize(string $data, string $type,string $format = 'json'): object
{
return $this->serializer->deserialize($data, $type, $format, $this->context);
}
/**
* {@inheritdoc }
*/
public function arrayToObject(array $data, string $type, string $format = null): object
{
return $this->denormalizer->denormalize($data, $type, $format, $this->context);
}
/**
* @param array $context
*/
public function setContext(array $context): void
{
$this->context = $context;

View File

@ -5,17 +5,11 @@ namespace RetailCrm\ServiceBundle\Tests\DataFixtures;
use Symfony\Component\Validator\Constraints as Assert;
use JMS\Serializer\Annotation as JMS;
/**
* Class RequestDto
*
* @package RetailCrm\ServiceBundle\Tests\DataFixtures
*/
class RequestDto
{
/**
* @var string
* @Assert\NotNull()
* @JMS\Type("string")
*/
public $param;
public string $param;
}

View File

@ -6,11 +6,6 @@ use PHPUnit\Framework\TestCase;
use RetailCrm\ServiceBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;
/**
* Class ConfigurationTest
*
* @package RetailCrm\ServiceBundle\Tests\DependencyInjection
*/
class ConfigurationTest extends TestCase
{
public function testConfig(): void

View File

@ -12,11 +12,6 @@ use RetailCrm\ServiceBundle\Security\FrontApiClientAuthenticator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
/**
* Class RetailCrmServiceExtensionTest
*
* @package RetailCrm\ServiceBundle\Tests\DependencyInjection
*/
class RetailCrmServiceExtensionTest extends TestCase
{
private $container;

View File

@ -3,21 +3,20 @@
namespace RetailCrm\ServiceBundle\Tests\Fixtures\App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class Kernel extends \Symfony\Component\HttpKernel\Kernel
{
use MicroKernelTrait;
public function registerBundles()
public function registerBundles(): array
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle()
];
}
protected function configureContainer(ContainerBuilder $container/*, LoaderInterface $loader*/): void
protected function configureContainer(ContainerBuilder $container): void
{
$container
->register(TestCommand::class, TestCommand::class)
@ -26,8 +25,4 @@ class Kernel extends \Symfony\Component\HttpKernel\Kernel
$container->setParameter('kernel.project_dir', __DIR__ . '/..');
}
// public function registerContainerConfiguration(LoaderInterface $loader)
// {
// }
}

View File

@ -5,11 +5,6 @@ namespace RetailCrm\ServiceBundle\Tests\Messenger;
use PHPUnit\Framework\TestCase;
use RetailCrm\ServiceBundle\Tests\DataFixtures\TestMessage;
/**
* Class CommandMessageTest
*
* @package RetailCrm\ServiceBundle\Tests\Messenger
*/
class CommandMessageTest extends TestCase
{
public function testMessage(): void

View File

@ -7,11 +7,6 @@ use RetailCrm\ServiceBundle\Messenger\MessageHandler\InNewProcessRunner;
use RetailCrm\ServiceBundle\Tests\Fixtures\App\TestCommandMessage;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* Class InNewProcessRunnerTest
*
* @package RetailCrm\ServiceBundle\Tests\Messenger\MessageHandler
*/
class InNewProcessRunnerTest extends KernelTestCase
{
protected function setUp(): void

View File

@ -7,11 +7,6 @@ use RetailCrm\ServiceBundle\Messenger\MessageHandler\SimpleConsoleRunner;
use RetailCrm\ServiceBundle\Tests\Fixtures\App\TestCommandMessage;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* Class SimpleConsoleRunnerTest
*
* @package RetailCrm\ServiceBundle\Tests\Messenger\MessageHandler
*/
class SimpleConsoleRunnerTest extends KernelTestCase
{
protected function setUp(): void

View File

@ -7,11 +7,6 @@ use RetailCrm\ServiceBundle\Messenger\CommandMessage;
use RetailCrm\ServiceBundle\Messenger\MessageHandler;
use RetailCrm\ServiceBundle\Messenger\MessageHandler\JobRunner;
/**
* Class MessageHandlerTest
*
* @package RetailCrm\ServiceBundle\Tests\Messenger
*/
class MessageHandlerTest extends TestCase
{
public function testRun(): void

View File

@ -15,17 +15,9 @@ use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
/**
* Class LockableMessageMiddlewareTest
*
* @package RetailCrm\ServiceBundle\Tests\Messenger\Middleware
*/
class LockableMessageMiddlewareTest extends TestCase
{
/**
* @var LockFactory
*/
private $lockFactory;
private LockFactory $lockFactory;
protected function setUp(): void
{
@ -35,7 +27,7 @@ class LockableMessageMiddlewareTest extends TestCase
public function testHandle(): void
{
$store = $this->createMock(PersistingStoreInterface::class);
$key = new Key(uniqid());
$key = new Key(uniqid('', true));
$lock = new Lock($key, $store);
$this->lockFactory->expects(static::once())->method('createLock')->willReturn($lock);
$envelope = new Envelope(new TestMessage(), [new ReceivedStamp('test')]);
@ -55,7 +47,7 @@ class LockableMessageMiddlewareTest extends TestCase
{
$store = $this->createMock(PersistingStoreInterface::class);
$store->method('save')->willThrowException(new LockConflictedException);
$key = new Key(uniqid());
$key = new Key(uniqid('', true));
$lock = new Lock($key, $store);
$this->lockFactory->expects(static::once())->method('createLock')->willReturn($lock);
$envelope = new Envelope(new TestMessage(), [new ReceivedStamp('test')]);
@ -75,7 +67,7 @@ class LockableMessageMiddlewareTest extends TestCase
{
$store = $this->createMock(PersistingStoreInterface::class);
$store->method('save')->willThrowException(new LockConflictedException);
$key = new Key(uniqid());
$key = new Key(uniqid('', true));
$lock = new Lock($key, $store);
$this->lockFactory->expects(static::never())->method('createLock')->willReturn($lock);
$envelope = new Envelope(new \stdClass(), [new ReceivedStamp('test')]);
@ -95,7 +87,7 @@ class LockableMessageMiddlewareTest extends TestCase
{
$store = $this->createMock(PersistingStoreInterface::class);
$store->method('save')->willThrowException(new LockConflictedException);
$key = new Key(uniqid());
$key = new Key(uniqid('', true));
$lock = new Lock($key, $store);
$this->lockFactory->expects(static::never())->method('createLock')->willReturn($lock);
$envelope = new Envelope(new TestMessage());

View File

@ -7,7 +7,7 @@ use PHPUnit\Framework\TestCase;
use RetailCrm\ServiceBundle\Serializer\JMSSerializerAdapter;
use RetailCrm\ServiceBundle\Tests\DataFixtures\RequestDto;
class JSMSerializerAdapterTest extends TestCase
class JMSSerializerAdapterTest extends TestCase
{
private $serializer;
private $transformer;