File size: 3,031 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

namespace Mautic\EmailBundle\Helper;

use Mautic\CacheBundle\Cache\CacheProviderInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Request;

/**
 * Helper class for storing request payload to a cache location and retrieving it back as a Request.
 *
 * @deprecated as unused. To be removed in Mautic 6.0.
 */
class RequestStorageHelper
{
    /**
     * Separator between the transport class name and random hash.
     */
    public const KEY_SEPARATOR = ';webhook_request;';

    public function __construct(
        private CacheProviderInterface $cacheStorage
    ) {
    }

    /**
     * Stores the request content into cache and returns the unique key under which it's stored.
     *
     * @param string $transportName
     */
    public function storeRequest($transportName, Request $request): string
    {
        $key  = $this->getUniqueCacheHash($transportName);
        $item = $this->cacheStorage->getItem($key);
        $item->set($request->request->all());
        $this->cacheStorage->save($item);

        return $key;
    }

    /**
     * Creates new Request with the original payload.
     *
     * @param string $key
     *
     * @throws \UnexpectedValueException
     */
    public function getRequest($key): Request
    {
        $error = "Request with key '{$key}' was not found.";
        $key   = $this->removeCachePrefix($key);

        try {
            $item = $this->cacheStorage->getItem($key);
        } catch (InvalidArgumentException) {
            throw new \UnexpectedValueException($error);
        }

        if (!$item->isHit()) {
            throw new \UnexpectedValueException($error);
        }

        return new Request([], $item->get());
    }

    /**
     * @param string $key
     */
    public function deleteCachedRequest($key): void
    {
        $this->cacheStorage->deleteItem($this->removeCachePrefix($key));
    }

    /**
     * Reads the transport class name path from the key.
     *
     * @param string $key
     *
     * @return string
     */
    public function getTransportNameFromKey($key)
    {
        $key = $this->removeCachePrefix($key);

        // Take the part before the key separator as the serialized transpot name.
        [$serializedTransportName] = explode(self::KEY_SEPARATOR, $key);

        // Unserialize transport name to the standard full class name.
        $transportName = str_replace('|', '\\', $serializedTransportName);

        return $transportName;
    }

    /**
     * Remove the default cache key prefix if set.
     */
    private function removeCachePrefix(string $key): string
    {
        if (str_starts_with($key, 'mautic:')) {
            $key = ltrim($key, 'mautic:');
        }

        return $key;
    }

    /**
     * Generates unique hash in format $transportName:webhook_request:unique.hash.
     */
    private function getUniqueCacheHash(string $transportName): string
    {
        return uniqid(str_replace('\\', '|', $transportName).self::KEY_SEPARATOR, true);
    }
}