|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/force/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/force/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\salesforce\validators; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use flipbox\craft\salesforce\connections\SavableConnectionInterface; |
|
13
|
|
|
use flipbox\craft\salesforce\records\Connection; |
|
14
|
|
|
use Flipbox\Salesforce\Connections\ConnectionInterface; |
|
15
|
|
|
use yii\base\Model; |
|
16
|
|
|
use yii\validators\Validator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Flipbox Factory <[email protected]> |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class ConnectionValidator extends Validator |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @inheritdoc |
|
26
|
|
|
*/ |
|
27
|
|
|
public function validateAttribute($model, $attribute) |
|
28
|
|
|
{ |
|
29
|
|
|
$class = $model->$attribute; |
|
30
|
|
|
|
|
31
|
|
|
// Handles are always required, so if it's blank, the required validator will catch this. |
|
32
|
|
|
if ($class) { |
|
33
|
|
|
if (!$this->isValid($class)) { |
|
34
|
|
|
$message = Craft::t( |
|
35
|
|
|
'hubspot', |
|
36
|
|
|
'“{class}” is a not a valid connection.', |
|
37
|
|
|
['class' => $class] |
|
38
|
|
|
); |
|
39
|
|
|
$this->addError($model, $attribute, $message); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->validateConnection($model, $class, $attribute); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $class |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function isValid(string $class): bool |
|
51
|
|
|
{ |
|
52
|
|
|
return $class instanceof ConnectionInterface || |
|
53
|
|
|
is_subclass_of($class, ConnectionInterface::class); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Model $model |
|
58
|
|
|
* @param string $class |
|
59
|
|
|
* @param $attribute |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function validateConnection(Model $model, string $class, $attribute) |
|
62
|
|
|
{ |
|
63
|
|
|
if (!$model instanceof Connection) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** @var ConnectionInterface $connection */ |
|
68
|
|
|
$connection = $model->getConnection(); |
|
69
|
|
|
|
|
70
|
|
|
if (!$connection instanceof SavableConnectionInterface) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!$connection->validate()) { |
|
75
|
|
|
$message = Craft::t( |
|
76
|
|
|
'hubspot', |
|
77
|
|
|
'Invalid settings.', |
|
78
|
|
|
['class' => $class] |
|
79
|
|
|
); |
|
80
|
|
|
$this->addError($model, $attribute, $message); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|