Passed
Push — master ( b6f366...fc6f44 )
by Karel
15:03
created

Site::module()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Chuckbe\Chuckcms\Chuck\Accessors;
4
5
use Chuckbe\Chuckcms\Chuck\SiteRepository;
6
use Chuckbe\Chuckcms\Chuck\ModuleRepository;
7
use Chuckbe\Chuckcms\Models\Site as SiteModel;
8
use Exception;
9
use Illuminate\Support\Facades\Schema;
10
11
class Site
12
{
13
    private $siteRepository;
14
    private $moduleRepository;
15
    private $currentSite;
16
    private $siteSettings;
17
    private $siteSupportedLocales;
18
19
    public function __construct(SiteModel $site, SiteRepository $siteRepository, ModuleRepository $moduleRepository) 
20
    {
21
        $this->currentSite = $this->getCurrentSite($site);
22
        $this->siteSettings = $this->getSiteSettings($site);
23
        $this->siteSupportedLocales = $this->getSiteSupportedLocales($site);
24
        $this->siteRepository = $siteRepository;
25
        $this->moduleRepository = $moduleRepository;
26
    }
27
28
    public static function forSite($site)
29
    {
30
        return new static($site, \App::make(SiteRepository::class));
0 ignored issues
show
Bug introduced by
The call to Chuckbe\Chuckcms\Chuck\A...ors\Site::__construct() has too few arguments starting with moduleRepository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        return /** @scrutinizer ignore-call */ new static($site, \App::make(SiteRepository::class));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
31
    }
32
33
    private function getSiteSettings(SiteModel $site)
34
    {
35
        $settings = $site->settings;
36
        return $settings;
37
    }
38
39
    private function getCurrentSite(SiteModel $site)
40
    {
41
        return $site;
42
    }
43
44
    private function getSiteSupportedLocales(SiteModel $site)
45
    {
46
        $settings = $site->settings;
47
        $locales = [];
48
        if (is_array($settings)) { 
0 ignored issues
show
introduced by
The condition is_array($settings) is always true.
Loading history...
49
            foreach (config('lang.allLocales') as $langKey => $langValue) {
50
                if (in_array($langKey, explode(',', $settings['lang']))) {
51
                    $locales[$langKey] = $langValue;
52
                }
53
            }
54
        } else {
55
            $allLocales = config('lang.allLocales');
56
            $locales['en'] = $allLocales['en'];
57
            $locales['nl'] = $allLocales['nl'];
58
        }
59
60
        return $locales;
61
    }
62
63
    public function getSettings()
64
    {
65
        $settings = $this->siteSettings;
66
        return $settings ? $settings : null;
67
    }
68
69
    public function getSetting($var)
70
    {
71
        $setting = $this->resolveSetting($var, $this->siteSettings);
72
        return $setting ? $setting : null;
73
    }
74
75
    public function getSite($var)
76
    {
77
        $setting = $this->resolveSiteAttribute($var, $this->currentSite);
78
        return $setting ? $setting : null;
79
    }
80
81
    public function getSupportedLocales()
82
    {
83
        $supportedLocales = $this->siteSupportedLocales;
84
        return $supportedLocales ? $supportedLocales : null;
85
    }
86
87
    public function getFeaturedLocale()
88
    {
89
        return config('lang.featuredLocale');
90
    }
91
92
    private function resolveSetting($var, $settings)
93
    {
94
        $split = explode('.', $var);
95
        foreach ($split as $value) {
96
            if (array_key_exists($value, $settings)) {
97
                $settings = $settings[$value];
98
            } else {
99
                return null;
100
            }
101
        }
102
103
        return $settings;
104
    }
105
106
    private function resolveSiteAttribute($var, $currentSite)
107
    {
108
        if ($var == 'domain') {
109
            return $currentSite->domain;
110
        }
111
        if ($var == 'name') {
112
            return $currentSite->name;
113
        }
114
        if ($var == 'slug') {
115
            return $currentSite->slug;
116
        }
117
    }
118
119
    public function module(string $slug)
120
    {
121
        return $this->moduleRepository->get($slug);
122
    }
123
}