Completed
Branch develop (4949ff)
by Nate
02:09
created

AccessorByString   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 73
ccs 0
cts 29
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
stringProperty() 0 1 ?
A findByString() 0 6 1
A getByString() 0 8 2
A notFoundByStringException() 0 9 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
7
 */
8
9
namespace flipbox\ember\services\traits\records;
10
11
use flipbox\ember\exceptions\RecordNotFoundException;
12
use yii\db\ActiveRecord as Record;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
trait AccessorByString
19
{
20
    use Accessor {
21
        find as parentFind;
22
    }
23
24
    /**
25
     * @return string
26
     */
27
    abstract protected static function stringProperty(): string;
28
29
    /*******************************************
30
     * OVERRIDE
31
     *******************************************/
32
33
    /**
34
     * @param $identifier
35
     * @param string|null $toScenario
36
     * @return Record|null
37
     */
38
    public function find($identifier, string $toScenario = null)
39
    {
40
        if (!is_numeric($identifier) && is_string($identifier)) {
41
            return $this->findByString($identifier, $toScenario);
0 ignored issues
show
Unused Code introduced by
The call to AccessorByString::findByString() has too many arguments starting with $toScenario.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
42
        }
43
44
        return $this->parentFind($identifier, $toScenario);
0 ignored issues
show
Bug introduced by
It seems like parentFind() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
    }
46
47
    /*******************************************
48
     * FIND STRING
49
     *******************************************/
50
51
    /**
52
     * @param string $string
53
     * @return Record|null
54
     */
55
    public function findByString(string $string)
56
    {
57
        return $this->findByCondition(
58
            [self::stringProperty() => $string]
59
        );
60
    }
61
62
    /**
63
     * @param string $string
64
     * @throws RecordNotFoundException
65
     * @return Record|null
66
     */
67
    public function getByString(string $string)
68
    {
69
        if (null === ($record = $this->findByString($string))) {
70
            $this->notFoundByStringException($string);
71
        }
72
73
        return $record;
74
    }
75
76
    /**
77
     * @param string|null $string
78
     * @throws RecordNotFoundException
79
     */
80
    protected function notFoundByStringException(string $string = null)
81
    {
82
        throw new RecordNotFoundException(
83
            sprintf(
84
                'Record does not exist with the string "%s".',
85
                (string)$string
86
            )
87
        );
88
    }
89
90
}