Completed
Push — master ( 493cb6...75e203 )
by WEBEWEB
02:04
created

QuoteTwigExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 105
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFunctions() 0 7 1
A getQuoteProvider() 0 12 3
A quoteAuthorFunction() 0 9 2
A quoteContentFunction() 0 9 2
A quoteFunction() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Twig\Extension;
13
14
use DateTime;
15
use Exception;
16
use Twig\Environment;
17
use Twig\TwigFunction;
18
use WBW\Bundle\CoreBundle\Manager\QuoteManager;
19
use WBW\Bundle\CoreBundle\Manager\QuoteManagerTrait;
20
use WBW\Bundle\CoreBundle\Provider\QuoteProviderInterface;
21
use WBW\Bundle\CoreBundle\Quote\QuoteInterface;
22
use WBW\Library\Core\Argument\ArrayHelper;
23
24
/**
25
 * Quote Twig extension.
26
 *
27
 * @author webeweb <https://github.com/webeweb/>
28
 * @package WBW\Bundle\CoreBundle\Twig\Extension
29
 */
30
class QuoteTwigExtension extends AbstractTwigExtension {
31
32
    use QuoteManagerTrait;
33
34
    /**
35
     * Service name.
36
     *
37
     * @var string
38
     */
39
    const SERVICE_NAME = "webeweb.core.twig.extension.quote";
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param Environment $twigEnvironment The Twig environment.
45
     * @param QuoteManager $quoteManager The quote manager.
46
     */
47
    public function __construct(Environment $twigEnvironment, QuoteManager $quoteManager) {
48
        parent::__construct($twigEnvironment);
49
        $this->setQuoteManager($quoteManager);
50
    }
51
52
    /**
53
     * Get the Twig functions.
54
     *
55
     * @return TwigFunction[] Returns the Twig functions.
56
     */
57
    public function getFunctions() {
58
        return [
59
            new TwigFunction("quote", [$this, "quoteFunction"], ["is_safe" => ["html"]]),
60
            new TwigFunction("quoteAuthor", [$this, "quoteAuthorFunction"], ["is_safe" => ["html"]]),
61
            new TwigFunction("quoteContent", [$this, "quoteContentFunction"], ["is_safe" => ["html"]]),
62
        ];
63
    }
64
65
    /**
66
     * Get the quote provider.
67
     *
68
     * @param string|null $domain The domain.
69
     * @return QuoteProviderInterface|null Returns the quote provider.
70
     */
71
    protected function getQuoteProvider($domain = null) {
72
73
        if (false === $this->getQuoteManager()->hasProviders()) {
74
            return null;
75
        }
76
77
        if (null !== $domain) {
78
            return $this->getQuoteManager()->getQuoteProvider($domain);
79
        }
80
81
        return $this->getQuoteManager()->getProviders()[0];
82
    }
83
84
    /**
85
     * Displays a quote author.
86
     *
87
     * @param string $domain The domain.
88
     * @return string Returns the quote author.
89
     * @throws Exception Throws an exception if an error occurs.
90
     */
91
    public function quoteAuthorFunction($domain = null) {
92
93
        $quote = $this->quoteFunction($domain);
94
        if (null === $quote) {
95
            return "";
96
        }
97
98
        return $quote->getAuthor();
99
    }
100
101
    /**
102
     * Displays a quote content.
103
     *
104
     * @param string $domain The domain.
105
     * @return string Returns the quote content.
106
     * @throws Exception Throws an exception if an error occurs.
107
     */
108
    public function quoteContentFunction($domain = null) {
109
110
        $quote = $this->quoteFunction($domain);
111
        if (null === $quote) {
112
            return "";
113
        }
114
115
        return $quote->getContent();
116
    }
117
118
    /**
119
     * Get the quote.
120
     *
121
     * @param string|null $domain The domain.
122
     * @return QuoteInterface|null Returns the quote.
123
     * @throws Exception Throws an exception if an error occurs.
124
     */
125
    public function quoteFunction($domain = null) {
126
127
        $provider = $this->getQuoteProvider($domain);
128
        if (null === $provider) {
129
            return null;
130
        }
131
132
        return ArrayHelper::get($provider->getQuotes(), (new DateTime())->format("m.d"), null);
133
    }
134
}
135