Fieldsets   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 37
c 3
b 0
f 0
dl 0
loc 204
rs 10
wmc 22

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getFileLocation() 0 3 1
A getDirLocation() 0 3 1
A fetch() 0 17 4
A rename() 0 3 1
A has() 0 3 1
A update() 0 9 2
A delete() 0 3 1
A copy() 0 3 1
A create() 0 9 2
A __construct() 0 10 3
A fetchAll() 0 22 5
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Flextype (http://flextype.org)
7
 * Founded by Sergey Romanenko and maintained by Flextype Community.
8
 */
9
10
namespace Flextype;
11
12
use Flextype\Component\Filesystem\Filesystem;
0 ignored issues
show
Bug introduced by
The type Flextype\Component\Filesystem\Filesystem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use function count;
14
use function rename;
15
16
class Fieldsets
17
{
18
    /**
19
     * Flextype Dependency Container
20
     */
21
    private $flextype;
22
23
    /**
24
     * Constructor
25
     *
26
     * @access public
27
     */
28
    public function __construct($flextype)
29
    {
30
        $this->flextype = $flextype;
31
32
        if (! Filesystem::has($this->getDirLocation())) {
33
            Filesystem::createDir($this->getDirLocation());
34
        }
35
36
        if (! Filesystem::has($this->getFileLocation('default'))) {
37
            Filesystem::copy(PATH['project'] . '/plugins/form/fieldsets/samples/default/default.yaml', $this->getFileLocation('default'));
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
        }
39
    }
40
41
    /**
42
     * Fetch fieldset
43
     *
44
     * @param string $id Fieldset id
45
     *
46
     * @return array|false The entry contents or false on failure.
47
     *
48
     * @access public
49
     */
50
    public function fetch(string $id)
51
    {
52
        $fieldset_file = $this->getFileLocation($id);
53
54
        if (Filesystem::has($fieldset_file)) {
55
            if ($fieldset_body = Filesystem::read($fieldset_file)) {
56
                if ($fieldset_decoded = $this->flextype['serializer']->decode($fieldset_body, 'yaml')) {
57
                    return $fieldset_decoded;
58
                }
59
60
                return false;
61
            }
62
63
            return false;
64
        }
65
66
        return false;
67
    }
68
69
    /**
70
     * Fetch all fieldsets
71
     *
72
     * @return array
73
     *
74
     * @access public
75
     */
76
    public function fetchAll() : array
77
    {
78
        // Init Fieldsets array
79
        $fieldsets = [];
80
81
        // Get fieldsets files
82
        $_fieldsets = Filesystem::listContents($this->getDirLocation());
83
84
        // If there is any fieldsets file then go...
85
        if (count($_fieldsets) > 0) {
86
            foreach ($_fieldsets as $fieldset) {
87
                if ($fieldset['type'] !== 'file' || $fieldset['extension'] !== 'yaml') {
88
                    continue;
89
                }
90
91
                $fieldset_content                 = $this->flextype['serializer']->decode(Filesystem::read($fieldset['path']), 'yaml');
92
                $fieldsets[$fieldset['basename']] = $fieldset_content['title'];
93
            }
94
        }
95
96
        // return fieldsets array
97
        return $fieldsets;
98
    }
99
100
    /**
101
     * Rename fieldset
102
     *
103
     * @param string $id     Fieldset id
104
     * @param string $new_id New fieldset id
105
     *
106
     * @return bool True on success, false on failure.
107
     *
108
     * @access public
109
     */
110
    public function rename(string $id, string $new_id) : bool
111
    {
112
        return rename($this->getFileLocation($id), $this->getFileLocation($new_id));
113
    }
114
115
    /**
116
     * Update fieldset
117
     *
118
     * @param string $id   Fieldset id
119
     * @param array  $data Fieldset data to save
120
     *
121
     * @return bool True on success, false on failure.
122
     *
123
     * @access public
124
     */
125
    public function update(string $id, array $data) : bool
126
    {
127
        $fieldset_file = $this->getFileLocation($id);
128
129
        if (Filesystem::has($fieldset_file)) {
130
            return Filesystem::write($fieldset_file, $this->flextype['serializer']->encode($data, 'yaml'));
131
        }
132
133
        return false;
134
    }
135
136
    /**
137
     * Create fieldset
138
     *
139
     * @param string $id   Fieldset id
140
     * @param array  $data Fieldset data to save
141
     *
142
     * @return bool True on success, false on failure.
143
     *
144
     * @access public
145
     */
146
    public function create(string $id, array $data) : bool
147
    {
148
        $fieldset_file = $this->getFileLocation($id);
149
150
        if (! Filesystem::has($fieldset_file)) {
151
            return Filesystem::write($fieldset_file, $this->flextype['serializer']->encode($data, 'yaml'));
152
        }
153
154
        return false;
155
    }
156
157
    /**
158
     * Delete fieldset
159
     *
160
     * @param string $id Fieldset id
161
     *
162
     * @return bool True on success, false on failure.
163
     *
164
     * @access public
165
     */
166
    public function delete(string $id) : bool
167
    {
168
        return Filesystem::delete($this->getFileLocation($id));
169
    }
170
171
    /**
172
     * Copy fieldset
173
     *
174
     * @param string $id     Fieldset id
175
     * @param string $new_id New fieldset id
176
     *
177
     * @return bool True on success, false on failure.
178
     *
179
     * @access public
180
     */
181
    public function copy(string $id, string $new_id) : bool
182
    {
183
        return Filesystem::copy($this->getFileLocation($id), $this->getFileLocation($new_id), false);
184
    }
185
186
    /**
187
     * Check whether fieldset exists.
188
     *
189
     * @param string $id Fieldset id
190
     *
191
     * @return bool True on success, false on failure.
192
     *
193
     * @access public
194
     */
195
    public function has(string $id) : bool
196
    {
197
        return Filesystem::has($this->getFileLocation($id));
198
    }
199
200
    /**
201
     * Helper method getDirLocation
202
     *
203
     * @access private
204
     */
205
    public function getDirLocation() : string
206
    {
207
        return PATH['project'] . '/fieldsets/';
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
208
    }
209
210
    /**
211
     * Helper method getFileLocation
212
     *
213
     * @param string $id Fieldsets id
214
     *
215
     * @access private
216
     */
217
    public function getFileLocation(string $id) : string
218
    {
219
        return PATH['project'] . '/fieldsets/' . $id . '.yaml';
0 ignored issues
show
Bug introduced by
The constant Flextype\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
220
    }
221
}
222