Completed
Push — master ( a3d07b...f35753 )
by Arjay
14:10
created

Repository::getDirectoryPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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