InitBackpackFormInstallation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 35
c 1
b 0
f 0
dl 0
loc 98
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addSidebarItem() 0 7 1
A handle() 0 30 2
A addSidebarItemIfNotExists() 0 22 4
1
<?php
2
3
namespace MedianetDev\BackpackForm\Console\Commands;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Artisan;
8
use Illuminate\Support\Facades\File;
9
10
class InitBackpackFormInstallation extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'backpack-form:installation';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Script call after installation via composer';
25
26
    /**
27
     * Execute the console command.
28
     *
29
     * @return mixed
30
     */
31
    public function handle()
32
    {
33
        $name = __('medianet-dev.backpack-form::formbuilder.labels.entity');
34
        $nameTitle = ucfirst(Str::camel($name));
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array and array; however, parameter $value of Illuminate\Support\Str::camel() 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

34
        $nameTitle = ucfirst(Str::camel(/** @scrutinizer ignore-type */ $name));
Loading history...
Unused Code introduced by
The assignment to $nameTitle is dead and can be removed.
Loading history...
35
        $nameKebab = 'formbuilder';
36
        $namePlural = ucfirst(__('medianet-dev.backpack-form::formbuilder.labels.entities_form'));
0 ignored issues
show
Bug introduced by
It seems like __('medianet-dev.backpac....labels.entities_form') can also be of type array and array; however, parameter $string of ucfirst() 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

36
        $namePlural = ucfirst(/** @scrutinizer ignore-type */ __('medianet-dev.backpack-form::formbuilder.labels.entities_form'));
Loading history...
Unused Code introduced by
The assignment to $namePlural is dead and can be removed.
Loading history...
37
38
        $this->call('migrate');
39
40
        $this->call('vendor:publish', [
41
            '--provider' => "MedianetDev\BackpackForm\AddonServiceProvider",
42
            '--tag' => "config"
43
        ]);
44
        $this->call('vendor:publish', [
45
            '--provider' => "MedianetDev\BackpackForm\AddonServiceProvider",
46
            '--tag' => "assets"
47
        ]);
48
49
        $this->call('vendor:publish', [
50
            '--provider' => "MedianetDev\BackpackForm\AddonServiceProvider",
51
            '--tag' => "views"
52
        ]);
53
54
        // Check if sidebar item already exists before adding it
55
        $this->addSidebarItemIfNotExists($nameKebab);
56
57
        // if the application uses cached routes, we should rebuild the cache so the previous added route will
58
        // be acessible without manually clearing the route cache.
59
        if (app()->routesAreCached()) {
0 ignored issues
show
introduced by
The method routesAreCached() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

59
        if (app()->/** @scrutinizer ignore-call */ routesAreCached()) {
Loading history...
60
            $this->call('route:cache');
61
        }
62
    }
63
64
    /**
65
     * Add the sidebar item only if it doesn't already exist in the sidebar_content file
66
     *
67
     * @param string $nameKebab
68
     * @return void
69
     */
70
    protected function addSidebarItemIfNotExists($nameKebab)
71
    {
72
        // Path to the Backpack sidebar_content file
73
        $sidebarPath = resource_path('views/vendor/backpack/base/inc/sidebar_content.blade.php');
74
75
        // Check if the sidebar content file exists
76
        if (!File::exists($sidebarPath)) {
77
            $this->warn('Sidebar content file not found. Creating sidebar item anyway.');
78
            $this->addSidebarItem($nameKebab);
79
            return;
80
        }
81
82
        // Get the content of the sidebar file
83
        $sidebarContent = File::get($sidebarPath);
84
85
        // Check if the sidebar already contains a link to the formbuilder
86
        if (Str::contains($sidebarContent, "backpack_url('$nameKebab')") ||
87
            Str::contains($sidebarContent, "{{ __('sidebar.forms_list') }}")) {
88
            $this->info('Formbuilder sidebar item already exists. Skipping.');
89
        } else {
90
            $this->info('Adding Formbuilder sidebar item.');
91
            $this->addSidebarItem($nameKebab);
92
        }
93
    }
94
95
    /**
96
     * Add the sidebar item
97
     *
98
     * @param string $nameKebab
99
     * @return void
100
     */
101
    protected function addSidebarItem($nameKebab)
102
    {
103
        $this->call('backpack:add-sidebar-content', [
104
            'code' => "
105
                @canany(['create_form', 'list_form', 'update_form', 'delete_form'])
106
                    <li class='nav-item'>
107
                        <a class='nav-link' href='{{ backpack_url('$nameKebab') }}'>
108
                            <i class='nav-icon la la-database'></i>
109
                            {{ __('sidebar.forms_list') }}
110
                        </a>
111
                    </li>
112
                @endcanany
113
            ",
114
        ]);
115
    }
116
}
117