Last active
January 2, 2016 18:39
-
-
Save anyt/8344824 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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