* avatar handling changed to work around BC break jira changes

This commit is contained in:
Dávid Danyi 2018-04-17 11:27:16 +02:00
parent 8f005718e0
commit f46056837e
6 changed files with 51 additions and 94 deletions

View File

@ -77,6 +77,7 @@ return [
'customfield_12500',
],
],
'url.jiraAvatar' => 'https://jirapducc.mo.ca.am.ericsson.se/secure/useravatar?ownerId=%s',
'url.mhWebPraGoals' => 'https://mhweb.ericsson.se:443/SearchWeb/faces/search/query/resultPage.xhtml?&v=3&queryKey=74023&output=CSV&csvDelimiter=COMMA',
'url.mhWebTrProgress' => 'https://mhweb.ericsson.se:443/SearchWeb/faces/search/query/resultPage.xhtml?&v=3&queryKey=68655&output=CSV&csvDelimiter=COMMA',
'url.mhWebTrEdit' => 'https://mhweb.ericsson.se/TREditWeb/faces/tredit/tredit.xhtml?eriref=%s',

View File

@ -18,15 +18,20 @@ class AvatarAction implements ServerMiddlewareInterface
$this->avatarService = $avatarService;
}
/**
* @param ServerRequestInterface $request
* @param DelegateInterface $delegate
* @return \Psr\Http\Message\ResponseInterface|EmptyResponse|static
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$signum = $request->getAttribute('signum', false);
if(!$signum) {
return new EmptyResponse();
try {
$avatarImageData = $this->avatarService->getAvatarImageData($signum);
} catch (\UnexpectedValueException $e) {
return new TextResponse("Avatar not found", 404);
}
$avatarData = $this->avatarService->getAvatarData($signum);
return (new TextResponse($avatarData, 200, [
return (new TextResponse($avatarImageData, 200, [
'content-type' => 'image/png',
]))->withHeader('Expires', '0')
->withHeader('Cache-Control', 'must-revalidate');

View File

@ -11,95 +11,54 @@ use Zend\Http\Client;
class AvatarService
{
/**
* @var Config
*/
/** @var Config */
private $config;
/**
* @var Client
*/
/** @var Client */
private $httpClient;
/**
* @var RouterInterface
*/
private $router;
/**
* @var StorageInterface
*/
/** @var StorageInterface */
private $cache;
/**
* JiraClientService constructor.
* @param Client $client
* @param Config $config
* @param RouterInterface $router
* @param StorageInterface $cache
*/
public function __construct(Client $client, Config $config, RouterInterface $router, StorageInterface $cache)
public function __construct(Client $client, Config $config, StorageInterface $cache)
{
$this->httpClient = $client;
$this->config = $config;
$this->router = $router;
$this->cache = $cache;
}
/**
* @param string $jiraAvatarUrl
* @param string $signum
* @return string
*/
public function getJiraAvatarUrl(string $jiraAvatarUrl, string $signum): string
{
if (!$this->cache->hasItem($signum)) {
$user = $this->config->get('jira.user');
$password = $this->config->get('jira.password');
$response = $this->httpClient
->setAuth($user, $password)
->setUri($jiraAvatarUrl)
->send();
if (!$response->isSuccess()) {
throw new \UnexpectedValueException("Bad JIRA result", $response->getStatusCode());
}
$this->cache->setItem($signum, $response->getBody());
}
return $this->router->generateUri('user.avatar', [
'signum' => $signum,
]);
}
/**
* Returns avatar image data as string
*
* @param string $signum
* @return string
*/
public function getAvatarData(string $signum): string
public function getAvatarImageData(string $signum): string
{
if (!$this->cache->hasItem($signum)) {
throw new \UnexpectedValueException("Missing avatar", 404);
$user = $this->config->get('jira.user');
$password = $this->config->get('jira.password');
$jiraAvatarUrl = $this->config->get('url.jiraAvatar');
$response = $this->httpClient
->setAuth($user, $password)
->setUri(sprintf($jiraAvatarUrl, $signum))
->send();
if (!$response->isSuccess()) {
throw new \UnexpectedValueException("Missing avatar", 404);
}
$this->cache->setItem($signum, $response->getBody());
}
return $this->cache->getItem($signum);
}
public function getUserAvatarResponse(string $signum)
{
$localAvatarFile = "public/avatars/$signum";
if(file_exists($localAvatarFile)) {
$fp = fopen($localAvatarFile,"r+");
$response = new Response($fp);
} else {
$response = new TextResponse($this->getAvatarData($signum));
}
return $response
->withHeader('Content-type', 'image/png');
}
}

View File

@ -4,18 +4,16 @@ namespace App\Service;
use Interop\Container\ContainerInterface;
use Zend\Config\Config;
use Zend\Expressive\Router\RouterInterface;
use Zend\Http\Client;
class AvatarServiceFactory
{
public function __invoke(ContainerInterface $container)
{
$router = $container->get(RouterInterface::class);
$httpClient = $container->get(Client::class);
$cache = $container->get('service.cache');
$configArray = $container->get('config');
$config = new Config($configArray['app.config']);
return new AvatarService($httpClient, $config, $router, $cache);
return new AvatarService($httpClient, $config, $cache);
}
}

View File

@ -9,6 +9,7 @@ use App\Entity\KanbanBoard;
use App\Entity\KanbanEntry;
use Zend\Cache\Storage\StorageInterface;
use Zend\Config\Config;
use Zend\Expressive\Router\RouterInterface;
use Zend\Http\Client;
use Zend\Json\Decoder;
use Zend\Json\Json;
@ -18,36 +19,29 @@ class JiraCollectorService
const CACHE_KEY_KANBANBOARD = 'kanbanBoard';
/**
* @var StorageInterface
*/
/** @var StorageInterface */
private $cache;
/**
* @var Config
*/
/** @var Config */
private $config;
/**
* @var Client
*/
/** @var Client */
private $httpClient;
/**
* @var AvatarService
*/
private $avatarService;
/** @var RouterInterface */
private $router;
/**
* JiraClientService constructor.
* @param StorageInterface $cache
* @param Client $client
* @param Config $config
* @param AvatarService $avatarService
* @param RouterInterface $router
*/
public function __construct(StorageInterface $cache, Client $client, Config $config, AvatarService $avatarService)
public function __construct(StorageInterface $cache, Client $client, Config $config, RouterInterface $router)
{
$this->cache = $cache;
$this->avatarService = $avatarService;
$this->router = $router;
$this->httpClient = $client;
$this->config = $config;
}
@ -94,6 +88,7 @@ class JiraCollectorService
/**
* @param bool $forceReload
* @return KanbanBoard
* @throws \Zend\Cache\Exception\ExceptionInterface
*/
public function getKanbanBoard(bool $forceReload = false): KanbanBoard
{
@ -171,10 +166,9 @@ class JiraCollectorService
// additional assignees : customfield_10401
if (isset($jsonIssue['fields']['customfield_10401'])) {
foreach ($jsonIssue['fields']['customfield_10401'] as $assignee) {
$avatarUrl = $this->avatarService->getJiraAvatarUrl(
$assignee['avatarUrls']['48x48'],
$assignee['key']
);
$avatarUrl = $this->router->generateUri('user.avatar', [
'signum' => $assignee['key'],
]);
$jiraAssignee = new JiraAssignee();
$jiraAssignee->setName($assignee['displayName'])
@ -238,10 +232,9 @@ class JiraCollectorService
// assignee
if ($jsonIssue['fields']['assignee']) {
$avatarUrl = $this->avatarService->getJiraAvatarUrl(
$jsonIssue['fields']['assignee']['avatarUrls']['48x48'],
$jsonIssue['fields']['assignee']['key']
);
$avatarUrl = $this->router->generateUri('user.avatar', [
'signum' => $jsonIssue['fields']['assignee']['key'],
]);
$jiraAssignee = new JiraAssignee();
$jiraAssignee->setName($jsonIssue['fields']['assignee']['displayName'])

View File

@ -4,6 +4,7 @@ namespace App\Service;
use Interop\Container\ContainerInterface;
use Zend\Config\Config;
use Zend\Expressive\Router\RouterInterface;
use Zend\Http\Client;
class JiraCollectorServiceFactory
@ -14,7 +15,7 @@ class JiraCollectorServiceFactory
$configArray = $container->get('config');
$httpClient = $container->get(Client::class);
$config = new Config($configArray['app.config']);
$avatarService = $container->get(AvatarService::class);
return new JiraCollectorService($cache,$httpClient, $config, $avatarService);
$router = $container->get(RouterInterface::class);
return new JiraCollectorService($cache,$httpClient, $config, $router);
}
}