Passed
Push — master ( 4c5faf...429320 )
by Chris
04:23
created

DataManagerFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 84
ccs 9
cts 20
cp 0.45
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 13 3
A getManagers() 0 3 1
A addManager() 0 5 1
A __construct() 0 4 1
A addManagers() 0 5 1
1
<?php
2
3
namespace WebTheory\Saveyour\Factories;
4
5
use Exception;
6
use WebTheory\GuctilityBelt\Traits\ClassResolverTrait;
7
use WebTheory\GuctilityBelt\Traits\SmartFactoryTrait;
8
use WebTheory\Saveyour\Contracts\FieldDataManagerInterface;
9
use WebTheory\Saveyour\Contracts\FieldDataManagerResolverFactoryInterface;
10
use WebTheory\Saveyour\Managers\FieldDataManagerCallback;
11
12
class DataManagerFactory implements FieldDataManagerResolverFactoryInterface
13
{
14
    use SmartFactoryTrait;
0 ignored issues
show
Bug introduced by
The trait WebTheory\GuctilityBelt\Traits\SmartFactoryTrait requires the property $name which is not provided by WebTheory\Saveyour\Factories\DataManagerFactory.
Loading history...
15
    use ClassResolverTrait;
16
17
    /**
18
     *
19
     */
20
    private $managers = [];
21
22
    /**
23
     *
24
     */
25
    protected $namespaces = [];
26
27
    public const NAMESPACES = [
28
        'webtheory.saveyour' =>  "WebTheory\\Saveyour\\Managers",
29
    ];
30
31
    public const MANAGERS = [
32
        'callback' => FieldDataManagerCallback::class
33
    ];
34
35
    protected const CONVENTION = '%sFieldDataManager';
36
37
    /**
38
     *
39
     */
40 9
    public function __construct(array $namespaces = [], array $managers = [])
41
    {
42 9
        $this->namespaces = $namespaces + static::NAMESPACES;
43 9
        $this->managers = $managers + static::MANAGERS;
44 9
    }
45
46
    /**
47
     * Get the value of managers
48
     *
49
     * @return mixed
50
     */
51
    public function getManagers()
52
    {
53
        return $this->managers;
54
    }
55
56
    /**
57
     * Set the value of managers
58
     *
59
     * @param mixed $managers
60
     *
61
     * @return self
62
     */
63
    public function addManager(string $arg, string $manager)
64
    {
65
        $this->managers[$arg] = $manager;
66
67
        return $this;
68
    }
69
70
    /**
71
     *
72
     */
73
    public function addManagers(array $managers)
74
    {
75
        $this->managers = $managers + $this->managers;
76
77
        return $this;
78
    }
79
80
    /**
81
     *
82
     */
83 9
    public function create(string $manager, array $args = []): FieldDataManagerInterface
84
    {
85 9
        $manager = $this->managers[$manager] ?? null;
86
87 9
        if (isset($manager)) {
88 9
            $manager = $this->build($manager, $args);
89
        } elseif ($class = $this->getClass($manager)) {
0 ignored issues
show
Bug introduced by
$manager of type null is incompatible with the type string expected by parameter $arg of WebTheory\Saveyour\Facto...agerFactory::getClass(). ( Ignorable by Annotation )

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

89
        } elseif ($class = $this->getClass(/** @scrutinizer ignore-type */ $manager)) {
Loading history...
90
            $manager = $this->build($class, $args);
91
        } else {
92
            throw new Exception("{$manager} is not a recognized data manager");
93
        }
94
95 9
        return $manager;
96
    }
97
}
98