QueueHistory   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 89.8%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 124
ccs 44
cts 49
cp 0.898
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setStoreDirectory() 0 4 1
A setFilesystem() 0 4 1
A setFinder() 0 4 1
A setHistoryLength() 0 4 1
A put() 0 10 1
B get() 0 30 5
A getPath() 0 8 1
A sanitize() 0 4 1
1
<?php
2
3
namespace Innmind\ProvisionerBundle\RabbitMQ;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Symfony\Component\Finder\Finder;
7
8
/**
9
 * Put data relative to a rabbitmq queue in an ENV variable
10
 */
11
class QueueHistory implements HistoryInterface
12
{
13
    protected $dir;
14
    protected $filesystem;
15
    protected $finder;
16
    protected $data = [];
17
    protected $length = 10;
18
19
    /**
20
     * Set the directory path where to store data
21
     *
22
     * @param string $dir
23
     */
24 18
    public function setStoreDirectory($dir)
25
    {
26 18
        $this->dir = (string) $dir;
27 18
    }
28
29
    /**
30
     * Set the filesystem
31
     *
32
     * @param Filesystem $fs
33
     */
34 18
    public function setFilesystem(Filesystem $fs)
35
    {
36 18
        $this->filesystem = $fs;
37 18
    }
38
39
    /**
40
     * Set the file finder
41
     *
42
     * @param Finder $finder
43
     */
44 18
    public function setFinder(Finder $finder)
45
    {
46 18
        $this->finder = $finder;
47 18
    }
48
49
    /**
50
     * Set the mex history length
51
     *
52
     * @param int $length
53
     */
54 3
    public function setHistoryLength($length)
55
    {
56 3
        $this->length = (int) $length;
57 3
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 9
    public function put($key, array $value)
63
    {
64 9
        $value = array_slice($value, -$this->length);
65 9
        $this->data[$this->sanitize($key)] = $value;
66
67 9
        $this->filesystem->dumpFile(
68 9
            $this->getPath($key),
69 9
            json_encode($value)
70 9
        );
71 9
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 9
    public function get($key)
77
    {
78 9
        $path = $this->getPath($key);
79 9
        $dirname = dirname($path);
80
81 9
        if (isset($this->data[$this->sanitize($key)])) {
82 6
            return $this->data[$this->sanitize($key)];
83
        }
84
85 3
        if (!is_dir($dirname)) {
86 3
            $this->filesystem->mkdir($dirname);
87 3
        }
88
89 3
        $files = $this
90 2
            ->finder
91 3
            ->files()
92 3
            ->name(basename($path))
93 3
            ->in($dirname);
94
95 3
        if (count($files) === 0) {
96 3
            return [];
97
        }
98
99
        foreach ($files as $file) {
100
            $data = json_decode($file->getContents());
101 2
            $this->data[$this->sanitize($key)] = $data;
102
103
            return $data;
104
        }
105
    }
106
107
    /**
108
     * Build absolute path to the filename
109
     *
110
     * @param string $key
111
     *
112
     * @return string
113
     */
114 9
    protected function getPath($key)
115
    {
116 9
        return sprintf(
117 9
            '%s/%s.data',
118 9
            $this->dir,
119 9
            strtolower($this->sanitize((string) $key))
120 9
        );
121
    }
122
123
    /**
124
     * Transform any special character to an underscore
125
     *
126
     * @param string $text
127
     *
128
     * @return string
129
     */
130 9
    protected function sanitize($text)
131
    {
132 9
        return preg_replace('/[^a-zA-Z0-9]+/', '_', $text);
133
    }
134
}
135