Skip to content

Commit

Permalink
implemented getThemePath, added specs, fixed code in CampTemplate class
Browse files Browse the repository at this point in the history
  • Loading branch information
takeit committed Nov 3, 2014
1 parent 7c3183f commit 9f6fe85
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 31 deletions.
32 changes: 27 additions & 5 deletions newscoop/library/Newscoop/Services/IssueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,21 @@ class IssueService implements IssueServiceInterface
*/
protected $issue;

/**
* Cache Service
*
* @var CacheService
*/
protected $cacheService;

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

/**
Expand Down Expand Up @@ -88,12 +96,26 @@ public function issueResolver(Request $request)
{
$uriParts = explode('/', $request->getRequestUri());
$uriPartsCount = count(array_filter($uriParts));
$issue = null;
if ($uriPartsCount >= 2 && $uriPartsCount <= 5) {
$publication = $this->publicationService->getPublication();
$issue = $this->em->getRepository('Newscoop\Entity\Issue')->findOneBy(array(
'publication' => $publication,
'shortName' => $uriParts[2]
));
$cacheKey = $this->cacheService->getCacheKey(array(
'resolver',
$publication->getId(),
$uriParts[1],
$uriParts[2]
), 'issue');

if ($this->cacheService->contains($cacheKey)) {
$issue = $this->cacheService->fetch($cacheKey);
} else {
$issue = $this->em->getRepository('Newscoop\Entity\Issue')->findOneBy(array(
'publication' => $publication,
'shortName' => $uriParts[2]
));

$this->cacheService->save($cacheKey, $issue);
}

if ($issue) {
$this->issueMetadata = array(
Expand Down
114 changes: 101 additions & 13 deletions newscoop/library/Newscoop/Services/ThemesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

use Newscoop\ThemesServiceInterface;
use Newscoop\IssueServiceInterface;
use Newscoop\Entity\Issue;
use Doctrine\ORM\EntityManager;
use Newscoop\Entity\Output;

/**
* Themes service
Expand Down Expand Up @@ -37,40 +40,125 @@ class ThemesService implements ThemesServiceInterface
*/
protected $publicationService;

/**
* Entity Manager
*
* @var EntityManager
*/
protected $em;

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

/**
* {@inheritDoc}
*/
public function getThemePath()
{
$issue = $this->issueService->getIssue();
$language = $issue->getLanguageId();
$publication = $this->publicationService->getPublication();
$cacheKeyThemePath = $this->cacheService->getCacheKey(array('getThemePath', $language, $publication->getId(), $issue->getNumber()), 'issue');
$issue = $this->issueService->getIssue();
$languageId = $issue->getLanguageId();
$publication = $this->publicationService->getPublication();
$cacheKeyThemePath = $this->cacheService->getCacheKey(array('getThemePath', $languageId, $publication->getId(), $issue->getNumber()), 'issue');

if ($this->cacheService->contains($cacheKeyThemePath)) {
$themePath = null;
$webOutput = null;
$outSetIssues = null;
if ($this->cacheService->contains($cacheKeyThemePath)) {
$themePath = $this->cacheService->fetch($cacheKeyThemePath);
} else {
$cacheKey = $this->cacheService->getCacheKey(array('issue', $publication->getId(), $language, $issue->getNumber()), 'issue');
if ($this->cacheService->contains($cacheKey)) {
$issue = $this->cacheService->fetch($cacheKey);
$cacheKeyWebOutput = $this->cacheService->getCacheKey(array('OutputService', 'Web'), 'outputservice');
if ($this->cacheService->contains($cacheKeyWebOutput)) {
$webOutput = $this->cacheService->fetch($cacheKeyWebOutput);
} else {
$this->cacheService->save($cacheKey, $issue);
$webOutput = $this->findByName('Web');
$this->cacheService->save($cacheKeyWebOutput, $webOutput);
}

$cacheKeyOutSetIssues = $this->cacheService->getCacheKey(array('outSetIssues', $issue->getId(), 'webOutput'));
if ($this->cacheService->contains($cacheKeyOutSetIssues)) {
$outSetIssues = $this->cacheService->fetch($cacheKeyOutSetIssues);
} else {
$outSetIssues = $this->findByIssueAndOutput($issue->getId(), $webOutput);
$this->cacheService->save($cacheKeyOutSetIssues, $outSetIssues);
}

if (!is_null($outSetIssues)) {
$themePath = $outSetIssues->getThemePath()->getPath();
}

//$resourceId = new ResourceId('template_engine/classes/CampSystem');
//$outputService = $resourceId->getService(IOutputService::NAME);
$this->cacheService->save($cacheKeyThemePath, $themePath);

}

return $themePath;
}

/**
* Finds output by name
*
* @param string $name Output name ('Web' in this case)
*
* @return string|null
* @throws Exception when wrong parameter supplied
*/
public function findByName($name)
{
if (is_null($name)) {
throw new \Exception("Please provide a value for the parameter 'name'");
} elseif (is_string($name) && trim($name) == '') {
throw new \Exception("Please provide a none empty value for the parameter 'name'.");
}

$outputs = $this->em->getRepository('Newscoop\Entity\Output')->findBy(array('name' => $name));
if (isset($outputs) && count($outputs) > 0) {
return $outputs[0];
}

return null;
}

/**
* Finds output for issue by issue and output
*
* @param Issue $issue Issue object
* @param Output $output Output object
*
* @return string|null
*/
public function findByIssueAndOutput($issue, $output)
{
$outputId = $output;
if ($output instanceof Output) {
$outputId = $output->getId();
}

$issueId = $issue;
if ($issue instanceof Issue) {
$issueId = $issue->getId();
}

$resources = $this->em->getRepository('Newscoop\Entity\Output\OutputSettingsIssue')->findBy(array(
'issue' => $issueId,
'output' => $outputId
));

if (!empty($resources)) {
return $resources[0];
}

return '';
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ services:

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

newscoop_newscoop.themes_service:
class: Newscoop\Services\ThemesService
arguments: ["@newscoop_newscoop.issue_service", "@newscoop.cache"]
arguments: ["@newscoop_newscoop.issue_service", "@newscoop.cache", "@newscoop_newscoop.publication_service", "@em"]

newscoop_newscoop.listener.publication:
class: Newscoop\NewscoopBundle\EventListener\PublicationListener
Expand Down
6 changes: 3 additions & 3 deletions newscoop/template_engine/classes/CampTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ private function getTemplateTranslationsFiles()
$request = \Zend_Registry::get('container')->getService('request');
$cacheService = \Zend_Registry::get('container')->getService('newscoop.cache');
$translator = \Zend_Registry::get('container')->getService('translator');

$themesService = \Zend_Registry::get('container')->getService('newscoop_newscoop.themes_service');
$locale = $request->getLocale();

$cacheKey = $cacheService->getCacheKey(array('templates_translations', 'publication_1/theme_1', $locale), 'templates_translations');
$cacheKey = $cacheService->getCacheKey(array('templates_translations', $themesService->getThemePath(), $locale), 'templates_translations');
$templateTranslations = array();
if ($cacheService->contains($cacheKey)) {
$templateTranslations = $cacheService->fetch($cacheKey);
Expand All @@ -126,7 +126,7 @@ private function getTemplateTranslationsFiles()
}

$filesystem = new Filesystem();
$dir = __DIR__.'/../../themes/publication_1/theme_1/translations';
$dir = __DIR__.'/../../themes/' . $themesService->getThemePath() . 'translations';
if ($filesystem->exists($dir)) {
$finder = new Finder();
$translator->addLoader('yaml', new YamlFileLoader());
Expand Down
11 changes: 9 additions & 2 deletions spec/Newscoop/Services/IssueServiceSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\HttpFoundation\Request;
use Newscoop\Entity\Issue;
use Newscoop\Services\IssueService;
use Newscoop\Services\CacheService;
use Newscoop\Services\PublicationService;
use Doctrine\ORM\EntityManager;
use Newscoop\Entity\Repository\IssueRepository;
Expand All @@ -21,7 +22,13 @@ public function it_is_initializable()
$this->shouldImplement('Newscoop\IssueServiceInterface');
}

public function let(EntityManager $em, PublicationService $publicationService, IssueRepository $repository, Issue $issue, Publication $publication)
public function let(
EntityManager $em,
PublicationService $publicationService,
IssueRepository $repository,
Issue $issue,
Publication $publication,
CacheService $cacheService)
{
$em
->getRepository('Newscoop\Entity\Issue')
Expand All @@ -34,7 +41,7 @@ public function let(EntityManager $em, PublicationService $publicationService, I
'shortName' => 'may2014'
))->willReturn($issue);

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

public function it_resolves_issue_from_request_data(Request $request, Issue $issue, ParameterBag $attributes)
Expand Down
55 changes: 49 additions & 6 deletions spec/Newscoop/Services/ThemesServiceSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
use Newscoop\Services\PublicationService;
use Newscoop\Entity\Issue;
use Newscoop\Entity\Publication;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Newscoop\Entity\Output;
use Newscoop\Entity\Output\OutputSettingsIssue;
use Prophecy\Argument;
use Newscoop\Entity\Resource;

class ThemesServiceSpec extends ObjectBehavior
{
Expand All @@ -22,20 +29,56 @@ public function let(
Issue $issue,
CacheService $cacheService,
PublicationService $publicationService,
Publication $publication)
Publication $publication,
EntityManager $em,
Registry $doctrine,
EntityRepository $repository,
Output $output,
OutputSettingsIssue $issueOutput
)
{
$issueService->getIssue()->willReturn($issue);
$publicationService->getPublication()->willReturn($publication);
$this->beConstructedWith($issueService, $cacheService, $publicationService);
}
$doctrine->getManager()->willReturn($em);

$em->getRepository(Argument::exact('Newscoop\Entity\Output'))->willReturn($repository);
$repository->findBy(array('name' => 'Web'))->willReturn(array($output));

$em->getRepository(Argument::exact('Newscoop\Entity\Output\OutputSettingsIssue'))->willReturn($repository);
$repository->findBy(array('issue' => 1, 'output' => 1))->willReturn(array($issueOutput));

public function it_gets_theme_path(Issue $issue)
{
$issue->getId()->willReturn(1);
$issue->getNumber()->willReturn(10);
$issue->getName()->willReturn("May 2014");
$issue->getShortName()->willReturn("may2014");
$issue->getLanguageId()->willReturn(1);
$this->getThemePath()->shouldBeString();
$output->getId()->willReturn(1);
$output->getName()->willReturn('Web');

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

public function it_gets_weboutput_by_name(Output $output)
{
$this->findByName('Web')->shouldReturn($output);
}

public function it_finds_by_issue_and_output(Issue $issue, Output $output, OutputSettingsIssue $issueOutput)
{
$this->findByIssueAndOutput($issue, $output)->shouldReturn($issueOutput);
}

public function it_should_throw_exception_when_name_is_empty()
{
$this
->shouldThrow('Exception')
->during('findByName', array(null));
}

public function it_gets_theme_path(OutputSettingsIssue $issueOutput, Resource $resource)
{
$resource->getPath()->willReturn('publication_1/theme_1/');
$issueOutput->getThemePath()->willReturn($resource);
$this->getThemePath()->shouldReturn('publication_1/theme_1/');
}
}

0 comments on commit 9f6fe85

Please sign in to comment.