Issues (31)

src/Resource/ResourceRegistryBuilder.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Resource;
10
11
use Symfony\Component\Config\FileLocatorInterface;
12
use Symfony\Component\Yaml\Yaml;
13
14
final class ResourceRegistryBuilder
15
{
16
    private FileLocatorInterface $configLocator;
17
18
    /** @var string[][][] */
19
    private array $registry;
20
    private ?string $selectedResource = null;
21
22 5
    public function __construct(FileLocatorInterface $configLocator)
23
    {
24 5
        $this->configLocator = $configLocator;
25 5
        $this->load();
26 5
    }
27
28
    /**
29
     * Loads resources registry
30
     *
31
     * @return $this
32
     */
33 5
    public function load(): self
34
    {
35 5
        $this->registry = Yaml::parse(file_get_contents($this->configLocator->locate('resources.yaml')));
0 ignored issues
show
It seems like $this->configLocator->locate('resources.yaml') can also be of type array; however, parameter $filename of file_get_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $this->registry = Yaml::parse(file_get_contents(/** @scrutinizer ignore-type */ $this->configLocator->locate('resources.yaml')));
Loading history...
36
37 5
        return $this;
38
    }
39
40
    /**
41
     * Returns TRUE if resource record exists
42
     *
43
     * @param string $resource
44
     * @return bool
45
     */
46 4
    public function hasResource(string $resource): bool
47
    {
48 4
        return true === array_key_exists($resource, $this->registry['resources']);
49
    }
50
51
    /**
52
     * Selects resource record - if it does not exist, new one is appended
53
     *
54
     * @param string $resource
55
     * @return $this
56
     */
57 2
    public function selectResource(string $resource): self
58
    {
59 2
        if (false === $this->hasResource($resource)) {
60
            return $this->appendResource($resource)->selectResource($resource);
61
        }
62
63 2
        $this->selectedResource = $resource;
64
65 2
        return $this;
66
    }
67
68
    /**
69
     * Appends new resource record
70
     *
71
     * @param string $resource
72
     * @return $this
73
     */
74 1
    public function appendResource(string $resource): self
75
    {
76 1
        $this->registry['resources'][$resource] = [];
77
78 1
        return $this;
79
    }
80
81
    /**
82
     * Merges provided operations with already registered to the resource
83
     *
84
     * @param string ...$operations
85
     * @return $this
86
     * @throws \BadMethodCallException
87
     */
88 3
    public function mergeOperations(string ...$operations): self
89
    {
90 3
        if (null === $this->selectedResource) {
91 1
            throw new \BadMethodCallException('There is no selected resource to merge operations into. See selectResource().');
92
        }
93
94 2
        $this->registry['resources'][$this->selectedResource] = array_values(array_unique(array_merge(
95 2
            $this->registry['resources'][$this->selectedResource],
96
            $operations,
97
        )));
98
99 2
        return $this;
100
    }
101
102
    /**
103
     * Saves registry
104
     *
105
     * @return $this
106
     */
107
    public function save(): self
108
    {
109
        file_put_contents(
110
            $this->configLocator->locate('resources.yaml'),
0 ignored issues
show
It seems like $this->configLocator->locate('resources.yaml') can also be of type array; however, parameter $filename of file_put_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
            /** @scrutinizer ignore-type */ $this->configLocator->locate('resources.yaml'),
Loading history...
111
            Yaml::dump($this->registry, 3, 2)
112
        );
113
114
        return $this;
115
    }
116
117
    /**
118
     * Returns current state of registry
119
     *
120
     * @return string[][]
121
     */
122 2
    public function getRegistry(): array
123
    {
124 2
        return $this->registry['resources'];
125
    }
126
}
127