Issues (45)

src/services/Settings.php (3 issues)

Labels
1
<?php
2
/**
3
 * Socializer plugin for Craft CMS 3.x
4
 *
5
 * @link      https://enupal.com/
6
 * @copyright Copyright (c) 2019 Enupal LLC
7
 */
8
9
namespace enupal\socializer\services;
10
11
use Craft;
12
use craft\db\Query;
13
use craft\helpers\UrlHelper;
14
use yii\base\Component;
15
use enupal\socializer\models\Settings as SettingsModel;
16
use enupal\socializer\Socializer;
17
use craft\helpers\App;
0 ignored issues
show
This use statement conflicts with another class in this namespace, enupal\socializer\services\App. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
19
class Settings extends Component
20
{
21
    /**
22
     * Saves Settings
23
     *
24
     * @param $scenario
25
     * @param $settings SettingsModel
26
     *
27
     * @return bool
28
     */
29
    public function saveSettings(SettingsModel $settings, $scenario = null): bool
30
    {
31
        $plugin = $this->getPlugin();
32
33
        if (!is_null($scenario)) {
34
            $settings->setScenario($scenario);
35
        }
36
37
        // Validate them, now that it's a model
38
        if ($settings->validate() === false) {
39
            return false;
40
        }
41
42
        $success = Craft::$app->getPlugins()->savePluginSettings($plugin, $settings->getAttributes());
43
44
        return $success;
45
    }
46
47
    /***
48
     * @return string
49
     * @throws \craft\errors\SiteNotFoundException
50
     */
51
    public function getPrimarySiteUrl()
52
    {
53
        return UrlHelper::baseSiteUrl();
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getCallbackUrl()
60
    {
61
        $baseUrl = $this->getPrimarySiteUrl();
62
        $settings = $this->getSettings();
63
        if (!empty($settings->siteUrl)) {
64
            $settingsBaseUrl = rtrim(trim(App::parseEnv($settings->siteUrl), "/"));
0 ignored issues
show
It seems like craft\helpers\App::parseEnv($settings->siteUrl) can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

64
            $settingsBaseUrl = rtrim(trim(/** @scrutinizer ignore-type */ App::parseEnv($settings->siteUrl), "/"));
Loading history...
65
            if (UrlHelper::isProtocolRelativeUrl($settingsBaseUrl)) {
66
                $baseUrl = $settingsBaseUrl;
67
            }
68
        }
69
70
        return $baseUrl."/socializer/login/callback";
71
    }
72
73
    /**
74
     * @return SettingsModel
75
     */
76
    public function getSettings()
77
    {
78
        /** @var SettingsModel $settings */
79
        $settings = $this->getPlugin()->getSettings();
80
81
        return $settings;
82
    }
83
84
    /**
85
     * @return \craft\base\PluginInterface|null
86
     */
87
    public function getPlugin()
88
    {
89
        return Craft::$app->getPlugins()->getPlugin('enupal-socializer');
90
    }
91
92
    /**
93
     * @return string|null
94
     */
95
    public function getPluginUid()
96
    {
97
        $plugin = (new Query())
98
            ->select(['uid'])
99
            ->from('{{%plugins}}')
100
            ->where(["handle" => 'enupal-socializer'])
101
            ->one();
102
103
        return $plugin['uid'] ?? null;
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function getGlobalFieldMapping()
110
    {
111
        $settings = $this->getSettings();
112
113
        return $settings->fieldMapping ?? Socializer::$app->providers->getDefaultFieldMapping();
114
    }
115
116
    /**
117
     * @return array|\craft\models\UserGroup[]
118
     */
119
    public function getUserGroups()
120
    {
121
        $userGroups = [
122
            ["name" => "None", "id" => ""]
123
        ];
124
        $craftUserGroups = Craft::$app->getUserGroups()->getAllGroups();
125
126
        $userGroups = array_merge($userGroups, $craftUserGroups);
127
128
        return $userGroups;
129
    }
130
131
    /**
132
     * @return array|null
133
     */
134
    public function getConfigSettings()
135
    {
136
        return Craft::$app->config->getGeneral()->socializer ?? null;
0 ignored issues
show
The method getGeneral() does not exist on null. ( Ignorable by Annotation )

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

136
        return Craft::$app->config->/** @scrutinizer ignore-call */ getGeneral()->socializer ?? null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
    }
138
139
    public function validateAppleSettings()
140
    {
141
        $config = $this->getConfigSettings();
142
143
        if (!isset($config['apple'])) {
144
            Craft::error('Apple config is not set', __METHOD__);
145
            return false;
146
        }
147
148
        $apple = $config['apple'];
149
150
        if (!isset($apple['keys']['id']) || !isset($apple['keys']['team_id']) ||
151
            !isset($apple['keys']['key_id']) ||
152
            !isset($apple['keys']['key_file']) || !isset($apple['scope']) ||
153
            !isset($apple['verifyTokenSignature'])) {
154
            Craft::error('Missing a required Apple config, please check our docs.', __METHOD__);
155
            return false;
156
        }
157
158
        if (!file_exists($apple['keys']['key_file'])) {
159
            Craft::error('Unable to find Apple key file: '.$apple['keys']['key_file'], __METHOD__);
160
            return false;
161
        }
162
163
        return true;
164
    }
165
}
166