Completed
Push — feature/EVO-7278-tracking-info... ( d73a1e...c12eb7 )
by
unknown
66:18
created

StoreManager::trackEvent()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 8
nc 3
nop 3
1
<?php
2
/**
3
 * To manage the data to be saved into DB as last thing to do.
4
 */
5
namespace Graviton\AuditTrackingBundle\Manager;
6
7
use Doctrine\ODM\MongoDB\DocumentManager;
8
use Graviton\AuditTrackingBundle\Document\AuditTracking;
9
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
10
11
/**
12
 * Class StoreManager
13
 * @package Graviton\AuditTrackingBundle\Manager
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class StoreManager
20
{
21
    /** @var ActivityManager */
22
    private $activityManager;
23
24
    /** @var DocumentManager */
25
    private $documentManager;
26
27
    /**
28
     * Avoid saving same action again
29
     * @var
30
     */
31
    private $saved;
0 ignored issues
show
Unused Code introduced by
The property $saved is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
32
33
    /**
34
     * StoreManager constructor.
35
     * @param ActivityManager $activityManager Main activity manager
36
     * @param ManagerRegistry $doctrine        Doctrine document mapper
37
     */
38
    public function __construct(
39
        ActivityManager $activityManager,
40
        ManagerRegistry $doctrine
41
    ) {
42
        $this->activityManager = $activityManager;
43
44
        $this->documentManager = $doctrine->getManager();
45
    }
46
47
    /**
48
     * Save data to DB
49
     * onKernelResponse
50
     *
51
     * @return void
52
     */
53
    public function persistEvents()
54
    {
55
        $events = $this->activityManager->getEvents();
56
        $thread = $this->generateUUID();
57
        $username = $this->activityManager->getSecurityUsername();
58
59
        foreach ($events as $event) {
60
            $this->trackEvent($event, $thread, $username);
0 ignored issues
show
Security Bug introduced by
It seems like $username defined by $this->activityManager->getSecurityUsername() on line 57 can also be of type false; however, Graviton\AuditTrackingBu...reManager::trackEvent() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
61
        }
62
    }
63
64
    /**
65
     * Save the event to DB
66
     *
67
     * @param AuditTracking $event    Performed by user
68
     * @param string        $thread   The thread ID
69
     * @param string        $username User connected name
70
     * @return void
71
     */
72
    private function trackEvent($event, $thread, $username)
73
    {
74
        // Request information
75
        $event->setThread($thread);
76
        $event->setUsername($username);
77
78
        try {
79
            $this->documentManager->persist($event);
80
            $this->documentManager->flush($event);
81
        } catch (\Exception $e) {
82
            die(var_dump($e->getMessage()));
0 ignored issues
show
Security Debugging Code introduced by
var_dump($e->getMessage()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
Coding Style Compatibility introduced by
The method trackEvent() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
83
            // TODO LOG the error and event
84
        }
85
    }
86
87
88
89
    /**
90
     * Generate a unique identifer
91
     *
92
     * @return string
93
     */
94
    private function generateUUID()
95
    {
96
        if (!function_exists('openssl_random_pseudo_bytes')) {
97
            return uniqid('unq', true);
98
        }
99
100
        $data = openssl_random_pseudo_bytes(16);
101
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // set version to 0100
102
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // set bits 6-7 to 10
103
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
104
    }
105
}
106