Passed
Push — master ( aba784...c39888 )
by Andre
09:06 queued 11s
created

Provider::isAppleProvider()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\elements;
10
11
use Craft;
12
use craft\base\Element;
13
use craft\behaviors\FieldLayoutBehavior;
14
use craft\elements\actions\Restore;
15
use craft\elements\db\ElementQueryInterface;
16
use craft\helpers\UrlHelper;
17
18
use enupal\socializer\elements\actions\Delete;
19
use enupal\socializer\records\Provider as ProviderRecord;
20
use craft\validators\UniqueValidator;
21
use enupal\socializer\elements\db\ProvidersQuery;
22
use enupal\socializer\Socializer;
23
use enupal\socializer\validators\EnabledValidator;
24
use Hybridauth\Adapter\AdapterInterface;
25
use Hybridauth\Provider\Apple;
26
use yii\base\Model;
27
28
/**
29
 * Provider represents a provider element.
30
 *
31
 */
32
class Provider extends Element
33
{
34
    /**
35
     * @inheritdoc
36
     */
37
    public $id;
38
39
    /**
40
     * @var string Name.
41
     */
42
    public $name;
43
44
    /**
45
     * @var string Handle.
46
     */
47
    public $handle;
48
49
    /**
50
     * @var string Type.
51
     */
52
    public $type;
53
54
    /**
55
     * @var string Client ID.
56
     */
57
    public $clientId;
58
59
    /**
60
     * @var string Client Secret.
61
     */
62
    public $clientSecret;
63
64
    /**
65
     * @var string Field Mapping
66
     */
67
    public $fieldMapping;
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function behaviors()
73
    {
74
        return array_merge(parent::behaviors(), [
75
            'fieldLayout' => [
76
                'class' => FieldLayoutBehavior::class,
77
                'elementType' => self::class
78
            ],
79
        ]);
80
    }
81
82
    public function init()
83
    {
84
        parent::init();
85
86
        $this->setScenario(Model::SCENARIO_DEFAULT);
87
88
        if ($this->fieldMapping && is_string($this->fieldMapping)) {
89
            $this->fieldMapping = json_decode($this->fieldMapping, true);
90
        }
91
    }
92
93
    /**
94
     * Returns the field context this element's content uses.
95
     *
96
     * @access protected
97
     * @return string
98
     */
99
    public function getFieldContext(): string
100
    {
101
        return 'enupalSocializer:' . $this->id;
102
    }
103
104
    /**
105
     * Returns the element type name.
106
     *
107
     * @return string
108
     */
109
    public static function displayName(): string
110
    {
111
        return Craft::t('enupal-socializer','Socializer');
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    public static function refHandle()
118
    {
119
        return 'socializer-providers';
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125
    public static function hasContent(): bool
126
    {
127
        return true;
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public static function hasTitles(): bool
134
    {
135
        return false;
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141
    public static function isLocalized(): bool
142
    {
143
        return false;
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149
    public static function hasStatuses(): bool
150
    {
151
        return true;
152
    }
153
154
    /**
155
     * @inheritdoc
156
     */
157
    public function getFieldLayout()
158
    {
159
        $behaviors = $this->getBehaviors();
160
        $fieldLayout = $behaviors['fieldLayout'];
161
162
        return $fieldLayout->getFieldLayout();
163
    }
164
165
    /**
166
     * @inheritdoc
167
     */
168
    public function getCpEditUrl()
169
    {
170
        return UrlHelper::cpUrl(
171
            'enupal-socializer/providers/edit/' . $this->id
172
        );
173
    }
174
175
    /**
176
     * Use the name as the string representation.
177
     *
178
     * @return string
179
     */
180
    public function __toString()
181
    {
182
        return $this->name;
183
    }
184
185
    /**
186
     * @inheritdoc
187
     *
188
     * @return ProvidersQuery The newly created [[ProvidersQuery]] instance.
189
     */
190
    public static function find(): ElementQueryInterface
191
    {
192
        return new ProvidersQuery(get_called_class());
193
    }
194
195
    /**
196
     * @inheritdoc
197
     */
198
    protected static function defineSources(string $context = null): array
199
    {
200
        $sources = [
201
            [
202
                'key' => '*',
203
                'label' => Craft::t('enupal-socializer','All Providers'),
204
            ]
205
        ];
206
207
        // @todo add groups
208
209
        return $sources;
210
    }
211
212
    /**
213
     * @inheritdoc
214
     */
215
    protected static function defineActions(string $source = null): array
216
    {
217
        $actions = [];
218
219
        // Delete
220
        $actions[] = Craft::$app->getElements()->createAction([
221
            'type' => Delete::class
222
        ]);
223
224
        $actions[] = Craft::$app->getElements()->createAction([
225
            'type' => Restore::class,
226
            'successMessage' => 'Providers restored'
227
        ]);
228
229
        return $actions;
230
    }
231
232
    /**
233
     * @inheritdoc
234
     */
235
    protected static function defineSearchableAttributes(): array
236
    {
237
        return ['name', 'type'];
238
    }
239
240
    /**
241
     * @inheritdoc
242
     */
243
    protected static function defineSortOptions(): array
244
    {
245
        $attributes = [
246
            'elements.dateCreated' => Craft::t('enupal-socializer','Date Created'),
247
            'name' => Craft::t('enupal-socializer','Name')
248
        ];
249
250
        return $attributes;
251
    }
252
253
    /**
254
     * @inheritdoc
255
     */
256
    protected static function defineTableAttributes(): array
257
    {
258
        $attributes = [];
259
        $attributes['name'] = ['label' => Craft::t('enupal-socializer','Name')];
260
        $attributes['handle'] = ['label' => Craft::t('enupal-socializer','Handle')];
261
        $attributes['dateCreated'] = ['label' => Craft::t('enupal-socializer','Date Created')];
262
263
        return $attributes;
264
    }
265
266
    /**
267
     * @inheritdoc
268
     */
269
    protected static function defineDefaultTableAttributes(string $source): array
270
    {
271
        $attributes = ['name', 'handle' , 'dateCreated'];
272
273
        return $attributes;
274
    }
275
276
    /**
277
     * @inheritdoc
278
     * @throws \yii\base\InvalidConfigException
279
     * @throws \yii\base\InvalidConfigException
280
     */
281
    protected function tableAttributeHtml(string $attribute): string
282
    {
283
        switch ($attribute) {
284
            case 'dateCreated':
285
                {
286
                    return $this->dateCreated->/** @scrutinizer ignore-call */ format("Y-m-d H:i");
287
                }
288
        }
289
290
        return parent::tableAttributeHtml($attribute);
291
    }
292
293
    /**
294
     * @inheritdoc
295
     */
296
    public function datetimeAttributes(): array
297
    {
298
        $attributes = parent::datetimeAttributes();
299
        $attributes[] = 'dateCreated';
300
        return $attributes;
301
    }
302
303
    /**
304
     * @inheritdoc
305
     * @param bool $isNew
306
     * @throws \Exception
307
     */
308
    public function afterSave(bool $isNew)
309
    {
310
        $record = new ProviderRecord();
311
312
        if (!$isNew) {
313
            $record = ProviderRecord::findOne($this->id);
314
315
            if (!$record) {
0 ignored issues
show
introduced by
$record is of type yii\db\ActiveRecord, thus it always evaluated to true.
Loading history...
316
                throw new \Exception('Invalid Provider ID: ' . $this->id);
317
            }
318
        } else {
319
            $record->id = $this->id;
320
        }
321
322
        $record->name = $this->name;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
323
        $record->type = $this->type;
324
        $record->handle = $this->handle;
325
326
        $record->clientId = $this->clientId;
327
        $record->clientSecret = $this->clientSecret;
328
        $record->fieldMapping = $this->fieldMapping;
329
330
        if (is_array($record->fieldMapping)){
0 ignored issues
show
introduced by
The condition is_array($record->fieldMapping) is always false.
Loading history...
331
            $record->fieldMapping = json_encode($record->fieldMapping);
332
        };
333
334
        $record->save(false);
335
336
        parent::afterSave($isNew);
337
    }
338
339
    /**
340
     * @return AdapterInterface
341
     */
342
    public function getAdapter()
343
    {
344
        return new $this->type($this->getProviderConfig());
345
    }
346
347
    /**
348
     * @return array
349
     */
350
    private function getProviderConfig()
351
    {
352
        // @todo add event to give a chance to update default config
353
        $config = [
354
            'callback' => Socializer::$app->settings->getCallbackUrl(),
355
            'keys' => [
356
                'id' => $this->getClientId(),
357
                'secret' => $this->getClientSecret()
358
            ],
359
            'includeEmail' => true
360
        ];
361
362
        if ($this->type === Apple::class && Socializer::$app->settings->validateAppleSettings()) {
363
            $configSettings = Socializer::$app->settings->getConfigSettings();
364
365
            if (isset($configSettings['apple'])) {
366
                $config = $configSettings['apple'];
367
                $config['callback'] = Socializer::$app->settings->getCallbackUrl();
368
            }
369
        }
370
371
        return $config;
372
    }
373
374
    /**
375
     * @return string
376
     */
377
    public function getClientId()
378
    {
379
        return Craft::parseEnv($this->clientId);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Craft::parseEnv($this->clientId) also could return the type boolean which is incompatible with the documented return type string.
Loading history...
380
    }
381
382
    /**
383
     * @return string
384
     */
385
    public function getClientSecret()
386
    {
387
        return Craft::parseEnv($this->clientSecret);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Craft::parseEnv($this->clientSecret) also could return the type boolean which is incompatible with the documented return type string.
Loading history...
388
    }
389
390
    /**
391
     * @return array|null
392
     */
393
    public function getCurrentUserToken()
394
    {
395
        return Socializer::$app->tokens->getCurrentUserToken($this->id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return enupal\socializer...entUserToken($this->id) also could return the type craft\base\ElementInterface which is incompatible with the documented return type array|null.
Loading history...
396
    }
397
398
    /**
399
     * @inheritdoc
400
     */
401
    public function rules()
402
    {
403
        $rules = parent::rules();
404
        $rules[] = [['name', 'type'], 'required'];
405
        $rules[] = [['name', 'type'], 'string', 'max' => 255];
406
        $rules[] = [['name', 'type'], UniqueValidator::class, 'targetClass' => ProviderRecord::class];
407
        $rules[] = [['name', 'type'], 'required'];
408
        $rules[] = [['clientId'], EnabledValidator::class];
409
410
        return $rules;
411
    }
412
413
    public function isAppleProvider()
414
    {
415
        return $this->type === Apple::class;
416
    }
417
}