Lto   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 12
c 3
b 1
f 0
lcom 1
cbo 3
dl 0
loc 94
ccs 0
cts 59
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A put() 0 17 3
A delete() 0 13 3
B listContents() 0 34 5
1
<?php
2
namespace AppBundle\Sync\Storage;
3
4
use AppBundle\Sync\Entity\File;
5
use AppBundle\Sync\Entity\FileCollection;
6
use AppBundle\Exception\StorageException;
7
use DateTime;
8
9
/**
10
 * LTO app storage
11
 *
12
 * @author Sergey Sadovoi <[email protected]>
13
 */
14
class Lto implements StorageInterface
15
{
16
    /**
17
     * @var string  Regex pattern for file list
18
     */
19
    private $regList;
20
21
    /**
22
     * Init the regList pattern
23
     */
24
    public function __construct()
25
    {
26
        $regExp = "~(?P<size>[\d,]+) +B +(?P<date>\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}) ".
27
            "+(?P<path>[^ ]+) +Never +Archive +Date: +(?P<archive_date>\d{2}\/\d{2}\/\d{2})~";
28
29
        $this->regList = $regExp;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function put($sourcePath, $destPath)
36
    {
37
        if (!is_file($sourcePath)) {
38
            throw new StorageException('File %s is missing', $sourcePath);
39
        }
40
41
        $this->delete($destPath);
42
43
        $command = sprintf('dsmc archive %s -v2archive', $destPath);
44
        $result  = shell_exec($command);
45
46
        if (strpos($result, 'finished without failure') === false) {
47
            throw new StorageException(
48
                sprintf('Archive error while processing command "%s". Server response: %s', $command, $result)
49
            );
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function delete($path)
57
    {
58
        $command = sprintf('dsmc delete archive %s -noprompt', $path);
59
        $result  = shell_exec($command);
60
61
        if (strpos($result, 'No objects on server match query')            === false &&
62
            strpos($result, 'Total number of objects deleted:          1') === false
63
        ) {
64
            throw new StorageException(
65
                sprintf('Delete error while processing command "%s". Server response: %s', $command, $result)
66
            );
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function listContents($directory = '')
74
    {
75
        $command = sprintf('dsmc query archive "%s/*" -subdir=yes', $directory);
76
        $result  = shell_exec($command);
77
78
        $fc = new FileCollection();
79
80
        if (strpos($result, 'No files matching search criteria were found') !== false) {
81
            return $fc;
82
        }
83
84
        $lines = explode("\n", $result);
85
        foreach ($lines as $line) {
86
            if (preg_match($this->regList, $line, $match)) {
87
                $size     = str_replace(',', '', $match['size']);
88
                $modified = DateTime::createFromFormat('m/d/Y H:i:s', $match['date']);
89
90
                if (false === $modified) {
91
                    continue;
92
                }
93
94
                $file = new File();
95
96
                $file->setUid(basename($match['path']));
97
                $file->setPath($match['path']);
98
                $file->setSize($size);
99
                $file->setModified($modified);
100
101
                $fc->addFile($file);
102
            }
103
        }
104
105
        return $fc;
106
    }
107
}
108