Memory::deleteProcess()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2
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
14
/**
15
 * Class Memory
16
 *
17
 * @package Ndrx\Profiler\DataSources
18
 */
19
class Memory implements DataSourceInterface
20
{
21
    const SUMMARY_KEY = 'summary';
22
23
    protected $memory = [];
24
25
    /**
26
     * @param int $offset
27
     * @param int $limit
28
     *
29
     * @return mixed
30
     */
31 4
    public function all($offset = 0, $limit = 15)
32
    {
33 4
        $results = array_slice($this->memory, $offset, $limit);
34
35 4
        foreach ($results as $key => $value) {
36 4
            $results[$key] = json_decode($value[self::SUMMARY_KEY]);
37 4
        }
38
39 4
        return $results;
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45 14
    public function count()
46
    {
47 14
        return count($this->memory);
48
    }
49
50
    /**
51
     * @param $processId
52
     *
53
     * @return \Generator
54
     */
55 6
    public function getProcess($processId)
56
    {
57 6
        $patchs = $this->memory[$processId];
58 6
        unset($patchs[self::SUMMARY_KEY]);
59 6
        foreach ($patchs as $patch) {
60 6
            yield $patch;
61 6
        }
62 6
    }
63
64
    /**
65
     * @return mixed
66
     */
67 2
    public function clear()
68
    {
69 2
        $this->memory = [];
70 2
    }
71
72
    /**
73
     * @param $processId
74
     *
75
     * @return mixed
76
     */
77 2
    public function deleteProcess($processId)
78
    {
79 2
        if (array_key_exists($processId, $this->memory)) {
80 2
            unset($this->memory[$processId]);
81 2
        }
82 2
    }
83
84
    /**
85
     * @param Process $process
86
     * @param array $item
87
     *
88
     * @return mixed
89
     */
90 62
    public function save(Process $process, array $item)
91
    {
92 62
        $this->initiateMemoryProcess($process);
93 62
        $this->memory[$process->getId()][] = json_encode($item);
94 62
    }
95
96
    /**
97
     * @param Process $process
98
     * @param array $item
99
     *
100
     * @return mixed
101
     */
102 8
    public function saveSummary(Process $process, array $item)
103
    {
104 8
        $this->initiateMemoryProcess($process);
105 8
        if (array_key_exists($process->getId(), $this->memory)
106 8
            && array_key_exists(self::SUMMARY_KEY, $this->memory[$process->getId()])
107 8
        ) {
108 2
            $content = (array)json_decode($this->memory[$process->getId()][self::SUMMARY_KEY]);
109 2
            $item = array_merge($content, $item);
110 2
        }
111
112 8
        $this->memory[$process->getId()][self::SUMMARY_KEY] = json_encode($item);
113 8
    }
114
115 66
    protected function initiateMemoryProcess(Process $process)
116
    {
117 66
        if (!array_key_exists($process->getId(), $this->memory)) {
118 64
            $this->memory[$process->getId()] = [];
119 64
        }
120 66
    }
121
}
122