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

AssetServiceProvider::getAssetUrlByname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
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