Completed
Push — master ( 3407e1...79fe03 )
by Nate
07:26
created

ConnectionValidator::validateConnection()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 18
cp 0
rs 9.568
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 20
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);
1 ignored issue
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Flipbox\Salesforce\Conn...nectionInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
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