Passed
Pull Request — master (#5)
by Saulius
01:58
created

CacheableWidget::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
ccs 9
cts 9
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sauls\Component\Widget;
6
7
use Sauls\Component\OptionsResolver\OptionsResolver;
8
use Sauls\Component\Widget\Exception\NotAWidgetException;
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Contracts\Cache\CacheInterface;
11
use Symfony\Contracts\Cache\ItemInterface;
12
13
use function Sauls\Component\Helper\object_ucnp;
14
use function sprintf;
15
16
class CacheableWidget extends Widget implements Named
17
{
18
    public static $prefix = 'cw';
19
    public static $total = 0;
20
    private CacheInterface $cache;
21
22 3
    public function __construct(CacheInterface $cache)
23
    {
24 3
        $this->cache = $cache;
25 3
    }
26
27 2
    public function render(): string
28
    {
29 2
        $self = $this;
30 2
        return $this->cache->get(
31 2
            $this->createKey(),
32 2
            function (ItemInterface $item) use ($self) {
33 1
                $item->expiresAfter($self->getOption('ttl'));
34 1
                return (string)$self->getOption('widget');
35 2
            }
36 2
        ) ?? '';
37
    }
38
39 2
    private function createKey(): string
40
    {
41 2
        return sprintf(
42 2
            '__%1$s%2$s__%3$s__',
43 2
            $this->getOption('namespace') . self::$prefix,
44 2
            strtolower(object_ucnp($this->getOption('widget'))),
45 2
            $this->getId()
46
        );
47
    }
48
49 3
    protected function configureOptions(OptionsResolver $resolver): void
50
    {
51
        $resolver
52 3
            ->setDefined(
53
                [
54 3
                    'widget',
55
                    'namespace',
56
                    'ttl',
57
                ]
58
            )
59 3
            ->setRequired(['widget'])
60 3
            ->addAllowedTypes('widget', ['object'])
61 3
            ->addAllowedTypes('namespace', ['string'])
62 3
            ->addAllowedTypes('ttl', ['int'])
63 3
            ->setDefaults(
64
                [
65 3
                    'namespace' => '__widget__',
66
                    'ttl' => 3600,
67
                ]
68
            )
69 3
            ->setNormalizer(
70 3
                'widget',
71 3
                function (Options $options, $value) {
72 3
                    if (!$value instanceof WidgetInterface) {
73 1
                        throw new NotAWidgetException(
74 1
                            sprintf(
75 1
                                'Given object must implement %1$s interface',
76 1
                                WidgetInterface::class
77
                            )
78
                        );
79
                    }
80
81 2
                    return $value;
82 3
                }
83
            );
84 3
    }
85
86
    public function getName(): string
87
    {
88
        return 'cacheable_widget';
89
    }
90
}
91