Skip to content

Commit

Permalink
Add more classes. Upgrade to PHP 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
fre5h authored Aug 30, 2021
1 parent 682ebe0 commit f47ddfc
Show file tree
Hide file tree
Showing 106 changed files with 3,767 additions and 155 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.styleci.yml export-ignore
/.travis.yml export-ignore
/phpstan.neon export-ignore
/phpunit.xml.dist export-ignore
/Tests export-ignore
21 changes: 12 additions & 9 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ jobs:
- name: 'Checkout Code'
uses: actions/checkout@v2

- name: 'Setup PHP'
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
ini-values: memory_limit=-1
coverage: none
tools: composer:v2

- name: 'Install Dependencies'
run: composer install --prefer-dist --no-progress --no-interaction

Expand All @@ -41,7 +49,7 @@ jobs:
- name: 'Setup PHP'
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 7.4
ini-values: memory_limit=-1
coverage: none
tools: composer:v2
Expand All @@ -66,7 +74,7 @@ jobs:
- name: 'Setup PHP'
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 7.4
ini-values: memory_limit=-1
coverage: none
tools: composer:v2
Expand All @@ -86,14 +94,9 @@ jobs:
fail-fast: false
matrix:
php-version:
- '7.3'
- '7.4'
- '8.0'
symfony-version:
- '5.0'
- '5.1'
- '5.2'
- '5.3'
- '5.4'
steps:
- name: 'Checkout Code'
uses: actions/checkout@v2
Expand Down Expand Up @@ -128,7 +131,7 @@ jobs:
- name: 'Setup PHP'
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
php-version: 7.4
ini-values: memory_limit=-1
coverage: pcov
tools: composer:v2
Expand Down
3 changes: 1 addition & 2 deletions Annotation/DTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class DTO implements DtoAnnotationInterface
{
private const DTO_SUFFIX = 'Dto';

/** @var string */
private $class;
private string $class;

/**
* @param mixed[] $options
Expand Down
3 changes: 1 addition & 2 deletions Annotation/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class JsonSchema implements JsonSchemaAnnotationInterface
{
private const JSON_FILE_EXTENSION = '.json';

/** @var string */
private $jsonSchemaName;
private string $jsonSchemaName;

/**
* @param mixed[] $options
Expand Down
2 changes: 1 addition & 1 deletion DTO/DtoWithRelationToEntityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public function getEntityId(): ?string;
*
* @return self
*/
public function setEntityId(string $entityId);
public function setEntityId(string $entityId): self;
}
3 changes: 1 addition & 2 deletions DTO/DtoWithRelationToEntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
*/
trait DtoWithRelationToEntityTrait
{
/** @var string */
private $entityId;
private ?string $entityId = null;

/**
* @return string|null
Expand Down
2 changes: 1 addition & 1 deletion DTO/OptimisticLockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public function getEditVersion(): int;
*
* @return self
*/
public function setEditVersion(int $editVersion);
public function setEditVersion(int $editVersion): self;
}
4 changes: 1 addition & 3 deletions DTO/OptimisticLockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@
trait OptimisticLockTrait
{
/**
* @var int
*
* @Assert\NotBlank()
* @Assert\Type("int")
* @Assert\GreaterThan(0)
*/
private $editVersion;
private int $editVersion;

/**
* @return int
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode = $treeBuilder->getRootNode();
$rootNode
->children()
->scalarNode('api_host')->cannotBeEmpty()->isRequired()->end()
->scalarNode('json_schema_dir')->defaultValue('%kernel.project_dir%/src/Json/Schema/')->cannotBeEmpty()->end()
->end()
;
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/StfalconApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yaml');

$container->setParameter('stfalcon_api.api_host', $config['api_host']);
$container->setParameter('stfalcon_api.json_schema_dir', $config['json_schema_dir']);
$container->registerForAutoconfiguration(CustomAppExceptionResponseProcessorInterface::class)->addTag('stfalcon_api.exception_response_processor');
}
Expand Down
10 changes: 3 additions & 7 deletions Entity/JWT/RefreshToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,18 @@
class RefreshToken extends AbstractRefreshToken
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
protected ?int $id = null;

/**
* @var \DateTimeImmutable
*
* @ORM\Column(type="datetimetz_immutable")
*
* @Assert\Type("\DateTimeImmutable")
*/
private $createdAt;
private \DateTimeImmutable $createdAt;

/**
* Constructor.
Expand All @@ -61,7 +57,7 @@ public function __construct()
/**
* {@inheritdoc}
*/
public function getId()
public function getId(): ?int
{
return $this->id;
}
Expand Down
60 changes: 60 additions & 0 deletions Error/BaseErrorNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Error;

/**
* BaseErrorNames.
*/
class BaseErrorNames
{
// 400
public const MISSED_REQUIRED_HEADER = 'missed_required_header';
public const MALFORMED_JSON = 'malformed_json';
public const INVALID_JSON_SCHEMA = 'invalid_json_schema';
public const INCORRECT_HEADER = 'incorrect_header';
public const INVALID_REQUEST = 'invalid_request';

// 401
public const UNAUTHORISED_USER = 'unauthorised_user';
public const INVALID_REFRESH_TOKEN = 'invalid_refresh_token';

// 402
public const INVALID_PAYMENT = 'invalid_payment';

// 403
public const ACCESS_DENIED = 'access_denied';
public const AUTHORIZED_USER = 'authorized_user';

// 404
public const RESOURCE_NOT_FOUND = 'resource_not_found';

// 405
public const METHOD_NOT_ALLOWED = 'method_not_allowed';

// 409
public const CONFLICT_TARGET_RESOURCE_UPDATE = 'conflict_target_resource_update';

// 422
public const INVALID_ENTITY = 'invalid_entity';

// 500
public const INTERNAL_SERVER_ERROR = 'internal_server_error';

/**
* Constructor.
*/
private function __construct()
{
// noop
}
}
3 changes: 1 addition & 2 deletions Event/User/AbstractUserEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
*/
abstract class AbstractUserEvent extends Event
{
/** @var UserInterface */
private $user;
private UserInterface $user;

/**
* @param UserInterface $user
Expand Down
57 changes: 57 additions & 0 deletions EventListener/JWT/JwtRefreshSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\EventListener\JWT;

use Gesdinet\JWTRefreshTokenBundle\Event\RefreshEvent;
use StfalconStudio\ApiBundle\Entity\JWT\RefreshToken;
use StfalconStudio\ApiBundle\Exception\JWT\InvalidRefreshTokenException;
use StfalconStudio\ApiBundle\Model\Credentials\CredentialsInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* JwtRefreshSubscriber.
*/
final class JwtRefreshSubscriber implements EventSubscriberInterface
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): iterable
{
yield 'gesdinet.refresh_token' => 'processRefreshToken';
yield RefreshEvent::class => 'processRefreshToken';
}

/**
* @param RefreshEvent $event
*
* @throws InvalidRefreshTokenException
*/
public function processRefreshToken(RefreshEvent $event): void
{
$user = $event->getPreAuthenticatedToken()->getUser();

if ($user instanceof CredentialsInterface) {
$refreshToken = $event->getRefreshToken();

if ($refreshToken instanceof RefreshToken) {
$userCredentialsLastChangedAt = $user->getCredentialsLastChangedAt();
$refreshTokenCreatedAt = $refreshToken->getCreatedAt()->getTimestamp();

if ($userCredentialsLastChangedAt instanceof \DateTimeInterface && $refreshTokenCreatedAt < $userCredentialsLastChangedAt->getTimestamp()) {
throw new InvalidRefreshTokenException();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\EventListener\Jwt;
namespace StfalconStudio\ApiBundle\EventListener\JWT;

use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTExpiredEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTInvalidEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTNotFoundEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use StfalconStudio\ApiBundle\Error\ErrorNames;
use StfalconStudio\ApiBundle\Error\BaseErrorNames;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -29,8 +29,7 @@ class JwtSubscriber implements EventSubscriberInterface
{
private const ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION = 'onAuthenticationFailureResponse';

/** @var TranslatorInterface */
protected $translator;
protected TranslatorInterface $translator;

/**
* @param TranslatorInterface $translator
Expand Down Expand Up @@ -63,22 +62,19 @@ public function onAuthenticationFailureResponse(AuthenticationFailureEvent $even
switch (true) {
case $event instanceof JWTInvalidEvent:
$message = 'invalid_jwt_token_message';

break;
case $event instanceof JWTNotFoundEvent:
$message = 'not_found_jwt_token_message';

break;
case $event instanceof JWTExpiredEvent:
$message = 'expired_jwt_token_message';

break;
default:
$message = 'unauthorised_user_message';
}

$data = [
'error' => ErrorNames::UNAUTHORISED_USER,
'error' => BaseErrorNames::UNAUTHORISED_USER,
'errorDescription' => $this->translator->trans($message),
];

Expand Down
Loading

0 comments on commit f47ddfc

Please sign in to comment.