Code Duplication    Length = 36-36 lines in 2 locations

src/Plugin/ForcedCopy.php 1 location

@@ 7-42 (lines=36) @@
4
5
use League\Flysystem\FileNotFoundException;
6
7
class ForcedCopy extends AbstractPlugin
8
{
9
    /**
10
     * @inheritdoc
11
     */
12
    public function getMethod()
13
    {
14
        return 'forceCopy';
15
    }
16
17
    /**
18
     * Copies a file, overwriting any existing files.
19
     *
20
     * @param string $path    Path to the existing file.
21
     * @param string $newpath The new path of the file.
22
     *
23
     * @throws FileNotFoundException Thrown if $path does not exist.
24
     *
25
     * @return bool True on success, false on failure.
26
     */
27
    public function handle($path, $newpath)
28
    {
29
        try {
30
            $deleted = $this->filesystem->delete($newpath);
31
        } catch (FileNotFoundException $e) {
32
            // The destination path does not exist. That's ok.
33
            $deleted = true;
34
        }
35
36
        if ($deleted) {
37
            return $this->filesystem->copy($path, $newpath);
38
        }
39
40
        return false;
41
    }
42
}
43

src/Plugin/ForcedRename.php 1 location

@@ 7-42 (lines=36) @@
4
5
use League\Flysystem\FileNotFoundException;
6
7
class ForcedRename extends AbstractPlugin
8
{
9
    /**
10
     * @inheritdoc
11
     */
12
    public function getMethod()
13
    {
14
        return 'forceRename';
15
    }
16
17
    /**
18
     * Renames a file, overwriting the destination if it exists.
19
     *
20
     * @param string $path    Path to the existing file.
21
     * @param string $newpath The new path of the file.
22
     *
23
     * @throws FileNotFoundException Thrown if $path does not exist.
24
     *
25
     * @return bool True on success, false on failure.
26
     */
27
    public function handle($path, $newpath)
28
    {
29
        try {
30
            $deleted = $this->filesystem->delete($newpath);
31
        } catch (FileNotFoundException $e) {
32
            // The destination path does not exist. That's ok.
33
            $deleted = true;
34
        }
35
36
        if ($deleted) {
37
            return $this->filesystem->rename($path, $newpath);
38
        }
39
40
        return false;
41
    }
42
}
43