Passed
Push — main ( a3578d...cf5a21 )
by Filipe
01:23 queued 31s
created

Snapshot::path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of fswatch
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\FsWatch\Directory;
13
14
use Slick\FsWatch\Directory;
15
use Slick\FsWatch\FileTools;
16
17
/**
18
 * Snapshot
19
 *
20
 * @package Slick\FsWatch\Directory
21
 */
22
final class Snapshot
23
{
24
    /** @var array<string, mixed> */
25
    protected array $sizeMap;
26
    private string $path;
27
28
    private FileTools $fileTools;
29
30
    /**
31
     * Creates a Snapshot
32
     *
33
     * @param Directory $directory
34
     */
35 11
    public function __construct(Directory $directory)
36
    {
37 11
        $this->sizeMap = $directory->sizeMap();
38 11
        $this->path = $directory->path();
39 11
        $this->fileTools = new FileTools();
40
    }
41
42
    /**
43
     * Directory content size hash
44
     *
45
     * @return string
46
     */
47 5
    public function hash(): string
48
    {
49 5
        return sha1(serialize($this->sizeMap));
50
    }
51
52
    /**
53
     * Directory path
54
     *
55
     * @return string
56
     */
57 2
    public function path(): string
58
    {
59 2
        return $this->path;
60
    }
61
62
    /**
63
     * @return array<string, mixed>
64
     */
65 3
    public function __serialize(): array
66
    {
67 3
        return [
68 3
            "path" => $this->path,
69 3
            "size" => $this->sizeMap,
70 3
        ];
71
    }
72
73
    /**
74
     * @param array<string, mixed> $data
75
     * @return void
76
     */
77 3
    public function __unserialize(array $data): void
78
    {
79 3
        $this->path = $data['path'];
80 3
        $this->sizeMap = $data['size'];
81 3
        $this->fileTools = new FileTools();
82
    }
83
84
    /**
85
     * Compares the current Snapshot with another Snapshot and determines the changes that occurred.
86
     *
87
     * @param Snapshot $secondSnapshot The second Snapshot to compare with.
88
     * @return Changes The changes that occurred between the two Snapshots.
89
     */
90 3
    public function compareTo(Snapshot $secondSnapshot): Changes
91
    {
92 3
        $changes = new Changes();
93 3
        $second = $this->fileTools->flattenSizeMap($secondSnapshot->sizeMap);
94 3
        $first = $this->fileTools->flattenSizeMap($this->sizeMap);
95
96 3
        $diff = array_diff_assoc($second, $first);
97
98 3
        foreach (array_keys($diff) as $key) {
99 3
            if (array_key_exists($key, $first)) {
100 1
                $changes->add($this->path.'/'.$key, FileChange::CHANGED);
101 1
                continue;
102
            }
103 3
            $changes->add($this->path.'/'.$key, FileChange::ADDED);
104
        }
105
106 3
        $diff = array_diff_assoc($first, $second);
107 3
        foreach (array_keys($diff) as $key) {
108 1
            if (!array_key_exists($key, $second)) {
109 1
                $changes->add($this->path.'/'.$key, FileChange::DELETED);
110
            }
111
        }
112 3
        return $changes;
113
    }
114
}
115