Completed
Push — master ( 16ebaf...a3d07b )
by Arjay
14:09
created

Repository::scan()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
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 0
dl 0
loc 7
rs 9.4285
1
<?php
2
3
namespace Yajra\CMS\Theme;
4
5
use Illuminate\Contracts\Config\Repository as Config;
6
use Illuminate\Foundation\Validation\ValidatesRequests;
7
use Illuminate\Support\Collection;
8
use Symfony\Component\Finder\Finder;
9
10
class Repository
11
{
12
    use ValidatesRequests;
13
14
    /**
15
     * Registered themes collection.
16
     *
17
     * @var array
18
     */
19
    protected $themes = [];
20
21
    /**
22
     * @var \Symfony\Component\Finder\Finder
23
     */
24
    protected $finder;
25
26
    /**
27
     * @var \Illuminate\Contracts\Config\Repository
28
     */
29
    protected $config;
30
31
    /**
32
     * Repository constructor.
33
     *
34
     * @param \Symfony\Component\Finder\Finder $finder
35
     * @param Config $config
36
     */
37
    public function __construct(Finder $finder, Config $config)
38
    {
39
        $this->finder = $finder;
40
        $this->config = $config;
41
    }
42
43
    /**
44
     * Scan themes directory.
45
     */
46
    public function scan()
47
    {
48
        $files = $this->finder->create()->in($this->config->get('theme.path'))->name('theme.json');
49
        foreach ($files as $file) {
50
            $this->register($file);
51
        }
52
    }
53
54
    /**
55
     * Register theme.json file.
56
     *
57
     * @param \SplFileInfo $file
58
     * @throws \Exception
59
     */
60
    public function register($file)
61
    {
62
        $theme = json_decode(file_get_contents($file), true);
63
64
        $validator = $this->getValidationFactory()->make($theme, [
65
            'name'        => 'required',
66
            'theme'       => 'required',
67
            'version'     => 'required',
68
            'description' => 'required',
69
            'positions'   => 'required',
70
        ]);
71
72
        if ($validator->fails()) {
73
            throw new \Exception('Invalid theme configuration: ' . $file->getRealPath());
74
        }
75
76
        $this->themes[$theme['theme']] = new Theme(
77
            $theme['name'],
78
            $theme['theme'],
79
            $theme['version'],
80
            $theme['description'],
81
            (array) $theme['positions'],
82
            $theme
83
        );
84
    }
85
86
    /**
87
     * Find or fail a theme.
88
     *
89
     * @param string $theme
90
     * @return \Yajra\CMS\Theme\Theme
91
     * @throws \Yajra\CMS\Theme\NotFoundException
92
     */
93
    public function findOrFail($theme)
94
    {
95
        if (in_array($theme, array_keys($this->themes))) {
96
            return $this->themes[$theme];
97
        }
98
99
        throw new NotFoundException('Theme not found!');
100
    }
101
102
    /**
103
     * Get all themes.
104
     *
105
     * @return \Illuminate\Support\Collection
106
     */
107
    public function all()
108
    {
109
        return new Collection($this->themes);
110
    }
111
112
    /**
113
     * Get current frontend theme.
114
     *
115
     * @return \Yajra\CMS\Theme\Theme
116
     * @throws \Yajra\CMS\Theme\NotFoundException
117
     */
118
    public function current()
119
    {
120
        return $this->findOrFail($this->config->get('theme.frontend'));
121
    }
122
}
123