Dispatch::onPostDispatchSecureFrontend()   C
last analyzed

Complexity

Conditions 8
Paths 4

Size

Total Lines 66
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 39
nc 4
nop 1
dl 0
loc 66
rs 6.7081
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace DnViewSnapshots\Subscriber;
4
5
use Doctrine\DBAL\Connection;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Connection 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 Enlight\Event\SubscriberInterface;
0 ignored issues
show
Bug introduced by
The type Enlight\Event\SubscriberInterface 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...
7
8
/**
9
 * Class Dispatch
10
 * @package DnViewSnapshots\Subscriber
11
 */
12
class Dispatch implements SubscriberInterface
13
{
14
    /**
15
     * @var \Enlight_Components_Session_Namespace
0 ignored issues
show
Bug introduced by
The type Enlight_Components_Session_Namespace 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...
16
     */
17
    private $session;
18
19
    /**
20
     * @var Connection
21
     */
22
    private $connection;
23
24
    /**
25
     * Dispatch constructor.
26
     * @param \Enlight_Components_Session_Namespace $session
27
     * @param Connection $connection
28
     */
29
    public function __construct(
30
        \Enlight_Components_Session_Namespace $session,
31
        Connection $connection
32
    )
33
    {
34
        $this->session = $session;
35
        $this->connection = $connection;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public static function getSubscribedEvents()
42
    {
43
        return [
44
            'Enlight_Controller_Action_PostDispatchSecure_Frontend' => 'onPostDispatchSecureFrontend',
45
        ];
46
    }
47
48
    /**
49
     * @param \Enlight_Controller_ActionEventArgs $args
50
     */
51
    public function onPostDispatchSecureFrontend(\Enlight_Controller_ActionEventArgs $args)
0 ignored issues
show
Bug introduced by
The type Enlight_Controller_ActionEventArgs 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...
52
    {
53
        $view = $args->getSubject()->View();
54
        $request = $args->getSubject()->Request();
55
        $params = $request->getParams();
56
        $sessionID = $this->session->get('sessionId');
57
58
        $isSessionRecorded = strtolower($request->getControllerName()) !== 'snapshots' ?
59
            $this->session->get('isSessionRecorded') :
60
            false;
61
62
        $snapshotSessionID = $view->getAssign('snapshotSessionID');
63
64
        $view->assign(
65
            [
66
                'snapshotSessionID' => $snapshotSessionID ? : $sessionID,
67
                'isSessionRecorded' => $isSessionRecorded,
68
            ]
69
        );
70
71
        if (
72
            $snapshotSessionID ||
73
            !$isSessionRecorded ||
74
            $request->isXmlHttpRequest()
75
        )
76
        {
77
            return;
78
        }
79
80
        $template = $view->Template()->template_resource;
81
82
        $variables = $view->getAssign();
83
84
        array_walk_recursive($variables, function (&$value) {
85
            if (is_object($value)) {
86
                try {
87
                    // workaround for PDOException when trying to serialize PDO instances
88
                    serialize($value);
89
                }
90
                catch (\Throwable $e) {
91
                    // as we only need a snapshot for the view, remove the PDO instance
92
                    $value = null;
93
                }
94
            }
95
        });
96
97
        $variables = serialize($variables);
98
99
        $params['__controller'] = $request->getControllerName();
100
        $params['__action'] = $request->getActionName();
101
        $params = json_encode($params);
102
103
        $step = (int)$this->connection->fetchColumn(
104
            'SELECT MAX(`step`) FROM `view_snapshots` WHERE `sessionID` = :sessionID',
105
            ['sessionID' => $sessionID]
106
        );
107
        $step++;
108
109
        $this->connection->insert(
110
            'view_snapshots',
111
            [
112
                'sessionID' => $sessionID,
113
                'template' => $template,
114
                'variables' => $variables,
115
                'params' => $params,
116
                'step' => $step,
117
            ]
118
        );
119
    }
120
}