ThemeManager::allPublicThemes()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 8.7624
cc 5
eloc 12
nc 5
nop 0
1
<?php namespace Modules\Core\Foundation\Theme;
2
3
use Illuminate\Contracts\Foundation\Application;
4
use Illuminate\Support\Str;
5
6
class ThemeManager implements \Countable
7
{
8
    /**
9
     * @var Application
10
     */
11
    private $app;
12
    /**
13
     * @var string Path to scan for themes
14
     */
15
    private $path;
16
17
    /**
18
     * @param Application $app
19
     * @param $path
20
     */
21
    public function __construct(Application $app, $path)
22
    {
23
        $this->app = $app;
24
        $this->path = $path;
25
    }
26
27
    /**
28
     * @param  string     $name
29
     * @return Theme|null
30
     */
31
    public function find($name)
32
    {
33
        foreach ($this->all() as $theme) {
34
            if ($theme->getLowerName() == strtolower($name)) {
35
                return $theme;
36
            }
37
        }
38
39
        return;
40
    }
41
42
    /**
43
     * Return all available themes
44
     * @return array
45
     */
46
    public function all()
47
    {
48
        $themes = [];
49
        if (!$this->getFinder()->isDirectory($this->path)) {
50
            return $themes;
51
        }
52
53
        $directories = $this->getDirectories();
54
55
        foreach ($directories as $theme) {
56
            if (Str::startsWith($name = basename($theme), '.')) {
57
                continue;
58
            }
59
            $themes[$name] = new Theme($name, $theme);
60
        }
61
62
        return $themes;
63
    }
64
65
    /**
66
     * Get only the public themes
67
     * @return array
68
     */
69
    public function allPublicThemes()
70
    {
71
        $themes = [];
72
        if (!$this->getFinder()->isDirectory($this->path)) {
73
            return $themes;
74
        }
75
76
        $directories = $this->getDirectories();
77
78
        foreach ($directories as $theme) {
79
            if (Str::startsWith($name = basename($theme), '.')) {
80
                continue;
81
            }
82
            $themeJson = $this->getThemeJsonFile($theme);
83
            if ($this->isFrontendTheme($themeJson)) {
84
                $themes[$name] = new Theme($name, $theme);
85
            }
86
        }
87
88
        return $themes;
89
    }
90
91
    /**
92
     * Get the theme directories
93
     * @return array
94
     */
95
    private function getDirectories()
96
    {
97
        return $this->getFinder()->directories($this->path);
98
    }
99
100
    /**
101
     * Return the theme assets path
102
     * @param  string $theme
103
     * @return string
104
     */
105
    public function getAssetPath($theme)
106
    {
107
        return public_path($this->getConfig()->get('themify.themes_assets_path') . '/' . $theme);
108
    }
109
110
    /**
111
     * @return \Illuminate\Filesystem\Filesystem
112
     */
113
    protected function getFinder()
114
    {
115
        return $this->app['files'];
116
    }
117
118
    /**
119
     * @return \Illuminate\Config\Repository
120
     */
121
    protected function getConfig()
122
    {
123
        return $this->app['config'];
124
    }
125
126
    /**
127
     * Counts all themes
128
     */
129
    public function count()
130
    {
131
        return count($this->all());
132
    }
133
134
    /**
135
     * Returns the theme json file
136
     * @param $theme
137
     * @return string
138
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
139
     */
140
    private function getThemeJsonFile($theme)
141
    {
142
        return json_decode($this->getFinder()->get("$theme/theme.json"));
143
    }
144
145
    /**
146
     * @param $themeJson
147
     * @return bool
148
     */
149
    private function isFrontendTheme($themeJson)
150
    {
151
        return isset($themeJson->type) && $themeJson->type !== 'frontend' ? false : true;
152
    }
153
}
154