CollectionRepository   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 6

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A scan() 0 7 2
A getBasePath() 0 4 1
B register() 0 27 2
A all() 0 4 1
A current() 0 4 1
A findOrFail() 0 8 2
A uninstall() 0 8 1
A getDirectoryPath() 0 4 1
1
<?php
2
3
namespace Yajra\CMS\Themes\Repositories;
4
5
use Illuminate\Contracts\Config\Repository as Config;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Foundation\Validation\ValidatesRequests;
8
use Illuminate\Support\Collection;
9
use Symfony\Component\Finder\Finder;
10
use Yajra\CMS\Themes\Exceptions\ThemeNotFoundException;
11
use Yajra\CMS\Themes\Theme;
12
13
class CollectionRepository implements Repository
14
{
15
    use ValidatesRequests;
16
17
    /**
18
     * Registered themes collection.
19
     *
20
     * @var array
21
     */
22
    protected $themes = [];
23
24
    /**
25
     * @var \Symfony\Component\Finder\Finder
26
     */
27
    protected $finder;
28
29
    /**
30
     * @var \Illuminate\Contracts\Config\Repository
31
     */
32
    protected $config;
33
34
    /**
35
     * Repository constructor.
36
     *
37
     * @param \Symfony\Component\Finder\Finder $finder
38
     * @param Config $config
39
     */
40
    public function __construct(Finder $finder, Config $config)
41
    {
42
        $this->finder = $finder;
43
        $this->config = $config;
44
    }
45
46
    /**
47
     * Scan themes directory.
48
     */
49
    public function scan()
50
    {
51
        $files = $this->finder->create()->in($this->getBasePath())->name('theme.json');
52
        foreach ($files as $file) {
53
            $this->register($file);
54
        }
55
    }
56
57
    /**
58
     * Get themes base path.
59
     *
60
     * @return string
61
     */
62
    public function getBasePath()
63
    {
64
        return $this->config->get('themes.path.base');
65
    }
66
67
    /**
68
     * Register theme.json file.
69
     *
70
     * @param \SplFileInfo $file
71
     * @throws \Exception
72
     */
73
    public function register($file)
74
    {
75
        $theme = json_decode(file_get_contents($file), true);
76
77
        $validator = $this->getValidationFactory()->make($theme, [
78
            'name'        => 'required',
79
            'theme'       => 'required',
80
            'type'        => 'required',
81
            'version'     => 'required',
82
            'description' => 'required',
83
            'positions'   => 'required',
84
        ]);
85
86
        if ($validator->fails()) {
87
            throw new \Exception('Invalid theme configuration: ' . $file->getRealPath());
88
        }
89
90
        $this->themes[$theme['type'] . '.' . $theme['theme']] = new Theme(
91
            $theme['name'],
92
            $theme['theme'],
93
            $theme['type'],
94
            $theme['version'],
95
            $theme['description'],
96
            (array) $theme['positions'],
97
            $theme
98
        );
99
    }
100
101
    /**
102
     * Get all themes.
103
     *
104
     * @return \Illuminate\Support\Collection
105
     */
106
    public function all()
107
    {
108
        return new Collection($this->themes);
109
    }
110
111
    /**
112
     * Get current frontend theme.
113
     *
114
     * @return \Yajra\CMS\Themes\Theme
115
     * @throws \Yajra\CMS\Themes\Exceptions\ThemeNotFoundException
116
     */
117
    public function current()
118
    {
119
        return $this->findOrFail($this->config->get('themes.frontend'));
120
    }
121
122
    /**
123
     * Find or fail a theme.
124
     *
125
     * @param string $theme
126
     * @param string $type
127
     * @return \Yajra\CMS\Themes\Theme
128
     * @throws \Yajra\CMS\Themes\Exceptions\ThemeNotFoundException
129
     */
130
    public function findOrFail($theme, $type = 'frontend')
131
    {
132
        if (in_array($type . '.' . $theme, array_keys($this->themes))) {
133
            return $this->themes[$type . '.' . $theme];
134
        }
135
136
        throw new ThemeNotFoundException('Theme not found!');
137
    }
138
139
    /**
140
     * Uninstall a theme.
141
     *
142
     * @param string $theme
143
     * @return bool
144
     */
145
    public function uninstall($theme)
146
    {
147
        /** @var Filesystem $filesystem */
148
        $filesystem = app(Filesystem::class);
149
        $dir        = $this->getDirectoryPath($theme);
150
151
        return $filesystem->deleteDirectory($dir);
152
    }
153
154
    /**
155
     * Get directory path of the theme.
156
     *
157
     * @param string $theme
158
     * @return string
159
     */
160
    public function getDirectoryPath($theme)
161
    {
162
        return $this->getBasePath() . DIRECTORY_SEPARATOR . $theme;
163
    }
164
}
165