Passed
Push — dependabot/npm_and_yarn/docs/w... ( a770a9...3a5b31 )
by
unknown
07:47
created

BlockCollection::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 1
rs 9.6666
c 1
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Services\Blocks;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
8
class BlockCollection extends Collection
9
{
10
    /**
11
     * @var \Illuminate\Support\Collection
12
     */
13
    protected $paths;
14
15
    /**
16
     * @var \Illuminate\Filesystem\Filesystem
17
     */
18
    protected $fileSystem;
19
20
    /**
21
     * @var \Illuminate\Support\Collection
22
     */
23
    private $missingDirectories;
24
25
    /**
26
     * @param mixed $items
27
     */
28 69
    public function __construct($items = [])
29
    {
30 69
        parent::__construct($items);
31
32 69
        $this->fileSystem = app(Filesystem::class);
33
34 69
        $this->missingDirectories = collect();
35
36 69
        $this->load();
37 69
    }
38
39
    private function addMissingDirectory($directory)
40
    {
41
        $this->missingDirectories->push($directory);
42
    }
43
44
    /**
45
     * @param $search
46
     * @param array $sources
47
     * @return mixed
48
     * @throws \Exception
49
     */
50 5
    public function findByName($search, $sources = [])
51
    {
52 5
        return $this->collect()
53
            ->filter(function ($block) use ($search, $sources) {
54 5
                return $block->name == $search &&
55 4
                    (blank($sources) ||
56 5
                    collect($sources)->contains($block->source));
57 5
            })
58
            ->sortBy(function ($block) {
59 4
                return $block->source === 'app' ? 0 : 1;
60 5
            })
61 5
            ->first();
62
    }
63
64
    /**
65
     * @return \Illuminate\Support\Collection
66
     */
67 7
    public function getBlocks()
68
    {
69 7
        return $this->collect()
70
            ->filter(function ($block) {
71 7
                return $block->type === Block::TYPE_BLOCK;
72 7
            })
73 7
            ->values();
74
    }
75
76
    /**
77
     * @return \Illuminate\Support\Collection
78
     */
79 7
    public function getBlockList()
80
    {
81
        return $this->getBlocks()->map(function (Block $block) {
82 7
            return $block->toList();
83 7
        });
84
    }
85
86
    /**
87
     * @return \Illuminate\Support\Collection
88
     */
89 3
    public function getMissingDirectories()
90
    {
91 3
        return $this->missingDirectories;
92
    }
93
94
    /**
95
     * @param $directory
96
     * @param $source
97
     * @param null $type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type is correct as it would always require null to be passed?
Loading history...
98
     * @return \Illuminate\Support\Collection
99
     */
100 69
    public function readBlocks($directory, $source, $type = null)
101
    {
102 69
        if (!$this->fileSystem->exists($directory)) {
103
            $this->addMissingDirectory($directory);
104
105
            return collect();
106
        }
107
108
        return collect($this->fileSystem->files($directory))->map(function (
109
            $file
110
        ) use ($source, $type) {
111 69
            return new Block($file, $type, $source);
112 69
        });
113
    }
114
115
    /**
116
     * @return $this
117
     */
118 69
    public function generatePaths()
119
    {
120 69
        $this->paths = collect(
121 69
            config('twill.block_editor.directories.source.blocks')
122
        )
123
            ->map(function ($path) {
124 69
                $path['type'] = Block::TYPE_BLOCK;
125
126 69
                return $path;
127 69
            })
128 69
            ->merge(
129 69
                collect(
130 69
                    config('twill.block_editor.directories.source.repeaters')
131
                )->map(function ($path) {
132 69
                    $path['type'] = Block::TYPE_REPEATER;
133
134 69
                    return $path;
135 69
                })
136
            );
137
138 69
        return $this;
139
    }
140
141
    /**
142
     * @param Block $block
143
     * @return string
144
     */
145 69
    public function detectCustomSources(Block $block)
146
    {
147 69
        if ($block->source === Block::SOURCE_APP) {
148
            if (
149 69
                $this->collect()
150 69
                ->where('fileName', $block->getFileName())
151 69
                ->where('source', Block::SOURCE_TWILL)
152 69
                ->isNotEmpty()
153
            ) {
154 69
                return Block::SOURCE_CUSTOM;
155
            }
156
        }
157
158 69
        return $block->source;
159
    }
160
161
    /**
162
     * @return $this
163
     */
164 69
    public function load()
165
    {
166 69
        $this->generatePaths();
167
168 69
        $this->items = collect($this->paths)
169
            ->reduce(function (Collection $keep, $path) {
170 69
                $this->readBlocks(
171 69
                    $path['path'],
172 69
                    $path['source'],
173 69
                    $path['type']
174
                )->each(function ($block) use ($keep) {
175 69
                    $keep->push($block);
176
177 69
                    return $keep;
178 69
                });
179
180 69
                return $keep;
181 69
            }, collect())
182 69
            ->toArray();
183
184 69
        $this->items = $this->collect()
185
            ->each(function (Block $block) {
186 69
                $block->setSource($this->detectCustomSources($block));
187 69
            })
188 69
            ->toArray();
189
190 69
        return $this;
191
    }
192
193
    /**
194
     * @return array
195
     */
196
    public function toArray()
197
    {
198
        return $this->list()->toArray();
199
    }
200
201
    /**
202
     * @return \Illuminate\Support\Collection
203
     */
204
    public function list()
205
    {
206
        return $this->collect()->map(function (Block $block) {
207
            return $block->toList();
208
        });
209
    }
210
211
    /**
212
     * @return \Illuminate\Support\Collection
213
     */
214 69
    public function collect()
215
    {
216 69
        return collect($this);
217
    }
218
219
    /**
220
     * @return \Illuminate\Support\Collection
221
     */
222 5
    public function getRepeaters()
223
    {
224 5
        return $this->collect()
225
            ->filter(function ($block) {
226 5
                return $block->type === Block::TYPE_REPEATER;
227 5
            })
228 5
            ->values();
229
    }
230
231
    /**
232
     * @return \Illuminate\Support\Collection
233
     */
234 5
    public function getRepeaterList()
235
    {
236
        return $this->getRepeaters()->map(function (Block $block) {
237 5
            return $block->toList();
238 5
        });
239
    }
240
}
241