SyncedTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
key() 0 1 ?
A isSynced() 0 14 3
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Entities\Relations\Traits;
9
10
use Spiral\ORM\Record;
11
use Spiral\ORM\RecordInterface;
12
13
/**
14
 * Checks if two records are synced (only for simple relations)
15
 */
16
trait SyncedTrait
17
{
18
    /**
19
     * If record not synced or can't be synced. Only work for PK based relations.
20
     *
21
     * @param RecordInterface $inner
22
     * @param RecordInterface $outer
23
     *
24
     * @return bool
25
     */
26
    protected function isSynced(RecordInterface $inner, RecordInterface $outer): bool
27
    {
28
        if (empty($inner->primaryKey()) || empty($outer->primaryKey())) {
29
            //Parent not saved
30
            return false;
31
        }
32
33
        //Comparing FK values
34
        return $outer->getField(
35
                $this->key(Record::OUTER_KEY)
36
            ) == $inner->getField(
37
                $this->key(Record::INNER_KEY)
38
            );
39
    }
40
41
    /**
42
     * Key name.
43
     *
44
     * @param int $key
45
     *
46
     * @return string|null
47
     */
48
    abstract protected function key(int $key);
49
}
50