Passed
Push — master ( 254acf...239936 )
by Christian
17:56 queued 04:52
created

AppCookieProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Framework\Cookie;
4
5
use Shopware\Core\Framework\App\AppEntity;
6
use Shopware\Core\Framework\Context;
7
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
8
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
9
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
10
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
11
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
12
13
class AppCookieProvider implements CookieProviderInterface
14
{
15
    /**
16
     * @var CookieProviderInterface
17
     */
18
    private $inner;
19
20
    /**
21
     * @var EntityRepositoryInterface
22
     */
23
    private $appRepository;
24
25
    public function __construct(CookieProviderInterface $inner, EntityRepositoryInterface $appRepository)
26
    {
27
        $this->inner = $inner;
28
        $this->appRepository = $appRepository;
29
    }
30
31
    public function getCookieGroups(): array
32
    {
33
        $result = $this->appRepository->search(
34
            (new Criteria())->addFilter(new NotFilter(NotFilter::CONNECTION_AND, [
35
                new EqualsFilter('app.cookies', null),
36
            ])),
37
            Context::createDefaultContext()
38
        );
39
40
        $cookies = array_values($this->inner->getCookieGroups());
41
42
        if ($result->count() < 1) {
43
            return $cookies;
44
        }
45
46
        return $this->mergeCookies($cookies, $result);
47
    }
48
49
    /**
50
     * merges cookie groups by the snippet name of the group
51
     * and only iterates once over every cookie
52
     */
53
    private function mergeCookies(array $cookies, EntitySearchResult $apps): array
54
    {
55
        $cookieGroups = [];
56
        // build an array with the snippetName of a cookie group and the index in the cookies array
57
        // this way we need to iterate only once over the cookies
58
        foreach ($cookies as $index => $cookie) {
59
            if (array_key_exists('entries', $cookie)) {
60
                $cookieGroups[$cookie['snippet_name']] = $index;
61
            }
62
        }
63
64
        /** @var AppEntity $app */
65
        foreach ($apps->getEntities() as $app) {
66
            foreach ($app->getCookies() as $cookie) {
67
                // cookies that are not part of a group can simply be added to the cookies array
68
                if (!array_key_exists('entries', $cookie)) {
69
                    $cookies[] = $cookie;
70
71
                    continue;
72
                }
73
74
                // if a cookie group with the same name already exists in the cookies array
75
                // we merge the entries of both cookie groups
76
                if (array_key_exists($cookie['snippet_name'], $cookieGroups)) {
77
                    $originalIndex = $cookieGroups[$cookie['snippet_name']];
78
                    $cookies[$originalIndex]['entries'] = array_merge(
79
                        $cookies[$originalIndex]['entries'],
80
                        $cookie['entries']
81
                    );
82
83
                    continue;
84
                }
85
86
                // if no group with that name exists we add the cookie group to the cookies array
87
                // and add the snippet name and the index to the snippet group array
88
                $cookies[] = $cookie;
89
                $cookieGroups[$cookie['snippet_name']] = count($cookies) - 1;
90
            }
91
        }
92
93
        return $cookies;
94
    }
95
}
96