1
0
Fork 0
mirror of synced 2025-04-05 06:03:34 +03:00
This commit is contained in:
Akolzin Dmitry 2021-02-05 14:42:50 +03:00
parent 6fb4a5d6e1
commit 3a106adc98
4 changed files with 241 additions and 0 deletions

View file

@ -0,0 +1,148 @@
## Installation
`composer require retailcrm/service-bundle`
## Usage
Enable bundle in `config/bundles.php`:
```php
<?php
return [
// other bundles
RetailCrm\ServiceBundle\RetailCrmServiceBundle::class => ['all' => true]
];
```
Create bundle config file in `config/packages/retail_crm_service.yaml`:
```yaml
retail_crm_service:
request_schema: ~
```
### Deserializing incoming requests
#### Callbacks (form data)
To automatically get the callback request parameter
```php
class AppController extends AbstractController
{
public function activityAction(\App\Dto\Callback\Activity $activity): Response
{
// handle activity
}
}
```
add to the config:
```yaml
retail_crm_service:
request_schema:
callback:
- type: App\Dto\Callback\Activity
params: ["activity"]
```
request automatically will be deserialization to $activity.
#### Body json content
```php
class AppController extends AbstractController
{
public function someAction(\App\Dto\Body $activity): Response
{
// handle activity
}
}
```
add to the config:
```yaml
retail_crm_service:
request_schema:
client:
- App\Dto\Body
```
### Authentication
Example security configuration:
```yaml
security:
providers:
client:
entity:
class: 'App\Entity\Connection' # must implements UserInterface
property: 'clientId'
firewalls:
api:
pattern: ^/api
provider: client
anonymous: ~
lazy: true
stateless: false
guard:
authenticators:
- RetailCrm\ServiceBundle\Security\FrontApiClientAuthenticator
callback:
pattern: ^/callback
provider: client
anonymous: ~
lazy: true
stateless: true
guard:
authenticators:
- RetailCrm\ServiceBundle\Security\CallbackClientAuthenticator
main:
anonymous: true
lazy: true
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } # login for programmatically authentication user
- { path: ^/api, roles: ROLE_USER }
- { path: ^/callback, roles: ROLE_USER }
```
To authenticate the user after creating it, you can use the following code
```php
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use RetailCrm\ServiceBundle\Security\FrontApiClientAuthenticator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class AppController extends AbstractController
{
public function someAction(
Request $request,
GuardAuthenticatorHandler $guardAuthenticatorHandler,
FrontApiClientAuthenticator $frontApiClientAuthenticator,
ConnectionManager $manager
): Response {
$user = $manager->getUser(); // getting user
$guardAuthenticatorHandler->authenticateUserAndHandleSuccess(
$user,
$request,
$frontApiClientAuthenticator,
'api'
);
// ...
}
}
```

View file

@ -0,0 +1,55 @@
<?php
namespace RetailCrm\ServiceBundle\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase;
use RetailCrm\ServiceBundle\ArgumentResolver\CallbackValueResolver;
use RetailCrm\ServiceBundle\ArgumentResolver\ClientValueResolver;
use RetailCrm\ServiceBundle\DependencyInjection\RetailCrmServiceExtension;
use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory;
use RetailCrm\ServiceBundle\Security\CallbackClientAuthenticator;
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;
protected function setUp(): void
{
$container = new ContainerBuilder(new EnvPlaceholderParameterBag());
$container->getCompilerPassConfig()->setOptimizationPasses([]);
$container->getCompilerPassConfig()->setRemovingPasses([]);
$extension = new RetailCrmServiceExtension();
$extension->load(
[
[
'request_schema' => []
]
],
$container
);
$container->compile();
$this->container = $container;
}
public function testLoad(): void
{
static::assertTrue($this->container->hasParameter('retail_crm_service.request_schema.callback'));
static::assertTrue($this->container->hasParameter('retail_crm_service.request_schema.client'));
static::assertTrue($this->container->hasDefinition(CallbackValueResolver::class));
static::assertTrue($this->container->hasDefinition(ClientValueResolver::class));
static::assertTrue($this->container->hasDefinition(ErrorJsonResponseFactory::class));
static::assertTrue($this->container->hasDefinition(CallbackClientAuthenticator::class));
static::assertTrue($this->container->hasDefinition(FrontApiClientAuthenticator::class));
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace RetailCrm\ServiceBundle\Tests\Response;
use PHPUnit\Framework\TestCase;
use RetailCrm\ServiceBundle\Models\Error;
use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class ErrorJsonResponseFactoryTest extends TestCase
{
private $serializer;
protected function setUp(): void
{
$this->serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
}
public function testCreate(): void
{
$factory = new ErrorJsonResponseFactory($this->serializer);
$error = new Error();
$error->message = 'Test error message';
$result = $factory->create($error);
static::assertInstanceOf(JsonResponse::class, $result);
static::assertEquals(Response::HTTP_BAD_REQUEST, $result->getStatusCode());
static::assertEquals('{"code":null,"message":"Test error message","details":null}', $result->getContent());
}
}

View file

@ -11,6 +11,9 @@
bootstrap="vendor/autoload.php"
>
<coverage>
<include>
<directory>./</directory>
</include>
<exclude>
<directory>./Tests</directory>
<directory>./vendor</directory>