Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions Data/JsonToArrayTransformer.php

This file was deleted.

5 changes: 4 additions & 1 deletion DependencyInjection/AlpixelJiraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('alpixel_jira.auth', $config['auth']);
$container->setParameter('alpixel_jira.auth.method', $config['auth']['method']);
$container->setParameter('alpixel_jira.auth.parameters', $config['auth']['parameters']);
$container->setParameter('alpixel_jira.auth.authentication_class', $config['auth']['authentication_class']);
$container->setParameter('alpixel_jira.base_url', $config['base_url']);
$container->setParameter('alpixel_jira.base_api', $config['base_api']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
Expand Down
43 changes: 23 additions & 20 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Alpixel\Bundle\JiraBundle\DependencyInjection;

use Alpixel\Bundle\JiraBundle\Request\BasicAuthentication;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -23,40 +24,42 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->scalarNode('base_url')->isRequired()->cannotBeEmpty()->end()
->append($this->addAuthParameters())
->scalarNode('base_api')->isRequired()->cannotBeEmpty()->end()
->append($this->addAuthConfiguration())
->end();

return $treeBuilder;
}

public function addAuthParameters()
public function addAuthConfiguration()
{
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('auth');

$node->isRequired()
->children()
->arrayNode('method')
->children()
->arrayNode('basic')
->children()
->scalarNode('username')->end()
->scalarNode('password')->end()
->end()
->end()
->arrayNode('oauth')
->children()
->scalarNode('id')->end()
->scalarNode('key')->end()
->end()
->end()
->end()
->scalarNode('method')->defaultValue('basic')->end()
->arrayNode('parameters')
->prototype('scalar')->end()
->end()
->scalarNode('authentication_class')
->defaultValue(BasicAuthentication::class)
->beforeNormalization()
->ifTrue(function ($v) { return (count($v) === 1) ? false : true ; })
->thenInvalid('You must set only one authentification method.')
->always()
->then(function ($fqcn) {
$isValidClass = false;
if (class_exists($fqcn)) {
$interfaces = class_implements($fqcn);
$isValidClass = isset($interfaces['\Alpixel\Bundle\JiraBundle\Request\AuthenticationInterface']);
}

if (!$isValidClass) {
throw new \InvalidArgumentException('The class has been not found or not implement "\Alpixel\Bundle\JiraBundle\Request\AuthenticationInterface"');
}
})
->end()
->end()
->end();

return $node;
}
}
204 changes: 204 additions & 0 deletions Request/AbstractRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

namespace Alpixel\Bundle\JiraBundle\Request;

use Alpixel\Bundle\JiraBundle\Response\Response;
use Psr\Log\LoggerInterface;


/**
* @author Alexis BUSSIERES <alexis@alpixel.fr>
*/
abstract class AbstractRequest
{
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';

/**
* @var string
*/
protected $baseUrlAPI;
/**
* @var SecurityContext
*/
private $securityContext;
/**
* @var array curl options
*/
private $curlOptions;
/**
* @var LoggerInterface
*/
private $monolog;

public function __construct(string $baseUrlAPI, SecurityContext $securityContext, LoggerInterface $monolog)
{
$this->setBaseUrlAPI($baseUrlAPI);
$this->setSecurityContext($securityContext);
$this->setMonolog($monolog);
}

/**
* @return string
*/
public function getBaseUrlAPI(): string
{
return $this->baseUrlAPI;
}

/**
* @param string $baseUrlAPI
* @return $this
*/
protected function setBaseUrlAPI(string $baseUrlAPI)
{
if (substr($baseUrlAPI, -1) !== '/') {
$baseUrlAPI .= '/';
}

$this->baseUrlAPI = $baseUrlAPI;

return $this;
}

/**
* @return SecurityContext
*/
public function getSecurityContext(): SecurityContext
{
return $this->securityContext;
}

/**
* @param SecurityContext $securityContext
* @return $this
*/
public function setSecurityContext(SecurityContext $securityContext)
{
$this->securityContext = $securityContext;

return $this;
}

/**
* @return LoggerInterface
*/
private function getMonolog()
{
return $this->monolog;
}

/**
* @param LoggerInterface $monolog
* @return LoggerInterface
*/
protected function setMonolog(LoggerInterface $monolog)
{
$this->monolog = $monolog;

return $this->monolog;
}

/**
* @return array
*/
public function getCurlOptions(): array
{
return $this->curlOptions;
}

/**
* @param array $curlOptions
* @return $this
*/
public function setCurlOptions(array $curlOptions)
{
$this->curlOptions = $curlOptions;

return $this;
}

public function get(string $url, array $urlParameters = [], array $curlOptions = [])
{
return $this->buildRequest(self::METHOD_GET, $url, $urlParameters, $curlOptions);
}

public function buildRequest(string $method, string $url, $urlParameters = [], array $curlOptions = [])
{
switch ($method) {
case self::METHOD_GET:
$curlOptions[CURLOPT_HTTPGET] = true;
if (!empty($urlParameters)) {
$url .= '?' . http_build_query($urlParameters);
}
break;
case self::METHOD_POST:
$curlOptions[CURLOPT_POST] = true;
break;
default:
throw new \InvalidArgumentException(sprintf('Unknown HTTP method "%s"'), $method);
}

return $this->executeRequest($url, $curlOptions);
}

/**
* Create final url to API
*
* @param $urlAPIEndpoint
* @return string
*/
public function createUrl($urlAPIEndpoint)
{
if (substr($urlAPIEndpoint, 0, 1) === '/') {
$urlAPIEndpoint = substr($urlAPIEndpoint, 1);
}

return $this->getBaseUrlAPI() . $urlAPIEndpoint;
}

/**
* @param array $curlOptions
* @return array
*/
protected function resolveCurlOptions(array $curlOptions = [])
{
return array_replace([
CURLOPT_TIMEOUT => 300,
CURLOPT_FORBID_REUSE => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
],
], $curlOptions);
}

/**
* @param string $urlAPIEndpoint
* @param array $curlOptions
* @return Response
*/
public function executeRequest(string $urlAPIEndpoint, array $curlOptions = [])
{
$url = $this->createUrl($urlAPIEndpoint);

$this->getMonolog()->info('Alpixel JIRA API [Request] : ' . $urlAPIEndpoint);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
$curlOptions = $this->resolveCurlOptions($curlOptions);
curl_setopt_array($ch, $curlOptions);

$this->getSecurityContext()->applyAuthentication($ch);

$data = curl_exec($ch);
$error = curl_error($ch);

$response = new Response();
$response->setResponse($ch, $error, $data);

return $response;
}
}
15 changes: 15 additions & 0 deletions Request/AuthenticationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Alpixel\Bundle\JiraBundle\Request;

/**
* @author Alexis BUSSIERES <alexis@alpixel.fr>
*/
interface AuthenticationInterface
{
/**
* @param resource (curl) $curlRessource
* @return mixed
*/
public function applyAuthentication($curlRessource);
}
Loading