Skip to content

Commit

Permalink
Merge pull request #22 from Bukashk0zzz/symfony4
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
thecatontheflat authored Dec 17, 2017
2 parents dba00dc + a659812 commit e0863a6
Show file tree
Hide file tree
Showing 29 changed files with 4,956 additions and 877 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/*
/vendor/
.php_cs.cache
46 changes: 46 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PSR1' => true,
'@PSR2' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHP70Migration' => true,
'@PHP70Migration:risky' => true,
'@PHP71Migration' => true,
'phpdoc_summary' => false,
'yoda_style' => false,
'self_accessor' => false,
'combine_consecutive_unsets' => true,
'blank_line_after_opening_tag' => false,
'phpdoc_to_comment' => false,
'phpdoc_no_empty_return' => false,
'strict_param' => true,
'doctrine_annotation_indentation' => true,
'mb_str_functions' => true,
'native_function_invocation' => true,
'no_short_echo_tag' => true,
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_class_elements' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_order' => true,
'simplified_null_return' => false,
'strict_comparison' => true,
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
'declare_equal_normalize' => ['space' => 'single'],
'array_syntax' => ['syntax' => 'short'],
'list_syntax' => ['syntax' => 'short'],
'doctrine_annotation_braces' => ['syntax' => 'with_braces'],
'general_phpdoc_annotation_remove' => ['annotations' => ['author', 'created', 'version', 'package', 'copyright', 'license', 'throws']],
])
->setFinder($finder)
;
30 changes: 30 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
language: php
sudo: false

php:
- 7.1

env:
- SYMFONY_VERSION="3.3.*"
- SYMFONY_VERSION="4.0.*"

before_script:
- composer self-update
- sed -i -e 's/^3.3|^4.0/${SYMFONY_VERSION}/g' composer.json;
- composer install --dev --prefer-dist --no-interaction -o

script:
- ./vendor/bin/phpcs -p --standard=./ruleset.xml
- ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover=coverage.xml

after_success:
- bash <(curl -s https://codecov.io/bash)

cache:
directories:
- $COMPOSER_CACHE_DIR

notifications:
email:
- [email protected]
- [email protected]
5 changes: 4 additions & 1 deletion AtlassianConnectBundle.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
<?php declare(strict_types = 1);

namespace AtlassianConnectBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Class AtlassianConnectBundle
*/
class AtlassianConnectBundle extends Bundle
{
}
80 changes: 60 additions & 20 deletions Command/RequestAPICommand.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,86 @@
<?php
<?php declare(strict_types = 1);

namespace AtlassianConnectBundle\Command;

use AtlassianConnectBundle\Entity\TenantInterface;
use AtlassianConnectBundle\Model\JWTRequest;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class RequestAPICommand extends ContainerAwareCommand
/**
* Class RequestAPICommand
*/
class RequestAPICommand extends Command
{
protected function configure()
/**
* @var bool
*/
protected $shouldNotRun;

/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;

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

/**
* @param ManagerRegistry $registry
* @param string $tenantClass
*/
public function __construct(ManagerRegistry $registry, string $tenantClass)
{
$this->em = $registry->getManager();
$this->tenantClass = $tenantClass;

parent::__construct();
}

/**
* @return void
*/
protected function configure(): void
{
$this
->setName('ac:request-api')
->addArgument('rest-url', InputArgument::REQUIRED, "REST api endpoint, like /rest/api/2/issue/{issueIdOrKey}")
->addOption('client-key', "c", InputOption::VALUE_REQUIRED, "Client-key from tenant")
->addOption('tenant-id', "t", InputOption::VALUE_REQUIRED, "Tenant-id")
->setDescription('Request REST end-points.
Documentation available on https://docs.atlassian.com/jira/REST/cloud/');
->addArgument('rest-url', InputArgument::REQUIRED, 'REST api endpoint, like /rest/api/2/issue/{issueIdOrKey}')
->addOption('client-key', 'c', InputOption::VALUE_REQUIRED, 'Client-key from tenant')
->addOption('tenant-id', 't', InputOption::VALUE_REQUIRED, 'Tenant-id')
->setDescription('Request REST end-points. Documentation available on https://docs.atlassian.com/jira/REST/cloud/');
}

protected function execute(InputInterface $input, OutputInterface $output)
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output): void
{
$restUrl = $input->getArgument('rest-url');
$em = $this->getContainer()->get('doctrine')->getManager();
$tenantClass = $this->getContainer()->getParameter("atlassian_connect_tenant_entity_class");
if($input->getOption("tenant-id")) {
$tenant = $em->getRepository($tenantClass)
->find($input->getOption("tenant-id"));
} elseif($input->getOption("client-key")) {
$tenant = $em->getRepository($tenantClass)
->findOneByClientKey($input->getOption('client-key'));

/** @var TenantInterface $tenant */
if ($input->getOption('tenant-id')) {
$tenant = $this->em->getRepository($this->tenantClass)->find($input->getOption('tenant-id'));
} elseif ($input->getOption('client-key')) {
/** @noinspection PhpUndefinedMethodInspection */
$tenant = $this->em->getRepository($this->tenantClass)->findOneByClientKey($input->getOption('client-key'));
} else {
throw new \Exception("Please provide client-key or tenant-id");
throw new \RuntimeException('Please provide client-key or tenant-id');
}

$request = new JWTRequest($tenant);
$json = $request->get($restUrl);

$output->writeln('');
print json_encode($json, JSON_PRETTY_PRINT);
echo \json_encode($json, \JSON_PRETTY_PRINT);
$output->writeln('');
}
}
40 changes: 32 additions & 8 deletions Controller/DescriptorController.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
<?php
<?php declare(strict_types = 1);

namespace AtlassianConnectBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;

class DescriptorController extends Controller
/**
* Class DescriptorController
*/
class DescriptorController
{
public function indexAction()
/**
* @var KernelInterface
*/
private $kernel;

/**
* @var mixed[]
*/
private $config;

/**
* @param KernelInterface $kernel
* @param mixed[] $config
*/
public function __construct(KernelInterface $kernel, array $config)
{
$this->kernel = $kernel;
$this->config = $config;
}

/**
* @return Response
*/
public function indexAction(): Response
{
$kernel = $this->container->get('kernel');
$config = $this->getParameter('atlassian_connect');
$envConfig = $config[$kernel->getEnvironment()];
$descriptor = json_encode($envConfig);
$envConfig = $this->config[$this->kernel->getEnvironment()];
$descriptor = \json_encode($envConfig);

$response = new Response();
$response->setContent($descriptor);
Expand Down
73 changes: 55 additions & 18 deletions Controller/HandshakeController.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,75 @@
<?php
<?php declare(strict_types = 1);

namespace AtlassianConnectBundle\Controller;

use AtlassianConnectBundle\JWT\Authentication\JWT;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AtlassianConnectBundle\Entity\Tenant;
use Doctrine\Common\Persistence\ManagerRegistry;
use Firebase\JWT\JWT;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class HandshakeController extends Controller
/**
* Class HandshakeController
*/
class HandshakeController
{
public function registerAction(Request $request)
/**
* @var \Doctrine\Common\Persistence\ObjectManager
*/
private $em;

/**
* @var LoggerInterface
*/
private $logger;

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

/**
* @param ManagerRegistry $registry
* @param LoggerInterface $logger
* @param string $tenantClass
*/
public function __construct(ManagerRegistry $registry, LoggerInterface $logger, string $tenantClass)
{
$content = $request->getContent();
$content = json_decode($content, true);
$this->em = $registry->getManager();
$this->logger = $logger;
$this->tenantClass = $tenantClass;
}

$tenantClass = $this->getParameter("atlassian_connect_tenant_entity_class");
$tenant = $this->getDoctrine()->getRepository($tenantClass)
->findOneByClientKey($content['clientKey']);
/**
* @param Request $request
*
* @return Response
*/
public function registerAction(Request $request): Response
{
$content = $request->getContent();
$content = \json_decode($content, true);

if ($tenant) {
/** @var Tenant $tenant */
/** @noinspection PhpUndefinedMethodInspection */
$tenant = $this->em->getRepository($this->tenantClass)->findOneByClientKey($content['clientKey']);
if ($tenant !== null) {
try {
$authorizationHeaderArray = explode(' ', $request->headers->get('authorization'));
if (count($authorizationHeaderArray) > 1) {
$authorizationHeaderArray = \explode(' ', $request->headers->get('authorization'));
if (\count($authorizationHeaderArray) > 1) {
$jwt = $authorizationHeaderArray[1];
JWT::decode($jwt, $tenant->getSharedSecret(), ['HS256']);
} else {
throw new \InvalidArgumentException('Bad authorization header');
}
} catch (\Exception $e) {
$this->get('logger')->error($e->getMessage(), ['exception' => $e]);
} catch (\Throwable $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);

return new Response('Unauthorized', 401);
}
} else {
$tenantClass = $this->tenantClass;
$tenant = new $tenantClass();
}

Expand All @@ -48,8 +85,8 @@ public function registerAction(Request $request)
->setDescription($content['description'])
->setEventType($content['eventType']);

$this->getDoctrine()->getManager()->persist($tenant);
$this->getDoctrine()->getManager()->flush();
$this->em->persist($tenant);
$this->em->flush();

return new Response('OK', 200);
}
Expand Down
29 changes: 23 additions & 6 deletions Controller/UnlicensedController.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
<?php
<?php declare(strict_types = 1);

namespace AtlassianConnectBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class UnlicensedController extends Controller
/**
* Class UnlicensedController
*/
class UnlicensedController
{
public function unlicensedAction()
/**
* @var \Twig_Environment
*/
private $twig;

/**
* @param \Twig_Environment $twig
*/
public function __construct(\Twig_Environment $twig)
{
$this->twig = $twig;
}

/**
* @return string
*/
public function unlicensedAction(): string
{
return $this->render('@AtlassianConnect/unlicensed.html.twig');
return $this->twig->render('@AtlassianConnect/unlicensed.html.twig');
}
}
Loading

0 comments on commit e0863a6

Please sign in to comment.