AppExtension::showConversation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 5
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PiedWeb\ConversationBundle\Twig;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Twig\Environment as Twig;
8
use Twig\Extension\AbstractExtension;
9
use Twig\TwigFunction;
10
11
class AppExtension extends AbstractExtension
12
{
13
    /** @var EntityManagerInterface */
14
    private $em;
15
16
    /** @var string */
17
    private $messageEntity;
18
19
    public function __construct(EntityManager $em, string $messageEntity)
20
    {
21
        $this->em = $em;
22
        $this->messageEntity = $messageEntity;
23
    }
24
25
    public function getFunctions()
26
    {
27
        return [
28
            new TwigFunction('showConversation', [$this, 'showConversation'], [
29
                'is_safe' => ['html'],
30
                'needs_environment' => true,
31
            ]),
32
        ];
33
    }
34
35
    public function showConversation(
36
        Twig $env,
37
        string $referring,
38
        string $orderBy = 'createdAt DESC',
39
        $limit = 0,
40
        string $template = '@PiedWebConversation/_messages_list.html.twig'
41
    ) {
42
        $messages = $this->em->getRepository($this->messageEntity)
43
            ->getMessagesPublishedByReferring($referring, $orderBy, $limit);
44
45
        return $env->render($template, ['messages' => $messages]);
46
    }
47
}
48