HasManagerRecordTrait::getManager()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 3
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Nip\Records\Traits\HasManager;
4
5
use Exception;
6
use Nip\Records\AbstractModels\RecordManager;
7
use Nip\Records\Locator\ModelLocator;
8
9
/**
10
 * Trait HasManagerRecordTrait
11
 * @package Nip\Records\Traits\HasManager
12
 */
13
trait HasManagerRecordTrait
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $managerName = null;
19
20
    /**
21
     * @return RecordManager
22
     */
23 16
    public function getManager()
24
    {
25 16
        if (!$this->hasManagerName()) {
26 3
            $manager = ModelLocator::for($this);
27 3
            $this->setManager($manager);
28 3
            return $manager;
29
        }
30 13
        return ModelLocator::get($this->getManagerName());
31
    }
32
33
    /**
34
     * @param RecordManager $manager
35
     */
36 23
    public function setManager($manager)
37
    {
38 23
        $class = get_class($manager);
39 23
        ModelLocator::set($class, $manager);
40 23
        $this->setManagerName($class);
41 23
    }
42
43
    /**
44
     * @return string
45
     */
46 16
    public function getManagerName()
47
    {
48 16
        if ($this->hasManagerName() === false) {
49 3
            $this->initManagerName();
50
        }
51
52 16
        return $this->managerName;
53
    }
54
55
    /**
56
     * @param string $managerName
57
     */
58 24
    public function setManagerName($managerName)
59
    {
60 24
        $this->managerName = $managerName;
61 24
    }
62
63
    /**
64
     * @return bool
65
     */
66 19
    public function hasManagerName()
67
    {
68 19
        return is_string($this->managerName);
69
    }
70
71 3
    protected function initManagerName()
72
    {
73 3
        $this->setManagerName($this->inflectManagerName());
74 3
    }
75
76
    /**
77
     * @return string
78
     */
79 3
    protected function inflectManagerName()
80
    {
81 3
        return ucfirst(inflector()->pluralize($this->getClassName()));
0 ignored issues
show
Bug introduced by
It seems like getClassName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        return ucfirst(inflector()->pluralize($this->/** @scrutinizer ignore-call */ getClassName()));
Loading history...
82
    }
83
}
84