FLockerStore   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 163
Duplicated Lines 14.72 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 24
loc 163
rs 10
c 1
b 0
f 0
wmc 22

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getLockPath() 0 3 1
A exists() 0 7 2
A delete() 0 7 2
A getAll() 0 13 2
A __construct() 0 15 4
A save() 0 12 2
A clear() 0 7 3
A update() 13 13 2
A get() 0 7 2
A acquire() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the LockerManager package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LockerManager\Infrastructure;
12
13
use Cocur\Slugify\Slugify;
14
use LockerManager\Domain\Lock;
15
use LockerManager\Infrastructure\Exception\ExistingKeyException;
16
use LockerManager\Infrastructure\Exception\InvalidArgumentException;
17
use LockerManager\Infrastructure\Exception\LockingKeyException;
18
use LockerManager\Infrastructure\Exception\NotExistingKeyException;
19
20
class FLockerStore implements LockerStoreInterface
21
{
22
    /**
23
     * @var null|string
24
     */
25
    private $lockPath;
26
27
    /**
28
     * FLockerStore constructor.
29
     * @param null $lockPath
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $lockPath is correct as it would always require null to be passed?
Loading history...
30
     */
31
    public function __construct($lockPath = null)
32
    {
33
        if (null === $lockPath) {
34
            $lockPath = sys_get_temp_dir();
35
        }
36
37
        if (!is_dir($lockPath)) {
38
            mkdir($lockPath, 0755, true);
39
        }
40
41
        if (!is_writable($lockPath)) {
42
            throw new InvalidArgumentException(sprintf('The directory "%s" is not writable.', $lockPath));
43
        }
44
45
        $this->lockPath = $lockPath;
46
    }
47
48
    /**
49
     * @param Lock $lock
50
     * @throws ExistingKeyException
51
     */
52 View Code Duplication
    public function acquire(Lock $lock)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $key = $lock->key();
55
        $fileName = $this->getLockPath($key);
56
57
        if (file_exists($fileName)) {
58
            throw new ExistingKeyException(sprintf('The key "%s" already exists.', $key));
59
        }
60
61
        $this->save($lock, $fileName);
62
    }
63
64
    /**
65
     * @param Lock $lock
66
     * @param $fileName
67
     * @throws LockingKeyException
68
     */
69
    private function save(Lock $lock, $fileName)
70
    {
71
        $file = @fopen($fileName, 'w');
72
73
        if (flock($file, LOCK_EX)) {
74
            fwrite($file, serialize($lock));
75
            flock($file, LOCK_UN);
76
        } else {
77
            throw new LockingKeyException(sprintf('Error locking file "%s".', $lock->key()));
78
        }
79
80
        fclose($file);
81
    }
82
83
    /**
84
     * clear all locks
85
     */
86
    public function clear()
87
    {
88
        $files = scandir($this->lockPath);
89
90
        foreach ($files as $file) {
91
            if (is_file($this->lockPath.$file)) {
92
                unlink($this->lockPath.$file);
93
            }
94
        }
95
    }
96
97
    /**
98
     * @param $key
99
     * @throws NotExistingKeyException
100
     */
101
    public function delete($key)
102
    {
103
        if (!$this->exists($key)) {
104
            throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
105
        }
106
107
        unlink($this->getLockPath($key));
108
    }
109
110
    /**
111
     * @param $key
112
     * @return bool
113
     */
114
    public function exists($key)
115
    {
116
        if (!@fopen($this->getLockPath($key), 'r')) {
117
            return false;
118
        }
119
120
        return true;
121
    }
122
123
    /**
124
     * @param $key
125
     * @return string
126
     */
127
    private function getLockPath($key)
128
    {
129
        return $this->lockPath.(new Slugify())->slugify($key).'.lock';
130
    }
131
132
    /**
133
     * @param $key
134
     * @return mixed
135
     * @throws NotExistingKeyException
136
     */
137
    public function get($key)
138
    {
139
        if (!$this->exists($key)) {
140
            throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
141
        }
142
143
        return unserialize(file_get_contents($this->getLockPath($key)));
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function getAll()
150
    {
151
        $files = scandir($this->lockPath);
152
        $locks = [];
153
154
        unset($files[0]);
155
        unset($files[1]);
156
157
        foreach ($files as $lock) {
158
            $locks[] = str_replace('.lock', '', $lock);
159
        }
160
161
        return array_values($locks);
162
    }
163
164
    /**
165
     * @param $key
166
     * @param $payload
167
     * @throws LockingKeyException
168
     * @throws NotExistingKeyException
169
     */
170 View Code Duplication
    public function update($key, $payload)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    {
172
        $fileName = $this->getLockPath($key);
173
174
        if (!file_exists($fileName)) {
175
            throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
176
        }
177
178
        /** @var Lock $lock */
179
        $lock = $this->get($key);
180
        $lock->update($payload);
181
182
        $this->save($lock, $fileName);
183
    }
184
}
185