Skip to content

Instantly share code, notes, and snippets.

@anyt
Last active January 2, 2016 18:39
Show Gist options
  • Save anyt/8344824 to your computer and use it in GitHub Desktop.
Save anyt/8344824 to your computer and use it in GitHub Desktop.
<?php
namespace Anyt\BlogBundle\Twig;
class MaxTaggedPostsCountExtension extends \Twig_Extension
{
public function getFunctions()
{
return array(
new Twig_SimpleFunction('getTagWeight', 'getTagWeight'),
);
}
/**
* maxWeight equals to available fonts sizes count
*/
private $maxWeight;
/**
* entity manager
*/
private $em;
/**
* doctrine 2 cacheDriver
* http://docs.doctrine-project.org/en/latest/reference/caching.html
*/
private $cacheDriver;
function getMaxTaggedPostsCount() {
if ($maxTaggedPostsCount = $cacheDriver->fetch('max_tagged_posts_count')) {
return $maxTaggedPostsCount;
}
$return $em->getRepository('DemoBundle:Tag')->getMaxTaggedPostsCount();
// getMaxTaggedPostsCount() contains DQL "SELECT MAX(t.taggedPostsCount) from AnytBlogBundle:Tag as t"
}
function getTagWeight(Tag $tag) {
return ceil( $tag->getTaggedPostsCount() * $this->maxWeight / $this->getMaxTaggedPostsCount() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment