Completed
Push — master ( e5ba92...7a1e6c )
by Arjay
13:54
created

AssetServiceProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 21
Bugs 1 Features 4
Metric Value
c 21
b 1
f 4
dl 16
loc 96
rs 10
wmc 11
lcom 1
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 11 2
A register() 0 3 1
A addAdminAssets() 0 6 2
A requireAdminDefaultAssets() 0 6 2
A assetJs() 8 8 1
A assetCss() 8 8 1
A strParser() 0 4 1
A getAssetUrlByname() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Yajra\CMS\Providers;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\ServiceProvider;
7
use Roumen\Asset\Asset;
8
use Yajra\CMS\Entities\Configuration;
9
use Yajra\CMS\Entities\FileAsset;
10
use Illuminate\Database\QueryException;
11
12
13
/**
14
 * Class AssetServiceProvider
15
 *
16
 * @package Yajra\CMS\Providers
17
 */
18
class AssetServiceProvider extends ServiceProvider
19
{
20
    /**
21
     * Bootstrap the application services.
22
     */
23
    public function boot()
24
    {
25
        try {
26
            $this->addAdminAssets();
27
            $this->requireAdminDefaultAssets();
28
            $this->assetJs();
29
            $this->assetCss();
30
        } catch (QueryException $e) {
31
            // \\_(",)_//
32
        }
33
    }
34
35
    /**
36
     * Register the application services.
37
     *
38
     * @return void
39
     */
40
    public function register()
41
    {
42
    }
43
44
    /**
45
     * Load admin assets.
46
     */
47
    protected function addAdminAssets()
48
    {
49
        foreach (config('asset.admin_assets') as $asset) {
50
            Asset::add($this->getAssetUrlByname($asset)->url);
51
        }
52
    }
53
54
    /**
55
     * Load require admin default assets.
56
     */
57
    protected function requireAdminDefaultAssets()
58
    {
59
        foreach (config('asset.admin_required_assets', []) as $asset => $requiredValue) {
60
            Asset::add($requiredValue);
61
        }
62
    }
63
64
    /**
65
     * Load js assets.
66
     *
67
     * @return string
68
     */
69 View Code Duplication
    protected function assetJs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        Blade::directive('assetJs', function ($asset) {
72
            $asset = $this->getAssetUrlByname($this->strParser($asset . '.js'));
73
74
            return '<?php echo "<script src=\"' . $asset->url . '\"></script>"; ?>';
75
        });
76
    }
77
78
    /**
79
     * Load css assets.
80
     *
81
     * @return string
82
     */
83 View Code Duplication
    protected function assetCss()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        Blade::directive('assetCss', function ($asset) {
86
            $asset = $this->getAssetUrlByname($this->strParser($asset . '.css'));
87
88
            return '<?php echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"' . $asset->url . '\">"; ?>';
89
        });
90
    }
91
92
    /**
93
     * @param string $str
94
     * @return string
95
     */
96
    private function strParser($str)
97
    {
98
        return str_replace("'", '', str_replace(['(', ')'], '', $str));
99
    }
100
101
    /**
102
     * Get url by asset name.
103
     *
104
     * @param string $asset
105
     * @return FileAsset
106
     */
107
    private function getAssetUrlByname($asset)
108
    {
109
        return FileAsset::where('name', $asset)
110
                        ->where('category', Configuration::key('asset.default'))
111
                        ->first();
112
    }
113
}
114