SoftManagedModelTrait::getReferenceSoft()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
dl 15
loc 15
ccs 10
cts 10
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 5
nop 1
crap 6
1
<?php
2
3
namespace LRC\Repository;
4
5
/**
6
 * Soft-deletion-aware foreign-key reference resolution for repository models using repository manager.
7
 */
8
trait SoftManagedModelTrait
9
{
10
    use ManagedModelTrait;
11
    
12
    
13
    /**
14
     * Retrieve a reference by name, ignoring soft-deleted entries.
15
     *
16
     * @param string    $name       Reference name.
17
     *
18
     * @return mixed                Model instance if found, null otherwise.
19
     *
20
     * @throws RepositoryException  If this model or the referenced model is not handled by a managed repository, or the referenced repository is not soft-deletion aware.
21
     */
22 3 View Code Duplication
    public function getReferenceSoft($name)
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...
23
    {
24 3
        if (!isset($this->_manager)) {
25 1
            throw new RepositoryException('Model is not handled by a managed repository');
26
        }
27 2
        if (array_key_exists($name, $this->_references)) {
28 2
            $ref = $this->_references[$name];
29 2
            $repo = $this->_manager->getByClass($ref['model']);
30 2
            if (!$repo || !($repo instanceof SoftRepositoryInterface)) {
31 1
                throw new RepositoryException('Referenced model is not handled by a managed soft-deletion-aware repository');
32
            }
33 1
            return ($repo->findSoft($ref['key'], $this->{$ref['attribute']}) ?: null);
34
        }
35 1
        return null;
36
    }
37
}
38