File::deleteProcess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arnaud
5
 * Date: 01/11/15
6
 * Time: 19:46
7
 */
8
9
namespace Ndrx\Profiler\DataSources;
10
11
use Ndrx\Profiler\DataSources\Contracts\DataSourceInterface;
12
use Ndrx\Profiler\Process;
13
use Symfony\Component\Filesystem\Exception\IOException;
14
use Symfony\Component\Filesystem\Filesystem;
15
use Symfony\Component\Finder\Finder;
16
use Symfony\Component\Finder\SplFileInfo;
17
18
/**
19
 * Class File
20
 * @package Ndrx\Profiler\DataSources
21
 */
22
class File implements DataSourceInterface
23
{
24
    const SUMMARY_FILENAME = 'summary.json';
25
26
    /**
27
     * @var string
28
     */
29
    protected $folder;
30
31
    /**
32
     * @var Filesystem
33
     */
34
    protected $filesystem;
35
36
    /**
37
     * File constructor.
38
     */
39 112
    public function __construct($outputFolder)
40
    {
41 112
        $this->folder = $outputFolder;
42
43 112
        $this->filesystem = new Filesystem();
44 112
        $this->initializeFolder();
45 112
    }
46
47
    /**
48
     * @param $processId
49
     * @return \Generator
50
     * @throws \InvalidArgumentException
51
     */
52 16
    public function getProcess($processId)
53
    {
54 16
        $finder = new Finder();
55
        $iterator = $finder
56 16
            ->name('*.json')
57 16
            ->sortByName()
58 16
            ->in($this->getProcessFolder($processId));
59
60
        /** @var SplFileInfo $file */
61 16
        foreach ($iterator as $file) {
62 12
            if (self::SUMMARY_FILENAME === $file->getFilename()) {
63 2
                continue;
64
            }
65 12
            yield file_get_contents($file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename());
66 16
        }
67 16
    }
68
69
    /**
70
     * @throws IOException
71
     * @throws \InvalidArgumentException
72
     */
73 14
    public function clear()
74
    {
75 14
        $finder = new Finder();
76 14
        $this->filesystem->remove($finder->in($this->folder));
77 14
    }
78
79
    /**
80
     * @throws IOException
81
     * @throws \InvalidArgumentException
82
     */
83 2
    public function deleteProcess($processId)
84
    {
85 2
        $this->filesystem->remove($this->getProcessFolder($processId));
86 2
    }
87
88
    /**
89
     * @param Process $process
90
     * @param array $item
91
     * @throws IOException
92
     * @return bool
93
     */
94 110
    public function save(Process $process, array $item)
95
    {
96 110
        $fileName = $this->getProcessFolder($process->getId())
97 110
            . DIRECTORY_SEPARATOR . microtime(true)
98 110
            . '-' . rand() . '.json';
99
100 110
        return file_put_contents($fileName, json_encode($item)) !== false;
101
    }
102
103
    /**
104
     * @return string
105
     * @throws IOException
106
     */
107 112
    protected function initializeFolder()
108
    {
109 112
        if (!is_dir($this->folder)) {
110 4
            $this->filesystem->mkdir($this->folder, 0777);
111 4
        }
112 112
    }
113
114
    /**
115
     * @param $processId
116
     * @return string
117
     * @throws IOException
118
     */
119 114
    protected function getProcessFolder($processId)
120
    {
121 114
        $processFolder = $this->folder . DIRECTORY_SEPARATOR . $processId;
122
123 114
        if (!is_dir($processFolder)) {
124 112
            $this->filesystem->mkdir($processFolder, 0777);
125 112
        }
126
127 114
        return $processFolder;
128
    }
129
130
    /**
131
     * @param int $offset
132
     * @param int $limit
133
     * @throws IOException
134
     * @throws \InvalidArgumentException
135
     * @throws \LogicException
136
     * @return array
137
     */
138 8
    public function all($offset = 0, $limit = 15)
139
    {
140 8
        $finder = new Finder();
141
        $iterator = $finder
142 8
            ->directories()
143 8
            ->depth(0)
144 8
            ->sortByModifiedTime()
145 8
            ->in($this->folder)
146 8
            ->getIterator();
147
148
149 8
        $processFiles = array_slice(iterator_to_array($iterator), $offset, $limit);
150
151 8
        $process = [];
152
        /** @var SplFileInfo $current */
153 8
        foreach ($processFiles as $current) {
154 8
            $summaryFile = $current->getPath() . DIRECTORY_SEPARATOR
155 8
                . $current->getFilename() . DIRECTORY_SEPARATOR
156 8
                . self::SUMMARY_FILENAME;
157
158 8
            if ($this->filesystem->exists($summaryFile)) {
159 4
                $process[] = json_decode(file_get_contents($summaryFile));
160 4
            }
161 8
        }
162
163 8
        return $process;
164
    }
165
166
    /**
167
     * @return int
168
     * @throws IOException
169
     * @throws \InvalidArgumentException
170
     */
171 12
    public function count()
172
    {
173 12
        $finder = new Finder();
174
175
        return $finder
176 12
            ->directories()
177 12
            ->depth(0)
178 12
            ->in($this->folder)
179 12
            ->count();
180
    }
181
182
    /**
183
     * @param Process $process
184
     * @param array $item
185
     * @return mixed
186
     * @throws IOException
187
     */
188 6
    public function saveSummary(Process $process, array $item)
189
    {
190 6
        $fileName = $this->getProcessFolder($process->getId())
191 6
            . DIRECTORY_SEPARATOR . self::SUMMARY_FILENAME;
192
193 6
        if ($this->filesystem->exists($fileName)) {
194 2
            $content = json_decode(file_get_contents($fileName));
195 2
            if (is_bool($content)) {
196
                $content = [];
197
            }
198
199 4
            $item = array_merge((array) $content, $item);
200 2
        }
201
202 6
        return file_put_contents($fileName, json_encode($item)) !== false;
203
    }
204
205
    /**
206
     * @return string
207
     */
208 2
    public function getFolder()
209
    {
210 2
        return $this->folder;
211
    }
212
}
213