Repository::findIndex()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the composer-link plugin.
7
 *
8
 * Copyright (c) 2021-2022 Sander Visser <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE.md
11
 * file that was distributed with this source code.
12
 *
13
 * @link https://github.com/SanderSander/composer-link
14
 */
15
16
namespace ComposerLink\Repository;
17
18
use ComposerLink\LinkedPackage;
19
use RuntimeException;
20
21
class Repository
22
{
23
    protected Transformer $transformer;
24
25
    protected StorageInterface $storage;
26
27
    /**
28
     * @var array<int, LinkedPackage>
29
     */
30
    protected array $linkedPackages = [];
31
32
    public function __construct(StorageInterface $storage, Transformer $transformer)
33
    {
34
        $this->transformer = $transformer;
35
        $this->storage = $storage;
36
37
        $this->load();
38
    }
39
40
    public function store(LinkedPackage $linkedPackage): void
41
    {
42
        $index = $this->findIndex($linkedPackage);
43
44
        if (is_null($index)) {
45
            $this->linkedPackages[] = clone $linkedPackage;
46
47
            return;
48
        }
49
50
        $this->linkedPackages[$index] = clone $linkedPackage;
51
    }
52
53
    /**
54
     * @return LinkedPackage[]
55
     */
56
    public function all(): array
57
    {
58
        $all = [];
59
        foreach ($this->linkedPackages as $package) {
60
            $all[] = clone $package;
61
        }
62
63
        return $all;
64
    }
65
66
    public function findByPath(string $path): ?LinkedPackage
67
    {
68
        foreach ($this->linkedPackages as $linkedPackage) {
69
            if ($linkedPackage->getPath() === $path) {
70
                return clone $linkedPackage;
71
            }
72
        }
73
74
        return null;
75
    }
76
77
    public function findByName(string $name): ?LinkedPackage
78
    {
79
        foreach ($this->linkedPackages as $linkedPackage) {
80
            if ($linkedPackage->getName() === $name) {
81
                return clone $linkedPackage;
82
            }
83
        }
84
85
        return null;
86
    }
87
88
    public function remove(LinkedPackage $linkedPackage): void
89
    {
90
        $index = $this->findIndex($linkedPackage);
91
92
        if (is_null($index)) {
93
            throw new RuntimeException('Linked package not found');
94
        }
95
96
        array_splice($this->linkedPackages, $index, 1);
97
    }
98
99
    public function persist(): void
100
    {
101
        $data = [
102
            'packages' => [],
103
        ];
104
        foreach ($this->linkedPackages as $package) {
105
            $data['packages'][] = $this->transformer->export($package);
106
        }
107
108
        $this->storage->write($data);
109
    }
110
111
    private function load(): void
112
    {
113
        if (!$this->storage->hasData()) {
114
            return;
115
        }
116
117
        $data = $this->storage->read();
118
119
        foreach ($data['packages'] as $package) {
120
            $this->linkedPackages[] = $this->transformer->load($package);
121
        }
122
    }
123
124
    private function findIndex(LinkedPackage $package): ?int
125
    {
126
        foreach ($this->linkedPackages as $index => $linkedPackage) {
127
            if ($linkedPackage->getName() === $package->getName()) {
128
                return $index;
129
            }
130
        }
131
132
        return null;
133
    }
134
}
135