Passed
Push — trunk ( c56b80...32ae88 )
by Christian
11:56 queued 12s
created

SystemConfigChangedHook   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 63
rs 10
c 1
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getWebhookPayload() 0 18 4
A getName() 0 3 1
A __construct() 0 4 1
A isAllowed() 0 21 5
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\System\SystemConfig\Event;
4
5
use Shopware\Core\Framework\App\AppEntity;
0 ignored issues
show
Bug introduced by
The type Shopware\Core\Framework\App\AppEntity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Shopware\Core\Framework\Log\Package;
7
use Shopware\Core\Framework\Webhook\AclPrivilegeCollection;
8
use Shopware\Core\Framework\Webhook\Hookable;
9
10
#[Package('core')]
11
class SystemConfigChangedHook implements Hookable
12
{
13
    /**
14
     * @param array<string, mixed> $values
15
     * @param array<string, string> $appMapping
16
     */
17
    public function __construct(
18
        private readonly array $values,
19
        private readonly array $appMapping
20
    ) {
21
    }
22
23
    public function getName(): string
24
    {
25
        return 'app.config.changed';
26
    }
27
28
    /**
29
     * @return array{changes: array<string>}
30
     */
31
    public function getWebhookPayload(?AppEntity $app = null): array
32
    {
33
        if ($app === null) {
34
            return [
35
                'changes' => array_keys($this->values),
36
            ];
37
        }
38
39
        $values = [];
40
41
        foreach ($this->values as $key => $value) {
42
            if (str_starts_with($key, $app->getName() . '.')) {
43
                $values[] = $key;
44
            }
45
        }
46
47
        return [
48
            'changes' => $values,
49
        ];
50
    }
51
52
    public function isAllowed(string $appId, AclPrivilegeCollection $permissions): bool
53
    {
54
        // Needs basic system_config.read permission
55
        if (!$permissions->isAllowed('system_config', 'read')) {
56
            return false;
57
        }
58
59
        $appName = $this->appMapping[$appId] ?? null;
60
61
        // When app doesn't exist
62
        if ($appName === null) {
63
            return false;
64
        }
65
66
        foreach ($this->values as $k => $v) {
67
            if (str_starts_with($k, $appName . '.')) {
68
                return true;
69
            }
70
        }
71
72
        return false;
73
    }
74
}
75