Passed
Push — master ( 136870...398928 )
by Quentin
53:06 queued 43:01
created

BlockCollection::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 27
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
    public function __construct($items = [])
29
    {
30
        parent::__construct($items);
31
32
        $this->fileSystem = app(Filesystem::class);
33
34
        $this->missingDirectories = collect();
35
36
        $this->load();
37
    }
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
    public function findByName($search, $sources = [])
51
    {
52
        return $this->collect()
53
            ->filter(function ($block) use ($search, $sources) {
54
                return $block->name == $search &&
55
                    (blank($sources) ||
56
                    collect($sources)->contains($block->source));
57
            })
58
            ->sortBy(function ($block) {
59
                return $block->source === 'app' ? 0 : 1;
60
            })
61
            ->first();
62
    }
63
64
    /**
65
     * @return \Illuminate\Support\Collection
66
     */
67
    public function getBlocks()
68
    {
69
        return $this->collect()
70
            ->filter(function ($block) {
71
                return $block->type === Block::TYPE_BLOCK;
72
            })
73
            ->values();
74
    }
75
76
    /**
77
     * @return \Illuminate\Support\Collection
78
     */
79
    public function getBlockList()
80
    {
81
        return $this->getBlocks()->map(function (Block $block) {
82
            return $block->toList();
83
        });
84
    }
85
86
    /**
87
     * @return \Illuminate\Support\Collection
88
     */
89
    public function getMissingDirectories()
90
    {
91
        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
    public function readBlocks($directory, $source, $type = null)
101
    {
102
        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
            return new Block($file, $type, $source);
112
        });
113
    }
114
115
    /**
116
     * @return $this
117
     */
118
    public function generatePaths()
119
    {
120
        $this->paths = collect(
121
            config('twill.block_editor.directories.source.blocks')
122
        )
123
            ->map(function ($path) {
124
                $path['type'] = Block::TYPE_BLOCK;
125
126
                return $path;
127
            })
128
            ->merge(
129
                collect(
130
                    config('twill.block_editor.directories.source.repeaters')
131
                )->map(function ($path) {
132
                    $path['type'] = Block::TYPE_REPEATER;
133
134
                    return $path;
135
                })
136
            );
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param Block $block
143
     * @return string
144
     */
145
    public function detectCustomSources(Block $block)
146
    {
147
        if ($block->source === Block::SOURCE_APP) {
148
            if (
149
                $this->collect()
150
                ->where('fileName', $block->getFileName())
151
                ->where('source', Block::SOURCE_TWILL)
152
                ->isNotEmpty()
153
            ) {
154
                return Block::SOURCE_CUSTOM;
155
            }
156
        }
157
158
        return $block->source;
159
    }
160
161
    /**
162
     * @return $this
163
     */
164
    public function load()
165
    {
166
        $this->generatePaths();
167
168
        $this->items = collect($this->paths)
169
            ->reduce(function (Collection $keep, $path) {
170
                $this->readBlocks(
171
                    $path['path'],
172
                    $path['source'],
173
                    $path['type']
174
                )->each(function ($block) use ($keep) {
175
                    $keep->push($block);
176
177
                    return $keep;
178
                });
179
180
                return $keep;
181
            }, collect())
182
            ->toArray();
183
184
        $this->items = $this->collect()
185
            ->each(function (Block $block) {
186
                $block->setSource($this->detectCustomSources($block));
187
            })
188
            ->toArray();
189
190
        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
    public function collect()
215
    {
216
        return collect($this);
217
    }
218
219
    /**
220
     * @return \Illuminate\Support\Collection
221
     */
222
    public function getRepeaters()
223
    {
224
        return $this->collect()
225
            ->filter(function ($block) {
226
                return $block->type === Block::TYPE_REPEATER;
227
            })
228
            ->values();
229
    }
230
231
    /**
232
     * @return \Illuminate\Support\Collection
233
     */
234
    public function getRepeaterList()
235
    {
236
        return $this->getRepeaters()->map(function (Block $block) {
237
            return $block->toList();
238
        });
239
    }
240
}
241