Completed
Pull Request — master (#10)
by Tomáš
04:50 queued 01:55
created

SourceSet::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Source;
13
14
final class SourceSet
15
{
16
    /**
17
     * @var array
18
     */
19
    private $sources = [];
20
21
    /**
22
     * @var array
23
     */
24
    private $newSources = [];
25
26 6
    public function __construct(array $sources = [])
27
    {
28 6
        foreach ($sources as $source) {
29 4
            $this->sources[$source->sourceId()] = $source;
30
        }
31 6
    }
32
33 2
    public function containsSource(SourceInterface $source) : bool
34
    {
35 2
        return array_key_exists($source->sourceId(), $this->sources);
36
    }
37
38 1
    public function mergeSource(SourceInterface $source)
39
    {
40 1
        if (array_key_exists($source->sourceId(), $this->sources)) {
41 1
            unset($this->sources[$source->sourceId()]);
42
        } else {
43 1
            $this->newSources[$source->sourceId()] = $source;
44
        }
45 1
        $this->sources[$source->sourceId()] = $source;
46 1
    }
47
48
    /**
49
     * @return SourceInterface[]
50
     */
51 2
    public function allSources() : array
52
    {
53 2
        return $this->sources;
54
    }
55
56
    /**
57
     * @return SourceInterface[]
58
     */
59
    public function updatedSources() : array
60
    {
61 1
        return array_filter($this->sources, function (SourceInterface $source) {
62 1
            return $source->hasChanged();
63 1
        });
64
    }
65
66
    /**
67
     * @return SourceInterface[]
68
     */
69
    public function newSources() : array
70
    {
71
        return $this->newSources;
72
    }
73
74
    /**
75
     * Should be called after each loop while watching.
76
     */
77 1
    public function reset()
78
    {
79 1
        foreach ($this->sources as $source) {
80 1
            $source->setHasNotChanged();
81
        }
82
83 1
        $this->newSources = [];
84 1
    }
85
}
86