Issues (6)

src/Plugin/SectionLoadControllerPlugin.php (1 issue)

Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace IntegerNet\SessionUnblocker\Plugin;
5
6
use Magento\Framework\Session\Generic as GenericSession;
7
use Magento\Framework\Session\SessionManagerInterface;
8
9
/**
10
 * We are writing this plugin to make sure sessions are all loaded before
11
 * Magento\Customer\Controller\Section\Load
12
 *
13
 * Right after initiating all the needed Sessions we close the session,
14
 * since we're only reading from the session when requesting sectionData
15
 * in the frontend.
16
 *
17
 * This means every sectionPool that is being loaded does not need to read
18
 * from an open session, which means calls to SectionLoad controller are
19
 * no longer blocking each other because they are waiting for the request to
20
 * finish and the close the session.
21
 */
22
class SectionLoadControllerPlugin
23
{
24
    /**
25
     * @param GenericSession $genericSession
26
     * @param SessionManagerInterface[] $additionalSessions
27
     *
28
     * Disabling 3 PHPCS rules because:
29
     * 1 - We are well aware that we normally shouldn't call Sessions without
30
     *     proxy, but in this case, we actually want the sessions to be
31
     *     initiated directly.
32
     * 2 - Also, we don't actually use the Sessions
33
     * 3 - Lastly, we normally should not execute operations in a constructor
34
     *
35
     * phpcs:disable MEQP2.Classes.MutableObjects.MutableObjects
36
     * phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found
37
     * phpcs:disable MEQP2.Classes.ConstructorOperations.CustomOperationsFound
38
     */
39 3
    public function __construct(
40
        GenericSession $genericSession,
41
        array $additionalSessions = []
42
    ) {
43
        /**
44
         * This is earliest moment where we can close the session,
45
         * after we initialised all sessions we think will be needed
46
         *
47
         * Should there ever be an additional Session-type that's needed,
48
         * nothing breaks, but the new session-type will open a new session
49
         * and therefore block other requests
50
         */
51 3
        $hasMessages = 0;
52 3
        foreach ($additionalSessions as $session) {
53 3
            if ($session instanceOf \Magento\Framework\Message\Session){
54
                // @param \Magento\Framework\Message\Collection $messageCollection
55 3
                foreach ($session->getData() as $messageCollection){
56 1
                    $hasMessages += count($messageCollection->getItems());
57
                }
58
            }
59
        }
60
        /**
61
         * We've checked if there were no messages in the current session
62
         * because the session then needs to stay open to allow the messages
63
         * to be removed after loading them
64
        */
65 3
        if ($hasMessages === 0 ) {
0 ignored issues
show
The condition $hasMessages === 0 is always true.
Loading history...
66 3
            $genericSession->writeClose();
67
        }
68 3
    }
69
70
    //phpcs:ignore MEQP2.Classes.PublicNonInterfaceMethods.PublicMethodFound
71 3
    public function beforeExecute()
72
    {
73 3
    }
74
}
75