Completed
Push — develop ( 04ac75...c0d878 )
by Nate
10:49
created

ProviderLocks::dissociate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\patron\services;
10
11
use flipbox\ember\services\traits\records\Accessor;
12
use flipbox\patron\db\ProviderQuery;
13
use flipbox\patron\records\ProviderLock;
14
use yii\base\Component;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 *
20
 * @method ProviderQuery getQuery($config = [])
21
 * @method ProviderLock create(array $attributes = [])
22
 * @method ProviderLock find($identifier)
23
 * @method ProviderLock get($identifier)
24
 * @method ProviderLock findByCondition($condition = [])
25
 * @method ProviderLock getByCondition($condition = [])
26
 * @method ProviderLock findByCriteria($criteria = [])
27
 * @method ProviderLock getByCriteria($criteria = [])
28
 * @method ProviderLock[] findAllByCondition($condition = [])
29
 * @method ProviderLock[] getAllByCondition($condition = [])
30
 * @method ProviderLock[] findAllByCriteria($criteria = [])
31
 * @method ProviderLock[] getAllByCriteria($criteria = [])
32
 */
33
class ProviderLocks extends Component
34
{
35
    use Accessor;
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public static function recordClass(): string
41
    {
42
        return ProviderLock::class;
43
    }
44
45
46
    /*******************************************
47
     * ASSOCIATE / DISSOCIATE
48
     *******************************************/
49
50
    /**
51
     * @param ProviderLock $record
52
     * @return bool
53
     * @throws \Exception
54
     */
55
    public function associate(
56
        ProviderLock $record
57
    ): bool {
58
        if (true === $this->existingAssociation($record)) {
59
            $reOrder = true;
0 ignored issues
show
Unused Code introduced by
$reOrder is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
        }
61
62
        return $record->save();
63
    }
64
65
    /**
66
     * @param ProviderLock $record
67
     * @return bool
68
     * @throws \yii\db\Exception
69
     */
70
    public function dissociate(
71
        ProviderLock $record
72
    ) {
73
        if (false === $this->existingAssociation($record)) {
74
            return true;
75
        }
76
77
        return $record->delete();
78
    }
79
80
81
    /*******************************************
82
     * EXISTING
83
     *******************************************/
84
85
    /**
86
     * @param ProviderLock $record
87
     * @return bool
88
     */
89
    protected function existingAssociation(
90
        ProviderLock $record
91
    ): bool {
92
        if (null !== ($existing = $this->lookupAssociation($record))) {
93
            $record->setOldAttributes(
94
                $existing->getOldAttributes()
95
            );
96
        }
97
98
        return $existing !== null;
99
    }
100
101
    /**
102
     * @param ProviderLock $record
103
     * @return ProviderLock|null
104
     */
105
    protected function lookupAssociation(
106
        ProviderLock $record
107
    ) {
108
        $model = $this->getQuery()
109
            ->where([
110
                'providerId' => $record->providerId,
111
                'pluginId' => $record->pluginId
112
            ])->one();
113
114
        return $model instanceof ProviderLock ? $model : null;
115
    }
116
117
    /**
118
     * @param int $providerId
119
     * @param int $pluginId
120
     * @param int|null $sortOrder
0 ignored issues
show
Bug introduced by
There is no parameter named $sortOrder. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
121
     * @return bool
122
     */
123
    public function associateByIds(
124
        int $providerId,
125
        int $pluginId
126
    ): bool {
127
        return $this->create([
0 ignored issues
show
Documentation Bug introduced by
The method associate does not exist on object<flipbox\patron\records\ProviderLock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
128
            'providerId' => $providerId,
129
            'pluginId' => $pluginId
130
        ])->associate();
131
    }
132
133
    /**
134
     * @param int $providerId
135
     * @param int $pluginId
136
     * @param int|null $sortOrder
0 ignored issues
show
Bug introduced by
There is no parameter named $sortOrder. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
137
     * @return bool
138
     */
139
    public function dissociateByIds(
140
        int $providerId,
141
        int $pluginId
142
    ): bool {
143
        return $this->create([
0 ignored issues
show
Documentation Bug introduced by
The method dissociate does not exist on object<flipbox\patron\records\ProviderLock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
144
            'providerId' => $providerId,
145
            'pluginId' => $pluginId
146
        ])->dissociate();
147
    }
148
}
149