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

CollectionRepository::findOrFail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Yajra\CMS\Repositories\Theme;
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\Exceptions\ThemeNotFoundException;
11
use Yajra\CMS\Theme\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->config->get('theme.path'))->name('theme.json');
52
        foreach ($files as $file) {
53
            $this->register($file);
54
        }
55
    }
56
57
    /**
58
     * Register theme.json file.
59
     *
60
     * @param \SplFileInfo $file
61
     * @throws \Exception
62
     */
63
    public function register($file)
64
    {
65
        $theme = json_decode(file_get_contents($file), true);
66
67
        $validator = $this->getValidationFactory()->make($theme, [
68
            'name'        => 'required',
69
            'theme'       => 'required',
70
            'version'     => 'required',
71
            'description' => 'required',
72
            'positions'   => 'required',
73
        ]);
74
75
        if ($validator->fails()) {
76
            throw new \Exception('Invalid theme configuration: ' . $file->getRealPath());
77
        }
78
79
        $this->themes[$theme['theme']] = new Theme(
80
            $theme['name'],
81
            $theme['theme'],
82
            $theme['version'],
83
            $theme['description'],
84
            (array) $theme['positions'],
85
            $theme
86
        );
87
    }
88
89
    /**
90
     * Get all themes.
91
     *
92
     * @return \Illuminate\Support\Collection
93
     */
94
    public function all()
95
    {
96
        return new Collection($this->themes);
97
    }
98
99
    /**
100
     * Get current frontend theme.
101
     *
102
     * @return \Yajra\CMS\Theme\Theme
103
     * @throws \Yajra\CMS\Exceptions\ThemeNotFoundException
104
     */
105
    public function current()
106
    {
107
        return $this->findOrFail($this->config->get('theme.frontend'));
108
    }
109
110
    /**
111
     * Find or fail a theme.
112
     *
113
     * @param string $theme
114
     * @return \Yajra\CMS\Theme\Theme
115
     * @throws \Yajra\CMS\Exceptions\ThemeNotFoundException
116
     */
117
    public function findOrFail($theme)
118
    {
119
        if (in_array($theme, array_keys($this->themes))) {
120
            return $this->themes[$theme];
121
        }
122
123
        throw new ThemeNotFoundException('Theme not found!');
124
    }
125
126
    /**
127
     * Uninstall a theme.
128
     *
129
     * @param string $theme
130
     * @return bool
131
     */
132
    public function uninstall($theme)
133
    {
134
        /** @var Filesystem $filesystem */
135
        $filesystem = app(Filesystem::class);
136
        $dir        = $this->getDirectoryPath($theme);
137
138
        return $filesystem->deleteDirectory($dir);
139
    }
140
141
    /**
142
     * Get directory path of the theme.
143
     *
144
     * @param string $theme
145
     * @return string
146
     */
147
    public function getDirectoryPath($theme)
148
    {
149
        return $this->getBasePath() . DIRECTORY_SEPARATOR . $theme;
150
    }
151
152
    /**
153
     * Get themes base path.
154
     *
155
     * @return string
156
     */
157
    public function getBasePath()
158
    {
159
        return $this->config->get('theme.path');
160
    }
161
}
162