Completed
Push — 1.1 ( 1f9e88...3a0864 )
by Kamil
53s queued 38s
created

SyliusCollector   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 125
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getVersion() 0 4 1
A getExtensions() 0 4 1
A getCurrencyCode() 0 4 1
A getLocaleCode() 0 4 1
A getDefaultCurrencyCode() 0 4 1
A getDefaultLocaleCode() 0 4 1
B __construct() 0 26 3
A collect() 0 16 3
A reset() 0 6 1
A getName() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\CoreBundle\Collector;
15
16
use Sylius\Bundle\CoreBundle\Application\Kernel;
17
use Sylius\Component\Channel\Context\ChannelNotFoundException;
18
use Sylius\Component\Core\Context\ShopperContextInterface;
19
use Sylius\Component\Core\Model\ChannelInterface;
20
use Sylius\Component\Currency\Context\CurrencyNotFoundException;
21
use Sylius\Component\Locale\Context\LocaleNotFoundException;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
25
26
final class SyliusCollector extends DataCollector
27
{
28
    /**
29
     * @var ShopperContextInterface
30
     */
31
    private $shopperContext;
32
33
    /**
34
     * @param ShopperContextInterface $shopperContext
35
     * @param array $bundles
36
     * @param string $defaultLocaleCode
37
     */
38
    public function __construct(
39
        ShopperContextInterface $shopperContext,
40
        array $bundles,
41
        string $defaultLocaleCode
42
    ) {
43
        $this->shopperContext = $shopperContext;
44
45
        $this->data = [
46
            'version' => Kernel::VERSION,
47
            'base_currency_code' => null,
48
            'currency_code' => null,
49
            'default_locale_code' => $defaultLocaleCode,
50
            'locale_code' => null,
51
            'extensions' => [
52
                'SyliusAdminApiBundle' => ['name' => 'API', 'enabled' => false],
53
                'SyliusAdminBundle' => ['name' => 'Admin', 'enabled' => false],
54
                'SyliusShopBundle' => ['name' => 'Shop', 'enabled' => false],
55
            ],
56
        ];
57
58
        foreach (array_keys($this->data['extensions']) as $bundleName) {
59
            if (isset($bundles[$bundleName])) {
60
                $this->data['extensions'][$bundleName]['enabled'] = true;
61
            }
62
        }
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getVersion(): string
69
    {
70
        return $this->data['version'];
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function getExtensions(): array
77
    {
78
        return $this->data['extensions'];
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getCurrencyCode(): ?string
85
    {
86
        return $this->data['currency_code'];
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getLocaleCode(): ?string
93
    {
94
        return $this->data['locale_code'];
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getDefaultCurrencyCode(): ?string
101
    {
102
        return $this->data['base_currency_code'];
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getDefaultLocaleCode(): ?string
109
    {
110
        return $this->data['default_locale_code'];
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function collect(Request $request, Response $response, \Exception $exception = null): void
117
    {
118
        try {
119
            /** @var ChannelInterface $channel */
120
            $channel = $this->shopperContext->getChannel();
121
122
            $this->data['base_currency_code'] = $channel->getBaseCurrency()->getCode();
123
            $this->data['currency_code'] = $this->shopperContext->getCurrencyCode();
124
        } catch (ChannelNotFoundException | CurrencyNotFoundException $exception) {
125
        }
126
127
        try {
128
            $this->data['locale_code'] = $this->shopperContext->getLocaleCode();
129
        } catch (LocaleNotFoundException $exception) {
130
        }
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function reset(): void
137
    {
138
        $this->data['base_currency_code'] = null;
139
        $this->data['currency_code'] = null;
140
        $this->data['locale_code'] = null;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function getName(): string
147
    {
148
        return 'sylius_core';
149
    }
150
}
151