Passed
Push — master ( 3cfbcc...dd6e64 )
by Christian
16:35 queued 06:50
created

ConfigurationService   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 137
rs 10
c 0
b 0
f 0
ccs 37
cts 37
cp 1
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fetchConfiguration() 0 15 4
A __construct() 0 10 1
A checkConfiguration() 0 8 2
A getAppByName() 0 9 1
C getConfiguration() 0 51 13
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\System\SystemConfig\Service;
4
5
use Shopware\Core\Framework\App\AppEntity;
6
use Shopware\Core\Framework\App\Lifecycle\AbstractAppLoader;
7
use Shopware\Core\Framework\Context;
8
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
9
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
10
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
11
use Shopware\Core\Framework\Feature;
12
use Shopware\Core\System\SystemConfig\Exception\BundleConfigNotFoundException;
13
use Shopware\Core\System\SystemConfig\Exception\ConfigurationNotFoundException;
14
use Shopware\Core\System\SystemConfig\Util\ConfigReader;
15
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
16
17
class ConfigurationService
18
{
19
    /**
20
     * @var array
21
     */
22
    private $bundles;
23
24
    /**
25
     * @var ConfigReader
26
     */
27
    private $configReader;
28
29
    /**
30
     * @var AbstractAppLoader
31
     */
32
    private $appLoader;
33 4
34
    /**
35 4
     * @var EntityRepositoryInterface
36 4
     */
37 4
    private $appRepository;
38 4
39
    /**
40
     * @param BundleInterface[] $bundles
41
     */
42
    public function __construct(
43 3
        iterable $bundles,
44
        ConfigReader $configReader,
45 3
        AbstractAppLoader $appLoader,
46
        EntityRepositoryInterface $appRepository
47 3
    ) {
48 1
        $this->bundles = $bundles;
0 ignored issues
show
Documentation Bug introduced by
It seems like $bundles of type iterable is incompatible with the declared type array of property $bundles.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
        $this->configReader = $configReader;
50
        $this->appLoader = $appLoader;
51 2
        $this->appRepository = $appRepository;
52
    }
53 1
54
    /**
55
     * @throws ConfigurationNotFoundException
56 2
     * @throws \InvalidArgumentException
57
     * @throws BundleConfigNotFoundException
58 2
     *
59
     * @deprecated tag:v6.4.0 $context param will be required
60 2
     */
61
    public function getConfiguration(string $domain, ?Context $context = null): array
62 2
    {
63 1
        if (!$context) {
64
            $context = Context::createDefaultContext();
65
        }
66 2
67 1
        $validDomain = preg_match('/^([\w-]+)\.?([\w-]*)$/', $domain, $match);
68
69
        if (!$validDomain) {
70 1
            throw new \InvalidArgumentException('Expected domain');
71 1
        }
72 1
73 1
        $scope = $match[1];
74
        $configName = $match[2] !== '' ? $match[2] : null;
75
76 1
        $config = $this->fetchConfiguration($scope === 'core' ? 'System' : $scope, $configName, $context);
77
        if (!$config) {
78 1
            throw new ConfigurationNotFoundException($scope);
79
        }
80 1
81
        $domain = rtrim($domain, '.') . '.';
82
83 2
        foreach ($config as $i => $card) {
84
            foreach ($card['elements'] as $j => $field) {
85 2
                $newField = ['name' => $domain . $field['name']];
86
87 2
                if (\array_key_exists('flag', $field)) {
88 2
                    try {
89 2
                        if (!Feature::isActive($field['flag'])) {
90
                            continue;
91
                        }
92
                    } catch (\RuntimeException $e) {
93 2
                        continue;
94
                    }
95 2
                }
96
97
                if (array_key_exists('type', $field)) {
98 3
                    $newField['type'] = $field['type'];
99
                }
100 3
101 3
                unset($field['type'], $field['name']);
102 2
                if ($field === []) {
103
                    $field = new \stdClass();
104
                }
105
                $newField['config'] = $field;
106 1
                $card['elements'][$j] = $newField;
107
            }
108
            $config[$i] = $card;
109
        }
110
111
        return $config;
112
    }
113
114
    /**
115
     * @deprecated tag:v6.4.0 $context param will be required
116
     */
117
    public function checkConfiguration(string $domain, ?Context $context = null): bool
118
    {
119
        try {
120
            $this->getConfiguration($domain, $context);
0 ignored issues
show
Deprecated Code introduced by
The function Shopware\Core\System\Sys...ice::getConfiguration() has been deprecated: tag:v6.4.0 $context param will be required ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

120
            /** @scrutinizer ignore-deprecated */ $this->getConfiguration($domain, $context);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
121
122
            return true;
123
        } catch (\InvalidArgumentException | ConfigurationNotFoundException | BundleConfigNotFoundException $e) {
124
            return false;
125
        }
126
    }
127
128
    private function fetchConfiguration(string $scope, ?string $configName, Context $context): ?array
129
    {
130
        $technicalName = array_slice(explode('\\', $scope), -1)[0];
131
        foreach ($this->bundles as $bundle) {
132
            if ($bundle->getName() === $technicalName) {
133
                return $this->configReader->getConfigFromBundle($bundle, $configName);
134
            }
135
        }
136
137
        $app = $this->getAppByName($technicalName, $context);
138
        if ($app) {
139
            return $this->appLoader->getConfiguration($app);
0 ignored issues
show
Deprecated Code introduced by
The function Shopware\Core\Framework\...der::getConfiguration() has been deprecated: tag:v6.4.0 will be made abstract and extending classes will need to provide an implementation ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

139
            return /** @scrutinizer ignore-deprecated */ $this->appLoader->getConfiguration($app);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
140
        }
141
142
        return null;
143
    }
144
145
    private function getAppByName(string $name, Context $context): ?AppEntity
146
    {
147
        $criteria = new Criteria();
148
        $criteria->addFilter(new EqualsFilter('name', $name));
149
150
        /** @var AppEntity|null $result */
151
        $result = $this->appRepository->search($criteria, $context)->first();
152
153
        return $result;
154
    }
155
}
156