Passed
Push — master ( f2dea1...58a092 )
by Nate
04:47
created

ObjectById::find()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
nc 3
cc 3
eloc 6
nop 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\services;
10
11
use flipbox\spark\objects\ObjectWithId;
12
use flipbox\spark\Records\Record;
13
use flipbox\spark\records\RecordWithId;
14
use yii\base\Object as BaseObject;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.2.0
19
 */
20
abstract class ObjectById extends Object
21
{
22
23
    use traits\ObjectById;
24
25
    /*******************************************
26
     * FIND/GET
27
     *******************************************/
28
29
    /**
30
     * @inheritdoc
31
     */
32 View Code Duplication
    public function find($identifier, string $toScenario = null)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
35
        if ($object = parent::find($identifier, $toScenario)) {
36
            return $object;
37
        }
38
39
        if (!is_numeric($identifier)) {
40
            return null;
41
        }
42
43
        return $this->findById($identifier, $toScenario);
44
45
    }
46
47
    /*******************************************
48
     * CACHE
49
     *******************************************/
50
51
    /**
52
     * @inheritdoc
53
     */
54 View Code Duplication
    public function findCache($identifier)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
57
        if ($object = parent::findCache($identifier)) {
58
            return $object;
59
        }
60
61
        if (!is_numeric($identifier)) {
62
            return null;
63
        }
64
65
        return $this->findCacheById($identifier);
66
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 View Code Duplication
    public function findCacheByRecord(Record $record)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
75
        if ($object = parent::findCacheByRecord($record)) {
76
            return $object;
77
        }
78
79
        if (!$record instanceof RecordWithId) {
80
            return null;
81
        }
82
83
        // Check if already in cache by id
84
        return $this->findCacheById($record->id);
85
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function addToCache(BaseObject $object)
92
    {
93
94
        parent::addToCache($object);
95
96
        if ($object instanceof ObjectWithId) {
97
            $this->cacheById($object);
98
        }
99
100
        return $this;
101
102
    }
103
104
}
105