BlogExtension::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Acelaya\Website\Feed\Template\Extension;
5
6
use Acelaya\Website\Feed\BlogOptions;
7
use Doctrine\Common\Cache\Cache;
8
use League\Plates\Engine;
9
use League\Plates\Extension\ExtensionInterface;
10
11
class BlogExtension implements ExtensionInterface
12
{
13
    /**
14
     * @var Cache
15
     */
16
    private $cache;
17
    /**
18
     * @var BlogOptions
19
     */
20
    private $blogOptions;
21
22
    /**
23
     * BlogExtension constructor.
24
     * @param Cache $cache
25
     * @param BlogOptions $blogOptions
26
     */
27
    public function __construct(Cache $cache, BlogOptions $blogOptions)
28
    {
29
        $this->cache = $cache;
30
        $this->blogOptions = $blogOptions;
31
    }
32
33
    public function register(Engine $engine)
34
    {
35
        $engine->registerFunction('render_latest_blog_posts', [$this, 'renderLatestBlogPosts']);
36
    }
37
38
    public function renderLatestBlogPosts()
39
    {
40
        $cacheId = $this->blogOptions->getCacheKey();
41
        $posts = $this->cache->contains($cacheId) ? $this->cache->fetch($cacheId) : [];
42
        $elements = [];
43
44
        foreach ($posts as $post) {
45
            $elements[] = sprintf('<li><a target="_blank" href="%s">%s</a></li>', $post['link'], $post['title']);
46
        }
47
48
        return sprintf('<ul class="fh5co-links blog-posts">%s</ul>', implode('', $elements));
49
    }
50
}
51