Passed
Push — main ( 03ed8c...5e2119 )
by PRATIK
05:01 queued 11s
created

BlockRepository::indexBlock()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 3
eloc 5
c 2
b 1
f 1
nc 4
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Adminetic\Website\Repositories;
4
5
use Adminetic\Website\Contracts\BlockRepositoryInterface;
6
use Adminetic\Website\Http\Requests\BlockRequest;
0 ignored issues
show
Bug introduced by
The type Adminetic\Website\Http\Requests\BlockRequest 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...
7
use Adminetic\Website\Models\Admin\Block;
8
use Illuminate\Support\Facades\Cache;
9
use Intervention\Image\Facades\Image;
0 ignored issues
show
Bug introduced by
The type Intervention\Image\Facades\Image 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...
10
11
class BlockRepository implements BlockRepositoryInterface
12
{
13
    // Block Index
14
    public function indexBlock()
15
    {
16
        $blocks = config('adminetic.caching', true)
17
            ? (Cache::has('blocks') ? Cache::get('blocks') : Cache::rememberForever('blocks', function () {
18
                return Block::orderBy('position')->get();
19
            }))
20
            : Block::orderBy('position')->get();
21
22
        return compact('blocks');
23
    }
24
25
    // Block Create
26
    public function createBlock()
27
    {
28
        $block_domains = Block::latest()->pluck('page')->toArray();
29
        $versions = Block::latest()->pluck('version')->toArray();
30
        $block_types = Block::latest()->pluck('type')->toArray();
31
32
        return compact('block_domains', 'versions', 'block_types');
33
    }
34
35
    // Block Store
36
    public function storeBlock(BlockRequest $request)
37
    {
38
        $block = Block::create($request->validated());
39
        $this->versionActivationControl($block);
40
        $this->uploadImage($block);
41
    }
42
43
    // Block Show
44
    public function showBlock(Block $block)
45
    {
46
        return compact('block');
47
    }
48
49
    // Block Edit
50
    public function editBlock(Block $block)
51
    {
52
        $block_domains = Block::latest()->pluck('page')->toArray();
53
        $versions = Block::latest()->pluck('version')->toArray();
54
        $block_types = Block::latest()->pluck('type')->toArray();
55
56
        return compact('block', 'block_domains', 'versions', 'block_types');
57
    }
58
59
    // Block Update
60
    public function updateBlock(BlockRequest $request, Block $block)
61
    {
62
        $block->update($request->validated());
63
        $this->versionActivationControl($block);
64
        $this->uploadImage($block);
65
    }
66
67
    // Block Destroy
68
    public function destroyBlock(Block $block)
69
    {
70
        isset($block->image) ? deleteImage($block->image) : '';
0 ignored issues
show
Bug introduced by
The function deleteImage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

70
        isset($block->image) ? /** @scrutinizer ignore-call */ deleteImage($block->image) : '';
Loading history...
71
        $block->delete();
72
    }
73
74
    // Image Upload
75
    protected function uploadImage(Block $block)
76
    {
77
        if (request()->has('image')) {
78
            $block->update([
79
                'image' => request()->image->store('website/block', 'public'),
80
            ]);
81
            $image = Image::make(request()->file('image')->getRealPath());
82
            $image->save(public_path('storage/' . $block->image));
83
        }
84
    }
85
86
    // Version Activation Control
87
    protected function versionActivationControl(Block $block)
88
    {
89
        $blocks = Block::where([
90
            ['id', '<>', $block->id],
91
            ['type', '=', $block->type],
92
            ['theme', '=', $block->theme],
93
        ])->pluck('id')->toArray();
94
95
        if ($block->active) {
96
            Block::whereIn('id', $blocks)->update(['active' => 0]);
97
        }
98
    }
99
}
100