Skip to content

Commit

Permalink
CS-5549 - A new way to get theme path - speed improvement (issue serv…
Browse files Browse the repository at this point in the history
…ice, listener, specs)
  • Loading branch information
takeit committed Oct 30, 2014
1 parent 0b653e1 commit 54884b9
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 0 deletions.
33 changes: 33 additions & 0 deletions newscoop/library/Newscoop/IssueServiceInterface.php
@@ -0,0 +1,33 @@
<?php
/**
* @package Newscoop
* @copyright 2014 Sourcefabric z.ú.
* @author Rafał Muszyński <rafal.muszynski@sourcefabric.org>
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

namespace Newscoop;

use Symfony\Component\HttpFoundation\Request;

/**
* Issue service interface
*/
interface IssueServiceInterface
{
/**
* Issue resolver
*
* @param Request $request Request
*
* @return Issue|void Returns current issue or nothing
*/
public function issueResolver(Request $request);

/**
* Get issue meta data
*
* @return array Issue meta data
*/
public function getIssueMetadata();
}
83 changes: 83 additions & 0 deletions newscoop/library/Newscoop/Services/IssueService.php
@@ -0,0 +1,83 @@
<?php
/**
* @package Newscoop
* @copyright 2014 Sourcefabric z.ú.
* @author Rafał Muszyński <rafal.muszynski@sourcefabric.org>
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

namespace Newscoop\Services;

use Newscoop\IssueServiceInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;

/**
* Issue service
*/
class IssueService implements IssueServiceInterface
{
/**
* Entity Manager
*
* @var EntityManager
*/
protected $em;

/**
* Issue meta data array
*
* @var array
*/
protected $issueMetadata = array();

/**
* Publication service
*
* @var PublicationService
*/
protected $publicationService;

/**
* Construct
*/
public function __construct(EntityManager $em, PublicationService $publicationService)
{
$this->em = $em;
$this->publicationService = $publicationService;
}

/**
* {@inheritDoc}
*/
public function getIssueMetadata()
{
return $this->issueMetadata;
}

/**
* {@inheritDoc}
*/
public function issueResolver(Request $request)
{
$uriParts = explode('/', $request->getRequestUri());
$publication = $this->publicationService->getPublication();
$issue = $this->em->getRepository('Newscoop\Entity\Issue')->findOneBy(array(
'publication' => $publication,
'shortName' => $uriParts[2]
));

if ($issue) {
$this->issueMetadata = array(
'id' => $issue->getId(),
'number' => $issue->getNumber(),
'name' => $issue->getName(),
'shortName' => $issue->getShortName()
);

$request->attributes->set('_newscoop_issue_metadata', $this->issueMetadata);

return $issue;
}
}
}
32 changes: 32 additions & 0 deletions newscoop/library/Newscoop/Services/ThemesService.php
@@ -0,0 +1,32 @@
<?php
/**
* @package Newscoop
* @copyright 2014 Sourcefabric z.ú.
* @author Rafał Muszyński <rafal.muszynski@sourcefabric.org>
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

namespace Newscoop\Services;

use Newscoop\ThemesServiceInterface;

/**
* Themes service
*/
class ThemesService implements ThemesServiceInterface
{
/**
* Construct
*/
public function __construct()
{
}

/**
* {@inheritDoc}
*/
public function getThemePath()
{

}
}
22 changes: 22 additions & 0 deletions newscoop/library/Newscoop/ThemesServiceInterface.php
@@ -0,0 +1,22 @@
<?php
/**
* @package Newscoop
* @copyright 2014 Sourcefabric z.ú.
* @author Rafał Muszyński <rafal.muszynski@sourcefabric.org>
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

namespace Newscoop;

/**
* Themes service interface
*/
interface ThemesServiceInterface
{
/**
* Gets current theme path
*
* @return string Returns current theme path
*/
public function getThemePath();
}
@@ -0,0 +1,49 @@
<?php
/**
* @package Newscoop\Newscoop
* @author Rafał Muszyński <rafal.muszynski@sourcefabric.org>
* @copyright 2014 Sourcefabric z.ú.
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

namespace Newscoop\NewscoopBundle\EventListener;

use Newscoop\Services\IssueService;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

/**
* Issue listener to identify current issue
*/
class IssueListener
{
/**
* Issue service
* @var ThemesService
*/
protected $issueService;

/**
* Contruct
*
* @param IssueService $issueService Issue service
*/
public function __construct(IssueService $issueService)
{
$this->issueService = $issueService;
}

/**
* Resolve issue on request
*
* @param GetResponseEvent $event GetResponseEvent event
*
* @return void
*/
public function onRequest(GetResponseEvent $event)
{
$pos = strpos($event->getRequest()->getRequestUri(), 'admin');
if ($pos === false) {
$this->issueService->issueResolver($event->getRequest());
}
}
}
10 changes: 10 additions & 0 deletions newscoop/src/Newscoop/NewscoopBundle/Resources/config/services.yml
Expand Up @@ -135,6 +135,10 @@ services:
class: Newscoop\Services\PublicationService
arguments: ["@em"]

newscoop_newscoop.issue_service:
class: Newscoop\Services\IssueService
arguments: ["@em", "@newscoop_newscoop.publication_service"]

newscoop_newscoop.listener.publication:
class: Newscoop\NewscoopBundle\EventListener\PublicationListener
arguments: ["@newscoop_newscoop.publication_service"]
Expand Down Expand Up @@ -163,6 +167,12 @@ services:
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

newscoop_newscoop.listener.issue:
class: Newscoop\NewscoopBundle\EventListener\IssueListener
arguments: ["@newscoop_newscoop.issue_service"]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onRequest }

parameters:
smarty.extension.routing.class: "Newscoop\\NewscoopBundle\\Extension\\RoutingExtension"
smarty.extension.assetic.dynamic.class: "Newscoop\\NewscoopBundle\\Extension\\DynamicAsseticExtension"
Expand Down
27 changes: 27 additions & 0 deletions spec/Newscoop/NewscoopBundle/EventListener/IssueListenerSpec.php
@@ -0,0 +1,27 @@
<?php

namespace spec\Newscoop\NewscoopBundle\EventListener;

use PhpSpec\ObjectBehavior;
use Newscoop\Services\IssueService;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Request;

class IssueListenerSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType('Newscoop\NewscoopBundle\EventListener\IssueListener');
}

public function let(IssueService $issueService, Request $request)
{
$this->beConstructedWith($issueService);
}

public function it_calls_issue_resolver_on_request(GetResponseEvent $event, Request $request)
{
$event->getRequest()->willReturn($request);
$this->onRequest($event)->shouldReturn(null);
}
}
61 changes: 61 additions & 0 deletions spec/Newscoop/Services/IssueServiceSpec.php
@@ -0,0 +1,61 @@
<?php

namespace spec\Newscoop\Services;

use PhpSpec\ObjectBehavior;
use Symfony\Component\HttpFoundation\Request;
use Newscoop\Entity\Issue;
use Newscoop\Services\IssueService;
use Newscoop\Services\PublicationService;
use Doctrine\ORM\EntityManager;
use Newscoop\Entity\Repository\IssueRepository;
use Newscoop\Entity\Publication;
use Symfony\Component\HttpFoundation\ParameterBag;

class IssueServiceSpec extends ObjectBehavior
{
public function it_is_initializable()
{
$this->shouldHaveType('Newscoop\Services\IssueService');
$this->shouldImplement('Newscoop\IssueServiceInterface');
}

public function let(EntityManager $em, PublicationService $publicationService, IssueRepository $repository, Issue $issue, Publication $publication)
{
$em
->getRepository('Newscoop\Entity\Issue')
->willReturn($repository);

$publicationService->getPublication()->willReturn($publication);

$repository->findOneBy(array(
'publication' => $publication,
'shortName' => 'may2014'
))->willReturn($issue);

$this->beConstructedWith($em, $publicationService);
}

public function it_resolves_issue_from_request_data(Request $request, Issue $issue, ParameterBag $attributes)
{
$issue->getId()->willReturn(1);
$issue->getNumber()->willReturn(10);
$issue->getName()->willReturn("May 2014");
$issue->getShortName()->willReturn("may2014");
$request->getRequestUri()->willReturn('/en/may2014/60/test-article.htm');
$request->attributes = $attributes;
$request->attributes->set('_newscoop_issue_metadata', array(
'id' => $issue->getId(),
'number' => $issue->getNumber(),
'name' => $issue->getName(),
'shortName' => $issue->getShortName()
));

$this->issueResolver($request)->shouldReturn($issue);
}

public function it_gets_current_issue_meta_data(ParameterBag $attributes, Issue $issue)
{
$this->getIssueMetadata()->shouldReturn(array());
}
}
14 changes: 14 additions & 0 deletions spec/Newscoop/Services/ThemesServiceSpec.php
@@ -0,0 +1,14 @@
<?php

namespace spec\Newscoop\Services;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ThemesServiceSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Newscoop\Services\ThemesService');
}
}

0 comments on commit 54884b9

Please sign in to comment.