Issues (44)

Twig/CartExtension.php (4 issues)

1
<?php
2
3
namespace GGGGino\SkuskuCartBundle\Twig;
4
5
use GGGGino\SkuskuCartBundle\Model\SkuskuCart;
6
use GGGGino\SkuskuCartBundle\Model\SkuskuCurrencyInterface;
7
use GGGGino\SkuskuCartBundle\Model\SkuskuCustomerInterface;
8
use GGGGino\SkuskuCartBundle\Model\SkuskuLangInterface;
9
use GGGGino\SkuskuCartBundle\Service\CartManager;
10
use GGGGino\SkuskuCartBundle\Service\CurrencyManager;
11
use GGGGino\SkuskuCartBundle\Service\LangManager;
12
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
13
use Twig\Environment;
14
use Twig\Extension\AbstractExtension;
15
use Twig\TwigFunction;
16
use Symfony\Bundle\TwigBundle\TwigEngine;
17
use Doctrine\ORM\EntityManager;
18
19
class CartExtension extends AbstractExtension
20
{
21
    /**
22
     * @var Environment
23
     */
24
    private $templating;
25
26
    /**
27
     * @var CartManager
28
     */
29
    private $cartManager;
30
31
    /**
32
     * @var CurrencyManager
33
     */
34
    private $currencyManager;
35
36
    /**
37
     * @var LangManager
38
     */
39
    private $langManager;
40
41
    /**
42
     * @var string
43
     */
44
    private $templatePreviewCart = "GGGGinoSkuskuCartBundle:Cart:cart_preview.html.twig";
45
46
    /**
47
     * @var string
48
     */
49
    private $templateCurrencyCart = "GGGGinoSkuskuCartBundle:Cart:cart_currency_preview.html.twig";
50
51
    /**
52
     * @var string
53
     */
54
    private $templateLangCart = "GGGGinoSkuskuCartBundle:Cart:cart_lang_preview.html.twig";
55
56
    /**
57
     * CartExtension constructor.
58
     * @param Environment $templating
59
     * @param CartManager $cartManager
60
     * @param CurrencyManager $currencyManager
61
     * @param LangManager $langManager
62
     */
63
    public function __construct(
64
        Environment $templating,
65
        CartManager $cartManager,
66
        CurrencyManager $currencyManager,
67
        LangManager $langManager )
68
    {
69
        $this->templating = $templating;
70
        $this->cartManager = $cartManager;
71
        $this->currencyManager = $currencyManager;
72
        $this->langManager = $langManager;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function getFunctions()
79
    {
80
        return array(
81
            new TwigFunction('render_preview_cart', array($this, 'renderPreviewCart'), array(
82
                'is_safe' => array('html')
83
            )),
84
            new TwigFunction('render_currency_cart', array($this, 'renderCurrencyCart'), array(
85
                'is_safe' => array('html')
86
            )),
87
            new TwigFunction('render_lang_cart', array($this, 'renderLangCart'), array(
88
                'is_safe' => array('html')
89
            ))
90
        );
91
    }
92
93
    /**
94
     * Render the cart preview, with a little summary
95
     *
96
     * @param null $template
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $template is correct as it would always require null to be passed?
Loading history...
97
     * @return string
98
     */
99
    public function renderPreviewCart($template = null)
100
    {
101
        if (!$template) {
0 ignored issues
show
$template is of type null, thus it always evaluated to false.
Loading history...
102
            $template = $this->templatePreviewCart;
103
        }
104
105
        /** @var SkuskuCart $cart */
106
        $cart = $this->cartManager->getCartFromCustomer();
107
108
        return $this->templating->render($template, array(
109
            'cart' => $cart,
110
        ));
111
    }
112
113
    /**
114
     * Render the currency list
115
     *
116
     * @param string $template
117
     * @return string
118
     */
119
    public function renderCurrencyCart($template = null)
120
    {
121
        if (!$template) {
122
            $template = $this->templateCurrencyCart;
123
        }
124
125
        /** @var SkuskuCurrencyInterface[] $currencies */
126
        $currencies = $this->currencyManager->getActiveCurrencies();
127
        /** @var SkuskuCurrencyInterface $currentCurrency */
128
        $currentCurrency = $this->currencyManager->getCurrentCurrency();
129
130
        return $this->templating->render($template, array(
131
            'currencies' => $currencies,
132
            'currentCurrency' => $currentCurrency
133
        ));
134
    }
135
136
    /**
137
     * @param null $template
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $template is correct as it would always require null to be passed?
Loading history...
138
     * @return string
139
     */
140
    public function renderLangCart($template = null)
141
    {
142
        if (!$template) {
0 ignored issues
show
$template is of type null, thus it always evaluated to false.
Loading history...
143
            $template = $this->templateLangCart;
144
        }
145
146
        /** @var SkuskuLangInterface[] $languages */
147
        $languages = $this->langManager->getActiveLanguages();
148
149
        /** @var SkuskuLangInterface $currentLanguage */
150
        $currentLanguage = $this->langManager->getCurrentLanguage();
151
152
        return $this->templating->render($template, array(
153
            'languages' => $languages,
154
            'currentLanguage' => $currentLanguage
155
        ));
156
    }
157
}